Fix callable detection to support functools.partial and all callable objects#3121
Merged
davisking merged 3 commits intoNov 22, 2025
Merged
Conversation
…tial, etc) Fixes davisking#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.
Contributor
Author
|
I ran into this issue while using dlib for optimization in one of my projects. I was trying to use After digging into the source code, I realized the issue was with how callable detection was implemented. Since Hope this helps others who might run into the same issue |
…elf, 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 <ssam3003@gmail.com>
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 <ssam3003@gmail.com>
Owner
|
Nice, thanks for the PR :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2008
Problem
The
find_max_globalandfind_min_globalfunctions failed with anAttributeErrorwhen used withfunctools.partialobjects and other callable objects that don't have a__code__attribute.Example of the issue:
Solution
Changed the
num_function_argumentsfunction to use Python'sinspect.signature()which properly works with all callable objects:functools.partialobjects__call__methodThe implementation includes a fallback to the original
__code__method for backward compatibility ifinspect.signature()fails for any reason.Testing
I've included a test script (
test_callable_fix.py) that verifies the fix works with:functools.partialobjectsLet me know if you'd like me to add any additional tests or make any changes