Skip to content

Commit bb90f02

Browse files
committed
[3.10] bpo-46655: allow stringized TypeAlias with get_type_hints (pythonGH-31156).
(cherry picked from commit 77b025b) Co-authored-by: Gregory Beauregard <[email protected]>
1 parent eaeb994 commit bb90f02

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Lib/test/test_typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4693,6 +4693,11 @@ def test_no_isinstance(self):
46934693
with self.assertRaises(TypeError):
46944694
isinstance(42, TypeAlias)
46954695

4696+
def test_stringized_usage(self):
4697+
class A:
4698+
a: "TypeAlias"
4699+
self.assertEqual(get_type_hints(A), {'a': TypeAlias})
4700+
46964701
def test_no_issubclass(self):
46974702
with self.assertRaises(TypeError):
46984703
issubclass(Employee, TypeAlias)

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, is_class=False):
165165
if (isinstance(arg, _GenericAlias) and
166166
arg.__origin__ in invalid_generic_forms):
167167
raise TypeError(f"{arg} is not valid as type argument")
168-
if arg in (Any, NoReturn, Final):
168+
if arg in (Any, NoReturn, Final, TypeAlias):
169169
return arg
170170
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
171171
raise TypeError(f"Plain {arg} is not valid as type argument")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In :func:`typing.get_type_hints`, support evaluating bare stringified ``TypeAlias`` annotations. Patch by Gregory Beauregard.

0 commit comments

Comments
 (0)