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
bpo-43817: Add inspect.get_annotations(). #25522
Merged
Merged
Conversation
This file contains 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
inspect.get_annotations(o) safely retrieves and return the annotations from o. It handles un-stringizing stringized annotations, it works around the quirks of annotations on various objects, and it is super-safe. inspect.signature() now calls inspect.get_annotations() to get the annotations for an object.
Thank goodness!
gvanrossum
reviewed
Apr 24, 2021
JelleZijlstra
reviewed
Apr 24, 2021
Fidget-Spinner
reviewed
Apr 24, 2021
Misc/NEWS.d/next/Library/2021-04-22-04-12-13.bpo-43817.FQ-XlH.rst
Outdated
Show resolved
Hide resolved
Thanks, Fidget_Spinner!
If an explicit locals dict is not passed in, get_annotations(cls) will now use the cls namespace as locals by default.
inspect.get_annotations() and inspect.signature() will now only call eval() on string values by explicit user request. Calling eval() by default is too opinionated for the standard library. Neither function catches exceptions, so if the eval() raises an exception, it'll unwind up to the caller. Also, removed the special eval_str=ONLY_IF_STRINGIZED logic entirely. The user now tells us explicitly whether or not to call eval() on strings--so we don't need the heuristic for anything.
inspect.signature() and inspect.Signature_from_callable() both call inspect.get_annotations() to compute the annotations. Both functions also pass through their globals, locals, and eval_str arguments. This commit adds some tests just to ensure that the values are passed through correctly and everything is working. And who doesn't feel more secure with some extra tests!
JelleZijlstra
reviewed
Apr 29, 2021
__annotations__ is allowed to be None in some circumstances, and there is explicit code to handle it in the inspect module. But I introduced a bug ("isinstance(o, None)"), and this showed the code path wasn't being executed anyway, so now there's a bugfix and a test too.
JelleZijlstra
approved these changes
Apr 30, 2021
@larryhastings: Please replace |
kreathon
pushed a commit
to kreathon/cpython
that referenced
this issue
May 2, 2021
Add inspect.get_annotations, which safely computes the annotations defined on an object. It works around the quirks of accessing the annotations from various types of objects, and makes very few assumptions about the object passed in. inspect.get_annotations can also correctly un-stringize stringized annotations. inspect.signature, inspect.from_callable, and inspect.from_function now call inspect.get_annotations to retrieve annotations. This means inspect.signature and inspect.from_callable can now un-stringize stringized annotations, too.
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.
inspect.get_annotations(o)
safely retrieves and returnthe annotations from
o
. It handles un-stringizingstringized annotations, it works around the quirks of
annotations on various objects, and it is super-safe.
inspect.signature()
now callsinspect.get_annotations()
to get the annotations for an object.
https://bugs.python.org/issue43817