Skip to content

Commit

Permalink
Backport PR matplotlib#26513: Tweak shape repr in _api.check_shape er…
Browse files Browse the repository at this point in the history
…ror message.
  • Loading branch information
ksunden authored and meeseeksmachine committed Aug 15, 2023
1 parent 275a23f commit c301723
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
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

0 comments on commit c301723

Please sign in to comment.