From 82a845958537e1bf5743b034a1964be713157ccc Mon Sep 17 00:00:00 2001
From: Alexandru Fikl <alexfikl@gmail.com>
Date: Tue, 28 Sep 2021 09:38:16 -0500
Subject: [PATCH] make failed reshape message less opaque

---
 arraycontext/container/traversal.py | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/arraycontext/container/traversal.py b/arraycontext/container/traversal.py
index da17eec..cf9640f 100644
--- a/arraycontext/container/traversal.py
+++ b/arraycontext/container/traversal.py
@@ -530,9 +530,16 @@ def flatten(ary: ArrayOrContainerT, actx: ArrayContext) -> Any:
             except ValueError as exc:
                 # NOTE: we can't do much if the array context fails to ravel,
                 # since it is the one responsible for the actual memory layout
-                raise NotImplementedError("'flatten' requires advanced reshaping "
-                        "functionality that is not implemented in the array "
-                        f"context '{type(actx).__name__}'") from exc
+                if hasattr(subary, "strides"):
+                    strides_msg = f"and strides {subary.strides}"
+                else:
+                    strides_msg = ""
+
+                raise NotImplementedError(
+                        f"'{type(actx).__name__}.np.ravel' failed to reshape "
+                        f"an array with shape {subary.shape}{strides_msg}. "
+                        "This functionality needs to be implemented by the "
+                        "array context.") from exc
 
             result.append(flat_subary)
         else:
@@ -577,9 +584,11 @@ def unflatten(
             except ValueError as exc:
                 # NOTE: we can't do much if the array context fails to reshape,
                 # since it is the one responsible for the actual memory layout
-                raise NotImplementedError("'unflatten' requires advanced reshaping "
-                        "functionality that is not implemented in the array "
-                        f"context '{type(actx).__name__}'") from exc
+                raise NotImplementedError(
+                        f"'{type(actx).__name__}.np.reshape' failed to reshape "
+                        f"the flat array into shape {template_subary.shape}. "
+                        "This functionality needs to be implemented by the "
+                        "array context.") from exc
 
             if hasattr(template_subary, "strides"):
                 if template_subary.strides != subary.strides:
-- 
GitLab