Raise ValueError for non-string orientation in Axes.grouped_bar (#30706)#30707
Raise ValueError for non-string orientation in Axes.grouped_bar (#30706)#30707ilakkmanoharan wants to merge 1 commit intomatplotlib:mainfrom
Conversation
Use pathlib.Path instead of matplotlib.path.Path in text.pyi
|
Something went wrong with your commit. Perhaps you used |
|
Thanks for the report. This is an issue within For a correct solution, you’ll also have to check whether Consequently, the test should also be written for |
|
I did a quick search yesterday, and sometimes |
|
@ilakkmanoharan Thanks for the bug report and the attempt to fix this. We've now done the proper fix ourselves in #30714. Please understand that core developer time is precious and in this case implementing the fix ourselves is much more efficient than guiding you towards the correct solution and subsequently reviewing it. |
PR summary
This PR fixes issue #30706.
Why this change is necessary
Passing a non-string value (e.g.,
np.array,int,None) to theorientationparameter of
Axes.grouped_bar()previously caused a misleading NumPy error:ValueError: The truth value of an array with more than one element is ambiguous
This occurred because
_api.check_in_list()attempted a membership check on anon-scalar object, leading to an elementwise boolean comparison.
What problem it solves
This patch ensures that invalid (non-string)
orientationvalues raise a clean,user-facing
ValueErrorinstead of an ambiguous NumPy truth-value error.This behavior now matches other Matplotlib functions (e.g.,
barh,stem)that first validate enum arguments before list membership checks.
Implementation details
Added an early type guard in
Axes.grouped_bar()before calling_api.check_in_list():Tests
Added a new test test_grouped_bar_orientation_invalid() to
lib/matplotlib/tests/test_axes.py verifying the following cases:
"invalid"
1
None
np.array([1, 2, 3])
Each now raises:
ValueError: '' is not a valid value for orientation
Result
Clean ValueError instead of ambiguous truth-value error
Consistent behavior with other Matplotlib APIs
All tests pass locally
PR checklist
closes #30706
new and changed code is tested
[N/A] Plotting related features are demonstrated in an example
[N/A] New Features and API Changes are noted with a directive and release note
[N/A] Documentation complies with general and docstring guidelines