mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
[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:
parent
d5ad3b7fc8
commit
d4174fa7ca
4 changed files with 186 additions and 1 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue