FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix bar() named shape-mismatch error under NumPy nightlies by mitre88 · Pull Request #32431 · matplotlib/matplotlib · GitHub

Fix bar() named shape-mismatch error under NumPy nightlies - #32431

Open
mitre88 wants to merge 1 commit into
matplotlib:mainfrom
mitre88:fix/bar-broadcast-named-shape-mismatch-32430
Open

mitre88 wants to merge 1 commit into
matplotlib:mainfrom
mitre88:fix/bar-broadcast-named-shape-mismatch-32430

Conversation

mitre88 commented Oct 4, 2026

Copy link
Copy Markdown

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

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New features and API changes have release notes
  • [N/A] Documentation complies with general and docstring guidelines

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

timhoffm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Let's keep this simpler. Note: We do not guarantee stability of exception messages.

Comment on lines +2531 to +2546
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think this is too much effort. Instead we should:

  • still directly to np.broadcast_arrays - it's better to ask for foregiveness than for permission
  • do not try to interpret the original error string
  • raise a ValueError from the original error only providing the context

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

This branch has not been deployed

No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TST] Upcoming dependency test failures

2 participants


Back | FazBrowse Home | New Git URL