]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.10] bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156). (#31175)
authorGregory Beauregard <greg@greg.red>
Mon, 7 Feb 2022 16:21:56 +0000 (08:21 -0800)
committerGitHub <noreply@github.com>
Mon, 7 Feb 2022 16:21:56 +0000 (08:21 -0800)
(cherry picked from commit 77b025be4a4cd5a3bfc1b1af560cc57e8e956c98)

Co-authored-by: Gregory Beauregard <greg@greg.red>
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 f13541e10d58c9a2f08efd77af5851efbc4fcf1f..341be1dd031ab17429775025cb747bdad3cc5a7f 100644 (file)
@@ -4768,6 +4768,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 abd5899806f290dfccedf9ce623bc946a9146030..7743c7f76bde8fb6769bd8ce0a36910d668245f8 100644 (file)
@@ -165,7 +165,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, Final):
+    if arg in (Any, NoReturn, 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