From dd54a57af8132eb1ac41d1f29ac0e8612539b158 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Fri, 28 Jan 2022 00:33:40 -0800 Subject: [PATCH 1/3] typing: allow bare ClassVar annotations These are used in the wild and covered by dataclasses unit tests. Several static type checkers support this pattern. Note we adjust one test from ClassVar[ClassVar] to ClassVar[ClassVar[int]] so that it still fails. This is surprising, but not a result of weirdness we're doing in this patch. This is a limitation of the current way runtime type checking works that this case is allowed when bare special forms are allowed. For example, before this patch `ClassVar[Final]` is a valid annotation despite nesting these forms being generally disallowed. Addressing this limitation could be future work. --- Lib/test/test_typing.py | 6 +++++- Lib/typing.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 3069331c2a3541..8449affd03a768 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2071,7 +2071,7 @@ def test_fail_with_bare_union(self): with self.assertRaises(TypeError): Tuple[Optional] with self.assertRaises(TypeError): - ClassVar[ClassVar] + ClassVar[ClassVar[int]] with self.assertRaises(TypeError): List[ClassVar[int]] @@ -2896,12 +2896,16 @@ def test_special_forms_forward(self): class C: a: Annotated['ClassVar[int]', (3, 5)] = 4 b: Annotated['Final[int]', "const"] = 4 + x: 'ClassVar' = 4 + y: 'Final' = 4 class CF: b: List['Final[int]'] = 4 self.assertEqual(get_type_hints(C, globals())['a'], ClassVar[int]) self.assertEqual(get_type_hints(C, globals())['b'], Final[int]) + self.assertEqual(get_type_hints(C, globals())['x'], ClassVar) + self.assertEqual(get_type_hints(C, globals())['y'], Final) with self.assertRaises(TypeError): get_type_hints(CF, globals()), diff --git a/Lib/typing.py b/Lib/typing.py index 36b95d70dea5ba..232aa1a50d2e53 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -173,7 +173,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, ClassVar, Final): return arg if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol): raise TypeError(f"Plain {arg} is not valid as type argument") From 25dbc0b2f509fb6f8d49f92ea47796f072c93964 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 28 Jan 2022 08:47:54 +0000 Subject: [PATCH 2/3] =?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-01-28-08-47-53.bpo-46553.f7Uc96.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst diff --git a/Misc/NEWS.d/next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst b/Misc/NEWS.d/next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst new file mode 100644 index 00000000000000..433c29fb8b3b45 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst @@ -0,0 +1 @@ +In :func:`typing.get_type_hints`, support evaluating bare stringified ``ClassVar` annotations. Patch by Gregory Beauregard. \ No newline at end of file From 3ac306ad00f9fa7da785a0e9a160ce095d562fc0 Mon Sep 17 00:00:00 2001 From: Gregory Beauregard Date: Fri, 28 Jan 2022 00:48:26 -0800 Subject: [PATCH 3/3] Update 2022-01-28-08-47-53.bpo-46553.f7Uc96.rst --- .../next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst b/Misc/NEWS.d/next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst index 433c29fb8b3b45..2c03912afcb451 100644 --- a/Misc/NEWS.d/next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst +++ b/Misc/NEWS.d/next/Library/2022-01-28-08-47-53.bpo-46553.f7Uc96.rst @@ -1 +1 @@ -In :func:`typing.get_type_hints`, support evaluating bare stringified ``ClassVar` annotations. Patch by Gregory Beauregard. \ No newline at end of file +In :func:`typing.get_type_hints`, support evaluating bare stringified ``ClassVar`` annotations. Patch by Gregory Beauregard.