Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2eeffe

Browse files
authored
[3.10] bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156). (#31175)
(cherry picked from commit 77b025b) Co-authored-by: Gregory Beauregard <[email protected]>
1 parent c1ff4cb commit e2eeffe

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Lib/test/test_typing.py

+5
Original file line numberDiff line numberDiff line change
@@ -4768,6 +4768,11 @@ def test_no_isinstance(self):
47684768
with self.assertRaises(TypeError):
47694769
isinstance(42, TypeAlias)
47704770

4771+
def test_stringized_usage(self):
4772+
class A:
4773+
a: "TypeAlias"
4774+
self.assertEqual(get_type_hints(A), {'a': TypeAlias})
4775+
47714776
def test_no_issubclass(self):
47724777
with self.assertRaises(TypeError):
47734778
issubclass(Employee, TypeAlias)

Lib/typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
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")
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)