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

Skip to content

Commit 100f45d

Browse files
committed
Use true positional args in check_foo APIs instead of simulating them.
1 parent 3e98838 commit 100f45d

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

lib/matplotlib/_api/__init__.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,20 @@ def fget(self):
5858
return self._fget
5959

6060

61-
# In the following check_foo() functions, the first parameter starts with an
62-
# underscore because it is intended to be positional-only (e.g., so that
63-
# `_api.check_isinstance([...], types=foo)` doesn't fail.
61+
# In the following check_foo() functions, the first parameter is positional-only to make
62+
# e.g. `_api.check_isinstance([...], types=foo)` work.
6463

65-
def check_isinstance(_types, **kwargs):
64+
def check_isinstance(types, /, **kwargs):
6665
"""
6766
For each *key, value* pair in *kwargs*, check that *value* is an instance
68-
of one of *_types*; if not, raise an appropriate TypeError.
67+
of one of *types*; if not, raise an appropriate TypeError.
6968
70-
As a special case, a ``None`` entry in *_types* is treated as NoneType.
69+
As a special case, a ``None`` entry in *types* is treated as NoneType.
7170
7271
Examples
7372
--------
7473
>>> _api.check_isinstance((SomeClass, None), arg=arg)
7574
"""
76-
types = _types
7775
none_type = type(None)
7876
types = ((types,) if isinstance(types, type) else
7977
(none_type,) if types is None else
@@ -98,31 +96,31 @@ def type_name(tp):
9896
type_name(type(v))))
9997

10098

101-
def check_in_list(_values, *, _print_supported_values=True, **kwargs):
99+
def check_in_list(values, /, *, _print_supported_values=True, **kwargs):
102100
"""
103-
For each *key, value* pair in *kwargs*, check that *value* is in *_values*.
101+
For each *key, value* pair in *kwargs*, check that *value* is in *values*;
102+
if not, raise an appropriate ValueError.
104103
105104
Parameters
106105
----------
107-
_values : iterable
106+
values : iterable
108107
Sequence of values to check on.
109108
_print_supported_values : bool, default: True
110-
Whether to print *_values* when raising ValueError.
109+
Whether to print *values* when raising ValueError.
111110
**kwargs : dict
112-
*key, value* pairs as keyword arguments to find in *_values*.
111+
*key, value* pairs as keyword arguments to find in *values*.
113112
114113
Raises
115114
------
116115
ValueError
117-
If any *value* in *kwargs* is not found in *_values*.
116+
If any *value* in *kwargs* is not found in *values*.
118117
119118
Examples
120119
--------
121120
>>> _api.check_in_list(["foo", "bar"], arg=arg, other_arg=other_arg)
122121
"""
123122
if not kwargs:
124123
raise TypeError("No argument to check!")
125-
values = _values
126124
for key, val in kwargs.items():
127125
if val not in values:
128126
msg = f"{val!r} is not a valid value for {key}"
@@ -131,10 +129,10 @@ def check_in_list(_values, *, _print_supported_values=True, **kwargs):
131129
raise ValueError(msg)
132130

133131

134-
def check_shape(_shape, **kwargs):
132+
def check_shape(shape, /, **kwargs):
135133
"""
136-
For each *key, value* pair in *kwargs*, check that *value* has the shape
137-
*_shape*, if not, raise an appropriate ValueError.
134+
For each *key, value* pair in *kwargs*, check that *value* has the shape *shape*;
135+
if not, raise an appropriate ValueError.
138136
139137
*None* in the shape is treated as a "free" size that can have any length.
140138
e.g. (None, 2) -> (N, 2)
@@ -147,42 +145,37 @@ def check_shape(_shape, **kwargs):
147145
148146
>>> _api.check_shape((None, 2), arg=arg, other_arg=other_arg)
149147
"""
150-
target_shape = _shape
151148
for k, v in kwargs.items():
152149
data_shape = v.shape
153150

154-
if len(target_shape) != len(data_shape) or any(
155-
t not in [s, None]
156-
for t, s in zip(target_shape, data_shape)
157-
):
151+
if (len(data_shape) != len(shape)
152+
or any(s != t and t is not None for s, t in zip(data_shape, shape))):
158153
dim_labels = iter(itertools.chain(
159154
'MNLIJKLH',
160155
(f"D{i}" for i in itertools.count())))
161156
text_shape = ", ".join(str(n)
162157
if n is not None
163158
else next(dim_labels)
164-
for n in target_shape)
165-
if len(target_shape) == 1:
159+
for n in shape)
160+
if len(shape) == 1:
166161
text_shape += ","
167162

168163
raise ValueError(
169-
f"{k!r} must be {len(target_shape)}D "
170-
f"with shape ({text_shape}). "
171-
f"Your input has shape {v.shape}."
164+
f"{k!r} must be {len(shape)}D with shape ({text_shape}), "
165+
f"but your input has shape {v.shape}"
172166
)
173167

174168

175-
def check_getitem(_mapping, **kwargs):
169+
def check_getitem(mapping, /, **kwargs):
176170
"""
177171
*kwargs* must consist of a single *key, value* pair. If *key* is in
178-
*_mapping*, return ``_mapping[value]``; else, raise an appropriate
172+
*mapping*, return ``mapping[value]``; else, raise an appropriate
179173
ValueError.
180174
181175
Examples
182176
--------
183177
>>> _api.check_getitem({"foo": "bar"}, arg=arg)
184178
"""
185-
mapping = _mapping
186179
if len(kwargs) != 1:
187180
raise ValueError("check_getitem takes a single keyword argument")
188181
(k, v), = kwargs.items()

0 commit comments

Comments
 (0)