summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGregory Beauregard <greg@greg.red>2022-02-06 23:16:22 (GMT)
committerGitHub <noreply@github.com>2022-02-06 23:16:22 (GMT)
commit77b025be4a4cd5a3bfc1b1af560cc57e8e956c98 (patch)
tree3cdccfb0c42e06840f73133914b2fb1de11a95b1
parent06b8f1615b09099fae5c5393334b8716a4144d20 (diff)
downloadcpython-77b025be4a4cd5a3bfc1b1af560cc57e8e956c98.zip
cpython-77b025be4a4cd5a3bfc1b1af560cc57e8e956c98.tar.gz
cpython-77b025be4a4cd5a3bfc1b1af560cc57e8e956c98.tar.bz2
bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156)
-rw-r--r--Lib/test/test_typing.py5
-rw-r--r--Lib/typing.py2
-rw-r--r--Misc/NEWS.d/next/Library/2022-02-06-08-54-03.bpo-46655.DiLzYv.rst1
3 files changed, 7 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 14f49b0..2aee5c3 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -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)
diff --git a/Lib/typing.py b/Lib/typing.py
index e4e32b5..44c239c 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -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
index 0000000..4f0de95
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-02-06-08-54-03.bpo-46655.DiLzYv.rst
@@ -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