-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-91002: Support functools.partial and functools.partialmethod inspect in annotationlib.get_annotations #139753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
… inspect in annotationlib.get_annotations Signed-off-by: Manjusaka <me@manjusaka.me>
>>> add_10 = partial(add, 10) | ||
>>> get_annotations(add_10) | ||
{'b': <class 'int'>, 'c': <class 'int'>, 'return': <class 'int'>} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could refer to the new section below rather that repeating the example, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this instead be implemented with a custom __annotate__
function on partial objects, so annotationlib itself doesn't need to change?
# The first parameter (self/cls) should remain, but bound args should be removed | ||
try: | ||
# Get the function signature | ||
func_sig = inspect.signature(func) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will fail if the original function's signature has annotations that do not evaluate in the VALUE format.
return new_annotations | ||
|
||
except (ValueError, TypeError): | ||
# If we can't process, return the original annotations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? If we can't get correct annotations, we ideally shouldn't return wrong ones.
May not return a fresh dictionary. | ||
""" | ||
import functools |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import is in the crucial code path for annotationlib, it doesn't make sense to make it lazy.
fr.evaluate() | ||
|
||
|
||
class TestFunctoolsPartialMethod(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests involving functools.Placeholder
.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
📚 Documentation preview 📚: https://cpython-previews--139753.org.readthedocs.build/