Skip to content
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 12 commits into from Apr 30, 2021

Conversation

Copy link
Contributor

@larryhastings larryhastings commented Apr 22, 2021

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.

https://bugs.python.org/issue43817

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.
Lib/inspect.py Show resolved Hide resolved
Lib/inspect.py Outdated Show resolved Hide resolved
Lib/inspect.py 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!
Lib/inspect.py Outdated Show resolved Hide resolved
__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.
@larryhastings larryhastings merged commit 74613a4 into python:master Apr 30, 2021
12 checks passed
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Apr 30, 2021

@larryhastings: Please replace # with GH- in the commit message next time. Thanks!

@larryhastings larryhastings deleted the get_annotations branch Apr 30, 2021
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants