self.assertEqual(get_type_hints(foo, globals(), locals()),
{'a': Callable[..., T]})
+ def test_special_forms_forward(self):
+
+ class C:
+ a: Annotated['ClassVar[int]', (3, 5)] = 4
+ b: Annotated['Final[int]', "const"] = 4
+
+ class CF:
+ b: List['Final[int]'] = 4
+
+ self.assertEqual(get_type_hints(C, globals())['a'], ClassVar[int])
+ self.assertEqual(get_type_hints(C, globals())['b'], Final[int])
+ with self.assertRaises(TypeError):
+ get_type_hints(CF, globals()),
+
def test_syntax_error(self):
with self.assertRaises(SyntaxError):
# legitimate imports of those modules.
-def _type_convert(arg, module=None):
+def _type_convert(arg, module=None, *, allow_special_forms=False):
"""For converting None to type(None), and strings to ForwardRef."""
if arg is None:
return type(None)
if isinstance(arg, str):
- return ForwardRef(arg, module=module)
+ return ForwardRef(arg, module=module, is_class=allow_special_forms)
return arg
if is_argument:
invalid_generic_forms += (Final,)
- arg = _type_convert(arg, module=module)
+ arg = _type_convert(arg, module=module, allow_special_forms=allow_special_forms)
if (isinstance(arg, _GenericAlias) and
arg.__origin__ in invalid_generic_forms):
raise TypeError(f"{arg} is not valid as type argument")