-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-141388: Fully support non-function callables as annotate functions #141449
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
… implement in fwdref format
…ion(format=Format.STRING)`
dr-carlos
left a comment
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.
Current status: overall, there is a decent amount of special-casing going on to fully support various callable types. But at this point, it supports most stdlib callables (at least, the ones, I could think of), except for C functions which don't have __code__ or __globals__ so can never work with Forwardref/_Stringifier. Some better error messages would be good for those types. More testing is definitely necessary to ensure full support.
| if call_func := getattr(annotate.__call__, "__func__", None): | ||
| return getattr(call_func, attr, default) | ||
| elif isinstance(annotate, type): | ||
| return getattr(annotate.__init__, attr, default) |
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 means that only the __init__ method is used for fake global namespaces when a class is used as an annotate function. Maybe the __new__ method should also count? Just adds a bit more complexity.
Lib/annotationlib.py
Outdated
|
|
||
| return default | ||
|
|
||
| def _direct_call_annotate(func, annotate, format): |
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 feels like a poorly named function. Need some way tof describing that we're calling the full annotate function (annotate) based on the part of it that we compiled previously (func) with the given args (usually the format, unless we're recursively calling).
…on function class' __call__ attribute
…et don't raise an error when used as annotate functions
|
I've decided to take this PR out of draft mode as the tests are now pretty comprehensive and every stdlib callable I could think of is now supported where possible.
|
__annotate__function in 3.14, butannotationlibwill start throwing errors if the format is not implemented.call_annotate_function()__code__,__globals__, etc., so aForwardRefor stringification using the current techniques is impossible. Let me know if there are any more callables to implement, some more special-casing will probably be required.annotationlib, but tests are mostly present oncall_annotate_function()for now.__annotate__Functions don't actually need to be functions #141388