From 64f45a7e213e774415740c9b2c5058a8548010c5 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Sun, 6 Feb 2022 00:40:14 -0800 Subject: [PATCH 1/2] allow stringized TypeAlias with get_type_hints --- Lib/test/test_typing.py | 5 +++++ Lib/typing.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 85f74064458f2d..a68acd9247c5ad 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4854,6 +4854,11 @@ def test_no_isinstance(self): 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 0cf9755022e9b8..3822a9a4c84b10 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") From 7b425d9bced0b51f8c35138346ec9dffec6b561f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 6 Feb 2022 08:54:04 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2022-02-06-08-54-03.bpo-46655.DiLzYv.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-02-06-08-54-03.bpo-46655.DiLzYv.rst 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 00000000000000..4f0de9519a00e4 --- /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