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

This commit is contained in:
Alex Waygood 2024-06-13 22:16:40 +01:00 committed by GitHub
parent d88a1f2e15
commit 42351c3b9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 186 additions and 1 deletions

View file

@ -274,7 +274,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)