Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Tweak shape repr in _api.check_shape error message. #26513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/matplotlib/_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,10 @@ def check_shape(shape, /, **kwargs):
if (len(data_shape) != len(shape)
or any(s != t and t is not None for s, t in zip(data_shape, shape))):
dim_labels = iter(itertools.chain(
'MNLIJKLH',
'NMLKJIH',
(f"D{i}" for i in itertools.count())))
text_shape = ", ".join(str(n)
if n is not None
else next(dim_labels)
for n in shape)
text_shape = ", ".join([str(n) if n is not None else next(dim_labels)
for n in shape[::-1]][::-1])
if len(shape) == 1:
text_shape += ","

Expand Down
18 changes: 10 additions & 8 deletions lib/matplotlib/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
T = TypeVar('T')


@pytest.mark.parametrize('target,test_shape',
[((None, ), (1, 3)),
((None, 3), (1,)),
((None, 3), (1, 2)),
((1, 5), (1, 9)),
((None, 2, None), (1, 3, 1))
@pytest.mark.parametrize('target,shape_repr,test_shape',
[((None, ), "(N,)", (1, 3)),
((None, 3), "(N, 3)", (1,)),
((None, 3), "(N, 3)", (1, 2)),
((1, 5), "(1, 5)", (1, 9)),
((None, 2, None), "(M, 2, N)", (1, 3, 1))
])
def test_check_shape(target: tuple[int | None, ...],
shape_repr: str,
test_shape: tuple[int, ...]) -> None:
error_pattern = (f"^'aardvark' must be {len(target)}D.*" +
re.escape(f'has shape {test_shape}'))
error_pattern = "^" + re.escape(
f"'aardvark' must be {len(target)}D with shape {shape_repr}, but your input "
f"has shape {test_shape}")
data = np.zeros(test_shape)
with pytest.raises(ValueError, match=error_pattern):
_api.check_shape(target, aardvark=data)
Expand Down