]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156)
authorGregory Beauregard <greg@greg.red>
Sun, 6 Feb 2022 23:16:22 +0000 (15:16 -0800)
committerGitHub <noreply@github.com>
Sun, 6 Feb 2022 23:16:22 +0000 (15:16 -0800)
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2022-02-06-08-54-03.bpo-46655.DiLzYv.rst [new file with mode: 0644]

index 14f49b01aa9f015426590688ab2fb09c609ab2c9..2aee5c33b6674386369e93cbeadde36acbcbc248 100644 (file)
@@ -4866,6 +4866,11 @@ class TypeAliasTests(BaseTestCase):
         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)
index e4e32b5b320d046ba327f21ce7fdd5c0a79acc60..44c239c43ee4e6071956bfb7755ee75843b3f630 100644 (file)
@@ -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")
diff --git a/Misc/NEWS.d/next/Library/2022-02-06-08-54-03.bpo-46655.DiLzYv.rst b/Misc/NEWS.d/next/Library/2022-02-06-08-54-03.bpo-46655.DiLzYv.rst
new file mode 100644 (file)
index 0000000..4f0de95
--- /dev/null
@@ -0,0 +1 @@
+In :func:`typing.get_type_hints`, support evaluating bare stringified ``TypeAlias`` annotations. Patch by Gregory Beauregard.
\ No newline at end of file