10000 bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156) · python/cpython@77b025b · GitHub
[go: up one dir, main page]

Skip to content

Commit 77b025b

Browse files
authored
bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156)
1 parent 06b8f16 commit 77b025b

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
@@ -4866,6 +4866,11 @@ def test_no_isinstance(self):
48664866
with self.assertRaises(TypeError):
48674867
isinstance(42, TypeAlias)
48684868

4869+
def test_stringized_usage(self):
4870+
class A:
4871+
a: "TypeAlias"
4872+
self.assertEqual(get_type_hints(A), {'a': TypeAlias})
4873+
48694874
def test_no_issubclass(self):
48704875
with self.assertRaises(TypeError):
48714876
issubclass(Employee, TypeAlias)

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
174174
if (isinstance(arg, _GenericAlias) and
175175
arg.__origin__ in invalid_generic_forms):
176176
raise TypeError(f"{arg} is not valid as type argument")
177-
if arg in (Any, NoReturn, ClassVar, Final):
177+
if arg in (Any, NoReturn, ClassVar, Final, TypeAlias):
178178
return arg
179179
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
180180
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)
0