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

Skip to content

Commit 9f9ea54

Browse files
committed
Move cbook._check_shape() to _api.check_shape()
1 parent f0be6b1 commit 9f9ea54

File tree

5 files changed

+63
-58
lines changed

5 files changed

+63
-58
lines changed

lib/matplotlib/_api.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12

23

34
def check_in_list(_values, *, _print_supported_values=True, **kwargs):
@@ -31,3 +32,42 @@ def check_in_list(_values, *, _print_supported_values=True, **kwargs):
3132
f"supported values are {', '.join(map(repr, values))}")
3233
else:
3334
raise ValueError(f"{val!r} is not a valid value for {key}")
35+
36+
37+
def check_shape(_shape, **kwargs):
38+
"""
39+
For each *key, value* pair in *kwargs*, check that *value* has the shape
40+
*_shape*, if not, raise an appropriate ValueError.
41+
42+
*None* in the shape is treated as a "free" size that can have any length.
43+
e.g. (None, 2) -> (N, 2)
44+
45+
The values checked must be numpy arrays.
46+
47+
Examples
48+
--------
49+
To check for (N, 2) shaped arrays
50+
51+
>>> _api.check_shape((None, 2), arg=arg, other_arg=other_arg)
52+
"""
53+
target_shape = _shape
54+
for k, v in kwargs.items():
55+
data_shape = v.shape
56+
57+
if len(target_shape) != len(data_shape) or any(
58+
t not in [s, None]
59+
for t, s in zip(target_shape, data_shape)
60+
):
61+
dim_labels = iter(itertools.chain(
62+
'MNLIJKLH',
63+
(f"D{i}" for i in itertools.count())))
64+
text_shape = ", ".join((str(n)
65+
if n is not None
66+
else next(dim_labels)
67+
for n in target_shape))
68+
69+
raise ValueError(
70+
f"{k!r} must be {len(target_shape)}D "
71+
f"with shape ({text_shape}). "
72+
f"Your input has shape {v.shape}."
73+
)

lib/matplotlib/cbook/__init__.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,45 +2281,6 @@ def type_name(tp):
22812281
type_name(type(v))))
22822282

22832283

2284-
def _check_shape(_shape, **kwargs):
2285-
"""
2286-
For each *key, value* pair in *kwargs*, check that *value* has the shape
2287-
*_shape*, if not, raise an appropriate ValueError.
2288-
2289-
*None* in the shape is treated as a "free" size that can have any length.
2290-
e.g. (None, 2) -> (N, 2)
2291-
2292-
The values checked must be numpy arrays.
2293-
2294-
Examples
2295-
--------
2296-
To check for (N, 2) shaped arrays
2297-
2298-
>>> _api.check_in_list((None, 2), arg=arg, other_arg=other_arg)
2299-
"""
2300-
target_shape = _shape
2301-
for k, v in kwargs.items():
2302-
data_shape = v.shape
2303-
2304-
if len(target_shape) != len(data_shape) or any(
2305-
t not in [s, None]
2306-
for t, s in zip(target_shape, data_shape)
2307-
):
2308-
dim_labels = iter(itertools.chain(
2309-
'MNLIJKLH',
2310-
(f"D{i}" for i in itertools.count())))
2311-
text_shape = ", ".join((str(n)
2312-
if n is not None
2313-
else next(dim_labels)
2314-
for n in target_shape))
2315-
2316-
raise ValueError(
2317-
f"{k!r} must be {len(target_shape)}D "
2318-
f"with shape ({text_shape}). "
2319-
f"Your input has shape {v.shape}."
2320-
)
2321-
2322-
23232284
def _check_getitem(_mapping, **kwargs):
23242285
"""
23252286
*kwargs* must consist of a single *key, value* pair. If *key* is in

lib/matplotlib/path.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import numpy as np
1616

1717
import matplotlib as mpl
18-
from . import _path, cbook
18+
from . import _api, _path, cbook
1919
from .cbook import _to_unmasked_float_array, simple_linear_interpolation
2020
from .bezier import BezierSegment
2121

@@ -129,7 +129,7 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1,
129129
and codes as read-only arrays.
130130
"""
131131
vertices = _to_unmasked_float_array(vertices)
132-
cbook._check_shape((None, 2), vertices=vertices)
132+
_api.check_shape((None, 2), vertices=vertices)
133133

134134
if codes is not None:
135135
codes = np.asarray(codes, self.code_type)

lib/matplotlib/tests/test_api.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import re
2+
3+
import numpy as np
4+
import pytest
5+
6+
from matplotlib import _api
7+
8+
9+
@pytest.mark.parametrize('target,test_shape',
10+
[((None, ), (1, 3)),
11+
((None, 3), (1,)),
12+
((None, 3), (1, 2)),
13+
((1, 5), (1, 9)),
14+
((None, 2, None), (1, 3, 1))
15+
])
16+
def test_check_shape(target, test_shape):
17+
error_pattern = (f"^'aardvark' must be {len(target)}D.*" +
18+
re.escape(f'has shape {test_shape}'))
19+
data = np.zeros(test_shape)
20+
with pytest.raises(ValueError, match=error_pattern):
21+
_api.check_shape(target, aardvark=data)

lib/matplotlib/tests/test_cbook.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import itertools
22
import pickle
3-
import re
43

54
from weakref import ref
65
from unittest.mock import patch, Mock
@@ -675,22 +674,6 @@ def divisors(n):
675674
check(x, rstride=rstride, cstride=cstride)
676675

677676

678-
@pytest.mark.parametrize('target,test_shape',
679-
[((None, ), (1, 3)),
680-
((None, 3), (1,)),
681-
((None, 3), (1, 2)),
682-
((1, 5), (1, 9)),
683-
((None, 2, None), (1, 3, 1))
684-
])
685-
def test_check_shape(target, test_shape):
686-
error_pattern = (f"^'aardvark' must be {len(target)}D.*" +
687-
re.escape(f'has shape {test_shape}'))
688-
data = np.zeros(test_shape)
689-
with pytest.raises(ValueError,
690-
match=error_pattern):
691-
cbook._check_shape(target, aardvark=data)
692-
693-
694677
def test_setattr_cm():
695678
class A:
696679

0 commit comments

Comments
 (0)