[3.13] gh-114053: Fix bad interaction of PEP 695, PEP 563 and inspect.get_annotations (GH-120270) (#120474)

gh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect.get_annotations` (GH-120270)
(cherry picked from commit 42351c3b9a)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-06-13 23:41:14 +02:00 committed by GitHub
parent d5ad3b7fc8
commit d4174fa7ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 186 additions and 1 deletions

View file

@ -280,7 +280,13 @@ def get_annotations(obj, *, globals=None, locals=None, eval_str=False):
if globals is None:
globals = obj_globals
if locals is None:
locals = obj_locals
locals = obj_locals or {}
# "Inject" type parameters into the local namespace
# (unless they are shadowed by assignments *in* the local namespace),
# as a way of emulating annotation scopes when calling `eval()`
if type_params := getattr(obj, "__type_params__", ()):
locals = {param.__name__: param for param in type_params} | locals
return_value = {key:
value if not isinstance(value, str) else eval(value, globals, locals)