type_params = getattr(owner, "__type_params__", None)
# Type parameters exist in their own scope, which is logically
- # between the locals and the globals.
- type_param_scope = {}
+ # between the locals and the globals. We simulate this by adding
+ # them to the globals.
if type_params is not None:
+ globals = dict(globals)
for param in type_params:
- type_param_scope[param.__name__] = param
-
+ globals[param.__name__] = param
if self.__extra_names__:
locals = {**locals, **self.__extra_names__}
if arg.isidentifier() and not keyword.iskeyword(arg):
if arg in locals:
return locals[arg]
- elif arg in type_param_scope:
- return type_param_scope[arg]
elif arg in globals:
return globals[arg]
elif hasattr(builtins, arg):
else:
code = self.__forward_code__
try:
- return eval(code, globals=globals, locals={**type_param_scope, **locals})
+ return eval(code, globals=globals, locals=locals)
except Exception:
if not is_forwardref_format:
raise
# All variables, in scoping order, should be checked before
# triggering __missing__ to create a _Stringifier.
new_locals = _StringifierDict(
- {**builtins.__dict__, **globals, **type_param_scope, **locals},
+ {**builtins.__dict__, **globals, **locals},
globals=globals,
owner=owner,
is_class=self.__forward_is_class__,
with self.assertRaises(SyntaxError):
fr.evaluate()
- def test_re_evaluate_generics(self):
- global alias
- class C:
- x: alias[int]
-
- evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
- alias = list
- self.assertEqual(evaluated.evaluate(), list[int])
-
class TestAnnotationLib(unittest.TestCase):
def test__all__(self):