forked from pytoolz/toolz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_inspect_args.py
487 lines (402 loc) · 15.6 KB
/
test_inspect_args.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import functools
import inspect
import itertools
import operator
import toolz
from toolz.functoolz import (curry, is_valid_args, is_partial_args, is_arity,
num_required_args, has_varargs, has_keywords)
from toolz._signatures import builtins
import toolz._signatures as _sigs
from toolz.utils import raises
def make_func(param_string, raise_if_called=True):
if not param_string.startswith('('):
param_string = '(%s)' % param_string
if raise_if_called:
body = 'raise ValueError("function should not be called")'
else:
body = 'return True'
d = {}
exec('def func%s:\n %s' % (param_string, body), globals(), d)
return d['func']
def test_make_func():
f = make_func('')
assert raises(ValueError, lambda: f())
assert raises(TypeError, lambda: f(1))
f = make_func('', raise_if_called=False)
assert f()
assert raises(TypeError, lambda: f(1))
f = make_func('x, y=1', raise_if_called=False)
assert f(1)
assert f(x=1)
assert f(1, 2)
assert f(x=1, y=2)
assert raises(TypeError, lambda: f(1, 2, 3))
f = make_func('(x, y=1)', raise_if_called=False)
assert f(1)
assert f(x=1)
assert f(1, 2)
assert f(x=1, y=2)
assert raises(TypeError, lambda: f(1, 2, 3))
def test_is_valid(check_valid=is_valid_args, incomplete=False):
orig_check_valid = check_valid
check_valid = lambda func, *args, **kwargs: orig_check_valid(func, args, kwargs)
f = make_func('')
assert check_valid(f)
assert check_valid(f, 1) is False
assert check_valid(f, x=1) is False
f = make_func('x')
assert check_valid(f) is incomplete
assert check_valid(f, 1)
assert check_valid(f, x=1)
assert check_valid(f, 1, x=2) is False
assert check_valid(f, 1, y=2) is False
assert check_valid(f, 1, 2) is False
assert check_valid(f, x=1, y=2) is False
f = make_func('x=1')
assert check_valid(f)
assert check_valid(f, 1)
assert check_valid(f, x=1)
assert check_valid(f, 1, x=2) is False
assert check_valid(f, 1, y=2) is False
assert check_valid(f, 1, 2) is False
assert check_valid(f, x=1, y=2) is False
f = make_func('*args')
assert check_valid(f)
assert check_valid(f, 1)
assert check_valid(f, 1, 2)
assert check_valid(f, x=1) is False
f = make_func('**kwargs')
assert check_valid(f)
assert check_valid(f, x=1)
assert check_valid(f, x=1, y=2)
assert check_valid(f, 1) is False
f = make_func('x, *args')
assert check_valid(f) is incomplete
assert check_valid(f, 1)
assert check_valid(f, 1, 2)
assert check_valid(f, x=1)
assert check_valid(f, 1, x=1) is False
assert check_valid(f, 1, y=1) is False
f = make_func('x, y=1, **kwargs')
assert check_valid(f) is incomplete
assert check_valid(f, 1)
assert check_valid(f, x=1)
assert check_valid(f, 1, 2)
assert check_valid(f, x=1, y=2, z=3)
assert check_valid(f, 1, 2, y=3) is False
f = make_func('a, b, c=3, d=4')
assert check_valid(f) is incomplete
assert check_valid(f, 1) is incomplete
assert check_valid(f, 1, 2)
assert check_valid(f, 1, c=3) is incomplete
assert check_valid(f, 1, e=3) is False
assert check_valid(f, 1, 2, e=3) is False
assert check_valid(f, 1, 2, b=3) is False
assert check_valid(1) is False
def test_is_valid_py3(check_valid=is_valid_args, incomplete=False):
orig_check_valid = check_valid
check_valid = lambda func, *args, **kwargs: orig_check_valid(func, args, kwargs)
f = make_func('x, *, y=1')
assert check_valid(f) is incomplete
assert check_valid(f, 1)
assert check_valid(f, x=1)
assert check_valid(f, 1, y=2)
assert check_valid(f, 1, 2) is False
assert check_valid(f, 1, z=2) is False
f = make_func('x, *args, y=1')
assert check_valid(f) is incomplete
assert check_valid(f, 1)
assert check_valid(f, x=1)
assert check_valid(f, 1, y=2)
assert check_valid(f, 1, 2, y=2)
assert check_valid(f, 1, 2)
assert check_valid(f, 1, z=2) is False
f = make_func('*, y=1')
assert check_valid(f)
assert check_valid(f, 1) is False
assert check_valid(f, y=1)
assert check_valid(f, z=1) is False
f = make_func('x, *, y')
assert check_valid(f) is incomplete
assert check_valid(f, 1) is incomplete
assert check_valid(f, x=1) is incomplete
assert check_valid(f, 1, y=2)
assert check_valid(f, x=1, y=2)
assert check_valid(f, 1, 2) is False
assert check_valid(f, 1, z=2) is False
assert check_valid(f, 1, y=1, z=2) is False
f = make_func('x=1, *, y, z=3')
assert check_valid(f) is incomplete
assert check_valid(f, 1, z=3) is incomplete
assert check_valid(f, y=2)
assert check_valid(f, 1, y=2)
assert check_valid(f, x=1, y=2)
assert check_valid(f, x=1, y=2, z=3)
assert check_valid(f, 1, x=1, y=2) is False
assert check_valid(f, 1, 3, y=2) is False
f = make_func('w, x=2, *args, y, z=4')
assert check_valid(f) is incomplete
assert check_valid(f, 1) is incomplete
assert check_valid(f, 1, y=3)
f = make_func('a, b, c=3, d=4, *args, e=5, f=6, g, h')
assert check_valid(f) is incomplete
assert check_valid(f, 1) is incomplete
assert check_valid(f, 1, 2) is incomplete
assert check_valid(f, 1, 2, g=7) is incomplete
assert check_valid(f, 1, 2, g=7, h=8)
assert check_valid(f, 1, 2, 3, 4, 5, 6, 7, 8, 9) is incomplete
f = make_func('a: int, b: float')
assert check_valid(f) is incomplete
assert check_valid(f, 1) is incomplete
assert check_valid(f, b=1) is incomplete
assert check_valid(f, 1, 2)
f = make_func('(a: int, b: float) -> float')
assert check_valid(f) is incomplete
assert check_valid(f, 1) is incomplete
assert check_valid(f, b=1) is incomplete
assert check_valid(f, 1, 2)
f.__signature__ = 34
assert check_valid(f) is False
class RaisesValueError(object):
def __call__(self):
pass
@property
def __signature__(self):
raise ValueError('Testing Python 3.4')
f = RaisesValueError()
assert check_valid(f) is None
def test_is_partial():
test_is_valid(check_valid=is_partial_args, incomplete=True)
test_is_valid_py3(check_valid=is_partial_args, incomplete=True)
def test_is_valid_curry():
def check_curry(func, args, kwargs, incomplete=True):
try:
curry(func)(*args, **kwargs)
curry(func, *args)(**kwargs)
curry(func, **kwargs)(*args)
curry(func, *args, **kwargs)()
if not isinstance(func, type(lambda: None)):
return None
return incomplete
except ValueError:
return True
except TypeError:
return False
check_valid = functools.partial(check_curry, incomplete=True)
test_is_valid(check_valid=check_valid, incomplete=True)
test_is_valid_py3(check_valid=check_valid, incomplete=True)
check_valid = functools.partial(check_curry, incomplete=False)
test_is_valid(check_valid=check_valid, incomplete=False)
test_is_valid_py3(check_valid=check_valid, incomplete=False)
def test_func_keyword():
def f(func=None):
pass
assert is_valid_args(f, (), {})
assert is_valid_args(f, (None,), {})
assert is_valid_args(f, (), {'func': None})
assert is_valid_args(f, (None,), {'func': None}) is False
assert is_partial_args(f, (), {})
assert is_partial_args(f, (None,), {})
assert is_partial_args(f, (), {'func': None})
assert is_partial_args(f, (None,), {'func': None}) is False
def test_has_unknown_args():
assert has_varargs(1) is False
assert has_varargs(map)
assert has_varargs(make_func('')) is False
assert has_varargs(make_func('x, y, z')) is False
assert has_varargs(make_func('*args'))
assert has_varargs(make_func('**kwargs')) is False
assert has_varargs(make_func('x, y, *args, **kwargs'))
assert has_varargs(make_func('x, y, z=1')) is False
assert has_varargs(make_func('x, y, z=1, **kwargs')) is False
f = make_func('*args')
f.__signature__ = 34
assert has_varargs(f) is False
class RaisesValueError(object):
def __call__(self):
pass
@property
def __signature__(self):
raise ValueError('Testing Python 3.4')
f = RaisesValueError()
assert has_varargs(f) is None
def test_num_required_args():
assert num_required_args(lambda: None) == 0
assert num_required_args(lambda x: None) == 1
assert num_required_args(lambda x, *args: None) == 1
assert num_required_args(lambda x, **kwargs: None) == 1
assert num_required_args(lambda x, y, *args, **kwargs: None) == 2
assert num_required_args(map) == 2
assert num_required_args(dict) is None
def test_has_keywords():
assert has_keywords(lambda: None) is False
assert has_keywords(lambda x: None) is False
assert has_keywords(lambda x=1: None)
assert has_keywords(lambda **kwargs: None)
assert has_keywords(int)
assert has_keywords(sorted)
assert has_keywords(max)
assert has_keywords(map) is False
assert has_keywords(bytearray) is None
def test_has_varargs():
assert has_varargs(lambda: None) is False
assert has_varargs(lambda *args: None)
assert has_varargs(lambda **kwargs: None) is False
assert has_varargs(map)
assert has_varargs(max) is None
def test_is_arity():
assert is_arity(0, lambda: None)
assert is_arity(1, lambda: None) is False
assert is_arity(1, lambda x: None)
assert is_arity(3, lambda x, y, z: None)
assert is_arity(1, lambda x, *args: None) is False
assert is_arity(1, lambda x, **kwargs: None) is False
assert is_arity(1, all)
assert is_arity(2, map) is False
assert is_arity(2, range) is None
def test_introspect_curry_valid_py3(check_valid=is_valid_args, incomplete=False):
orig_check_valid = check_valid
check_valid = lambda _func, *args, **kwargs: orig_check_valid(_func, args, kwargs)
f = toolz.curry(make_func('x, y, z=0'))
assert check_valid(f)
assert check_valid(f, 1)
assert check_valid(f, 1, 2)
assert check_valid(f, 1, 2, 3)
assert check_valid(f, 1, 2, 3, 4) is False
assert check_valid(f, invalid_keyword=True) is False
assert check_valid(f(1))
assert check_valid(f(1), 2)
assert check_valid(f(1), 2, 3)
assert check_valid(f(1), 2, 3, 4) is False
assert check_valid(f(1), x=2) is False
assert check_valid(f(1), y=2)
assert check_valid(f(x=1), 2) is False
assert check_valid(f(x=1), y=2)
assert check_valid(f(y=2), 1)
assert check_valid(f(y=2), 1, z=3)
assert check_valid(f(y=2), 1, 3) is False
f = toolz.curry(make_func('x, y, z=0'), 1, x=1)
assert check_valid(f) is False
assert check_valid(f, z=3) is False
f = toolz.curry(make_func('x, y, *args, z'))
assert check_valid(f)
assert check_valid(f, 0)
assert check_valid(f(1), 0)
assert check_valid(f(1, 2), 0)
assert check_valid(f(1, 2, 3), 0)
assert check_valid(f(1, 2, 3, 4), 0)
assert check_valid(f(1, 2, 3, 4), z=4)
assert check_valid(f(x=1))
assert check_valid(f(x=1), 1) is False
assert check_valid(f(x=1), y=2)
def test_introspect_curry_partial_py3():
test_introspect_curry_valid_py3(check_valid=is_partial_args, incomplete=True)
def test_introspect_curry_py3():
f = toolz.curry(make_func(''))
assert num_required_args(f) == 0
assert is_arity(0, f)
assert has_varargs(f) is False
assert has_keywords(f) is False
f = toolz.curry(make_func('x'))
assert num_required_args(f) == 0
assert is_arity(0, f) is False
assert is_arity(1, f) is False
assert has_varargs(f) is False
assert has_keywords(f) # A side-effect of being curried
f = toolz.curry(make_func('x, y, z=0'))
assert num_required_args(f) == 0
assert is_arity(0, f) is False
assert is_arity(1, f) is False
assert is_arity(2, f) is False
assert is_arity(3, f) is False
assert has_varargs(f) is False
assert has_keywords(f)
f = toolz.curry(make_func('*args, **kwargs'))
assert num_required_args(f) == 0
assert has_varargs(f)
assert has_keywords(f)
def test_introspect_builtin_modules():
mods = [builtins, functools, itertools, operator, toolz,
toolz.functoolz, toolz.itertoolz, toolz.dicttoolz, toolz.recipes]
denylist = set()
def add_denylist(mod, attr):
if hasattr(mod, attr):
denylist.add(getattr(mod, attr))
add_denylist(builtins, 'basestring')
add_denylist(builtins, 'NoneType')
add_denylist(builtins, '__metaclass__')
add_denylist(builtins, 'sequenceiterator')
def is_missing(modname, name, func):
if name.startswith('_') and not name.startswith('__'):
return False
if name.startswith('__pyx_unpickle_') or name.endswith('_cython__'):
return False
try:
if issubclass(func, BaseException):
return False
except TypeError:
pass
try:
return (callable(func)
and func.__module__ is not None
and modname in func.__module__
and is_partial_args(func, (), {}) is not True
and func not in denylist)
except AttributeError:
return False
missing = {}
for mod in mods:
modname = mod.__name__
for name, func in vars(mod).items():
if is_missing(modname, name, func):
if modname not in missing:
missing[modname] = []
missing[modname].append(name)
if missing:
messages = []
for modname, names in sorted(missing.items()):
msg = '{}:\n {}'.format(modname, '\n '.join(sorted(names)))
messages.append(msg)
message = 'Missing introspection for the following callables:\n\n'
raise AssertionError(message + '\n\n'.join(messages))
def test_inspect_signature_property():
# By adding AddX to our signature registry, we can inspect the class
# itself and objects of the class. `inspect.signature` doesn't like
# it when `obj.__signature__` is a property.
class AddX(object):
def __init__(self, func):
self.func = func
def __call__(self, addx, *args, **kwargs):
return addx + self.func(*args, **kwargs)
@property
def __signature__(self):
sig = inspect.signature(self.func)
params = list(sig.parameters.values())
kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
newparam = inspect.Parameter('addx', kind)
params = [newparam] + params
return sig.replace(parameters=params)
addx = AddX(lambda x: x)
sig = inspect.signature(addx)
assert sig == inspect.Signature(parameters=[
inspect.Parameter('addx', inspect.Parameter.POSITIONAL_OR_KEYWORD),
inspect.Parameter('x', inspect.Parameter.POSITIONAL_OR_KEYWORD)])
assert num_required_args(AddX) is False
_sigs.signatures[AddX] = (_sigs.expand_sig((0, lambda func: None)),)
assert num_required_args(AddX) == 1
del _sigs.signatures[AddX]
def test_inspect_wrapped_property():
class Wrapped(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)
@property
def __wrapped__(self):
return self.func
func = lambda x: x
wrapped = Wrapped(func)
assert inspect.signature(func) == inspect.signature(wrapped)
assert num_required_args(Wrapped) is None
_sigs.signatures[Wrapped] = (_sigs.expand_sig((0, lambda func: None)),)
assert num_required_args(Wrapped) == 1