| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
NumPy nightlies changed broadcast ValueError text so it no longer includes "arg N", which broke the string-replace remapping in Axes.bar and failed test_bar_shape_mismatch (matplotlib#32430). Validate broadcastability with np.broadcast_shapes and raise a named ValueError from the first non-broadcastable argument pair, instead of parsing NumPy's wording. Successful broadcasts are unchanged. Fixes matplotlib#32430
There was a problem hiding this comment.
Let's keep this simpler. Note: We do not guarantee stability of exception messages.
Sorry, something went wrong.
| np.broadcast_shapes(*shapes) | ||
| except ValueError: | ||
| # Report the first pair (in argument order) that cannot broadcast, | ||
| # matching the historical named message users and tests expect. | ||
| for i in range(len(shapes)): | ||
| for j in range(i + 1, len(shapes)): | ||
| try: | ||
| np.broadcast_shapes(shapes[i], shapes[j]) | ||
| except ValueError: | ||
| raise ValueError( | ||
| "shape mismatch: objects cannot be broadcast to a " | ||
| "single shape. Mismatch is between " | ||
| f"'{arg_names[i]}' with shape {shapes[i]} and " | ||
| f"'{arg_names[j]}' with shape {shapes[j]}." | ||
| ) from None | ||
| raise |
There was a problem hiding this comment.
I think this is too much effort. Instead we should:
i.e.
except ValueError as e:
non_trivial_inputs = ", ".join(
f"{name} {np.shape(arg)}" for arg, name in zip(args, arg_names)
# filter out the args that could contribute to a broadcast error (i.e. non-trival shapes)
if any(dim != 1 for dim in np.shape(arg))
)
raise ValueError(f"bar() failed on broadcasting inputs: {non_trivial_inputs}") from e
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
PR summary
Fixes #32430.
Axes.bar used to catch NumPy's broadcast ValueError and rewrite "arg 0" → "'x'", "arg 1" → "'height'", etc. NumPy nightlies changed that message to something like operands could not be broadcast together with shapes (2,) (3,) () () () () (no arg N), so the remapping no longer ran and test_bar_shape_mismatch failed with the cryptic raw text.
This PR stops parsing NumPy's wording. Before np.broadcast_arrays, it checks shapes with np.broadcast_shapes and, on failure, finds the first non-broadcastable argument pair in order and raises:
shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between 'x' with shape (2,) and 'height' with shape (3,).
Successful broadcasts are unchanged. test_bar_shape_mismatch keeps the existing x/height case and adds a width mismatch case.
AI Disclosure
AI-assisted (Cursor agent under my direction). I specified the root cause, required fix (shape-based named error, not a test-only regex change), and reviewed the resulting patch and tests myself.
PR quality check