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

Skip to content

Commit 0e3fd10

Browse files
authored
Merge pull request #26513 from anntzer/csn
Tweak shape repr in _api.check_shape error message.
2 parents 27bcd08 + f37329b commit 0e3fd10

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

lib/matplotlib/_api/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,10 @@ def check_shape(shape, /, **kwargs):
151151
if (len(data_shape) != len(shape)
152152
or any(s != t and t is not None for s, t in zip(data_shape, shape))):
153153
dim_labels = iter(itertools.chain(
154-
'MNLIJKLH',
154+
'NMLKJIH',
155155
(f"D{i}" for i in itertools.count())))
156-
text_shape = ", ".join(str(n)
157-
if n is not None
158-
else next(dim_labels)
159-
for n in shape)
156+
text_shape = ", ".join([str(n) if n is not None else next(dim_labels)
157+
for n in shape[::-1]][::-1])
160158
if len(shape) == 1:
161159
text_shape += ","
162160

lib/matplotlib/tests/test_api.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717
T = TypeVar('T')
1818

1919

20-
@pytest.mark.parametrize('target,test_shape',
21-
[((None, ), (1, 3)),
22-
((None, 3), (1,)),
23-
((None, 3), (1, 2)),
24-
((1, 5), (1, 9)),
25-
((None, 2, None), (1, 3, 1))
20+
@pytest.mark.parametrize('target,shape_repr,test_shape',
21+
[((None, ), "(N,)", (1, 3)),
22+
((None, 3), "(N, 3)", (1,)),
23+
((None, 3), "(N, 3)", (1, 2)),
24+
((1, 5), "(1, 5)", (1, 9)),
25+
((None, 2, None), "(M, 2, N)", (1, 3, 1))
2626
])
2727
def test_check_shape(target: tuple[int | None, ...],
28+
shape_repr: str,
2829
test_shape: tuple[int, ...]) -> None:
29-
error_pattern = (f"^'aardvark' must be {len(target)}D.*" +
30-
re.escape(f'has shape {test_shape}'))
30+
error_pattern = "^" + re.escape(
31+
f"'aardvark' must be {len(target)}D with shape {shape_repr}, but your input "
32+
f"has shape {test_shape}")
3133
data = np.zeros(test_shape)
3234
with pytest.raises(ValueError, match=error_pattern):
3335
_api.check_shape(target, aardvark=data)

0 commit comments

Comments
 (0)