@@ -58,22 +58,20 @@ def fget(self):
58
58
return self ._fget
59
59
60
60
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.
64
63
65
- def check_isinstance (_types , ** kwargs ):
64
+ def check_isinstance (types , / , ** kwargs ):
66
65
"""
67
66
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.
69
68
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.
71
70
72
71
Examples
73
72
--------
74
73
>>> _api.check_isinstance((SomeClass, None), arg=arg)
75
74
"""
76
- types = _types
77
75
none_type = type (None )
78
76
types = ((types ,) if isinstance (types , type ) else
79
77
(none_type ,) if types is None else
@@ -98,31 +96,31 @@ def type_name(tp):
98
96
type_name (type (v ))))
99
97
100
98
101
- def check_in_list (_values , * , _print_supported_values = True , ** kwargs ):
99
+ def check_in_list (values , / , * , _print_supported_values = True , ** kwargs ):
102
100
"""
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.
104
103
105
104
Parameters
106
105
----------
107
- _values : iterable
106
+ values : iterable
108
107
Sequence of values to check on.
109
108
_print_supported_values : bool, default: True
110
- Whether to print *_values * when raising ValueError.
109
+ Whether to print *values * when raising ValueError.
111
110
**kwargs : dict
112
- *key, value* pairs as keyword arguments to find in *_values *.
111
+ *key, value* pairs as keyword arguments to find in *values *.
113
112
114
113
Raises
115
114
------
116
115
ValueError
117
- If any *value* in *kwargs* is not found in *_values *.
116
+ If any *value* in *kwargs* is not found in *values *.
118
117
119
118
Examples
120
119
--------
121
120
>>> _api.check_in_list(["foo", "bar"], arg=arg, other_arg=other_arg)
122
121
"""
123
122
if not kwargs :
124
123
raise TypeError ("No argument to check!" )
125
- values = _values
126
124
for key , val in kwargs .items ():
127
125
if val not in values :
128
126
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):
131
129
raise ValueError (msg )
132
130
133
131
134
- def check_shape (_shape , ** kwargs ):
132
+ def check_shape (shape , / , ** kwargs ):
135
133
"""
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.
138
136
139
137
*None* in the shape is treated as a "free" size that can have any length.
140
138
e.g. (None, 2) -> (N, 2)
@@ -147,42 +145,37 @@ def check_shape(_shape, **kwargs):
147
145
148
146
>>> _api.check_shape((None, 2), arg=arg, other_arg=other_arg)
149
147
"""
150
- target_shape = _shape
151
148
for k , v in kwargs .items ():
152
149
data_shape = v .shape
153
150
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 ))):
158
153
dim_labels = iter (itertools .chain (
159
154
'MNLIJKLH' ,
160
155
(f"D{ i } " for i in itertools .count ())))
161
156
text_shape = ", " .join (str (n )
162
157
if n is not None
163
158
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 :
166
161
text_shape += ","
167
162
168
163
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 } "
172
166
)
173
167
174
168
175
- def check_getitem (_mapping , ** kwargs ):
169
+ def check_getitem (mapping , / , ** kwargs ):
176
170
"""
177
171
*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
179
173
ValueError.
180
174
181
175
Examples
182
176
--------
183
177
>>> _api.check_getitem({"foo": "bar"}, arg=arg)
184
178
"""
185
- mapping = _mapping
186
179
if len (kwargs ) != 1 :
187
180
raise ValueError ("check_getitem takes a single keyword argument" )
188
181
(k , v ), = kwargs .items ()
0 commit comments