Commit 23d73be
authored
Fix callable detection to support functools.partial and all callable objects (#3121)
* Fix callable detection to support all callable objects (functools.partial, etc)
Fixes #2008
The old implementation checked for __code__ attribute directly, which
doesn't exist for callable objects like functools.partial. This caused
an AttributeError when trying to use such objects with find_max_global
and find_min_global functions.
Changed to use Python's inspect.signature() which properly handles all
callable objects including:
- Regular functions
- Lambda functions
- functools.partial objects
- Class methods
- Any object with __call__ method
The fix includes a fallback to the old __code__ method for backward
compatibility in case inspect.signature fails for any reason.
* Update the code to loop through params.values() instead of params itself, since parameters is a dictionary-like object. The earlier code tried to access .second on the iterator, which isn’t valid when working with dict values
Signed-off-by: Samaresh Kumar Singh <[email protected]>
* Fix parameter counting to exclude *args and **kwargs
The previous code was counting VAR_POSITIONAL (*args) and VAR_KEYWORD
(**kwargs) parameters in the total count, which caused functions like
'lambda a, b, c, *args' to be counted as having 4 parameters instead
of 3. Now only regular parameters are counted, while still checking
for the presence of *args to allow variable argument functions.
Also removed test_callable_fix.py as it was causing pytest errors.
Signed-off-by: Samaresh Kumar Singh <[email protected]>
---------
Signed-off-by: Samaresh Kumar Singh <[email protected]>1 parent 20b2172 commit 23d73be
1 file changed
+39
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 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 | + | |
56 | 90 | | |
57 | 91 | | |
58 | 92 | | |
| |||
0 commit comments