Repository navigation
Commit 12a29cb
committed
refactor: remove
The `inspect` module (CPython) relies on structural checking to support non-native function objects (in particular, any user-defined function can be "cythonized", and for the resulting object, `inspect.isfunction()` will return `False`), and the same was used in `_isfunctionlike()`. Essentially, this is a check that the function is introspectable, as also indicated in python/cpython#131983. But should we really follow this approach?
One of the main disadvantages of structural checks is that they are slow. Moreover, `_isfunctionlike()` also checks attribute types for compliance with "native" types (types from the `types` module), which is a rather narrow approach: will every possible introspectable function really provide `__code__` of a native type? And that is not to mention how ambiguous the checks themselves are:
1. They do not check the complete set of introspectable attributes (and it is quite possible that some implementations will not even provide all introspectable attributes, as can be seen in cython/cython#7448).
2. Even an incomplete set of attributes is redundant when we only need `__code__`, and it prevents support for partially introspectable objects.
It follows that "function-like" objects do not actually exist, which means that it is impossible to define a protocol for them.
The answer to how we should deal with non-native but introspectable objects can be found in the `functools.wraps()` implementation. It knows nothing about object types. We can wrap any callable object, and all known introspectable attributes will be assigned to the wrapper, even if the former is not a user-defined function. We can say that it assumes that the attribute values are consistent with the behavior of the object (and the meaning of the attributes themselves is already specified by their special names). And so, just the existence of the `__code__` attribute is enough to consider its flags relevant to the call result.
If the assumption is violated, we will consider it a user error._isfunctionlike()
1 parent d51a225 commit 12a29cb
3 files changed
Lines changed: 12 additions & 73 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
| |||
32 | 30 | | |
33 | 31 | | |
34 | 32 | | |
35 | | - | |
36 | | - | |
| 33 | + | |
37 | 34 | | |
38 | 35 | | |
39 | 36 | | |
| |||
73 | 70 | | |
74 | 71 | | |
75 | 72 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 73 | | |
82 | 74 | | |
83 | 75 | | |
| |||
94 | 86 | | |
95 | 87 | | |
96 | 88 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | 89 | | |
108 | 90 | | |
109 | 91 | | |
| |||
413 | 395 | | |
414 | 396 | | |
415 | 397 | | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | 398 | | |
439 | 399 | | |
440 | 400 | | |
| |||
480 | 440 | | |
481 | 441 | | |
482 | 442 | | |
483 | | - | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
484 | 449 | | |
485 | 450 | | |
486 | 451 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
| 9 | + | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | 41 | | |
47 | 42 | | |
48 | 43 | | |
| |||
58 | 53 | | |
59 | 54 | | |
60 | 55 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | 56 | | |
71 | 57 | | |
72 | 58 | | |
| |||
109 | 95 | | |
110 | 96 | | |
111 | 97 | | |
112 | | - | |
113 | 98 | | |
114 | 99 | | |
115 | 100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 22 | + | |
28 | 23 | | |
29 | 24 | | |
30 | 25 | | |
| |||
432 | 427 | | |
433 | 428 | | |
434 | 429 | | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
| 430 | + | |
439 | 431 | | |
440 | 432 | | |
441 | 433 | | |
| |||
507 | 499 | | |
508 | 500 | | |
509 | 501 | | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
| 502 | + | |
514 | 503 | | |
515 | 504 | | |
516 | 505 | | |
| |||
0 commit comments