Skip to content

Commit

Permalink
bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156)
Browse files Browse the repository at this point in the history
  • Loading branch information
GBeauregard authored Feb 6, 2022
1 parent 06b8f16 commit 77b025b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4866,6 +4866,11 @@ def test_no_isinstance(self):
with self.assertRaises(TypeError):
isinstance(42, TypeAlias)

def test_stringized_usage(self):
class A:
a: "TypeAlias"
self.assertEqual(get_type_hints(A), {'a': TypeAlias})

def test_no_issubclass(self):
with self.assertRaises(TypeError):
issubclass(Employee, TypeAlias)
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
if (isinstance(arg, _GenericAlias) and
arg.__origin__ in invalid_generic_forms):
raise TypeError(f"{arg} is not valid as type argument")
if arg in (Any, NoReturn, ClassVar, Final):
if arg in (Any, NoReturn, ClassVar, Final, TypeAlias):
return arg
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
raise TypeError(f"Plain {arg} is not valid as type argument")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In :func:`typing.get_type_hints`, support evaluating bare stringified ``TypeAlias`` annotations. Patch by Gregory Beauregard.

0 comments on commit 77b025b

Please sign in to comment.