Skip to content

Commit

Permalink
Reapply python#120272
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Jul 21, 2024
1 parent f60f435 commit 1f3a63c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,21 @@ def evaluate(self, *, globals=None, locals=None, type_params=None, owner=None):
# (unless they are shadowed by assignments *in* the local namespace),
# as a way of emulating annotation scopes when calling `eval()`
type_params = getattr(self.__owner__, "__type_params__", None)

# type parameters require some special handling,
# as they exist in their own scope
# but `eval()` does not have a dedicated parameter for that scope.
# For classes, names in type parameter scopes should override
# names in the global scope (which here are called `localns`!),
# but should in turn be overridden by names in the class scope
# (which here are called `globalns`!)
if type_params is not None:
locals = {param.__name__: param for param in type_params} | locals
globals, locals = dict(globals), dict(locals)
for param in type_params:
param_name = param.__name__
if not self.__forward_is_class__ or param_name not in globals:
globals[param_name] = param
locals.pop(param_name, None)

code = self.__forward_code__
value = eval(code, globals=globals, locals=locals)
Expand Down

0 comments on commit 1f3a63c

Please sign in to comment.