From d6f3ff9256d1f02da11c4f99a66be680168b5429 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Mon, 24 Jan 2022 15:42:45 -0800 Subject: [PATCH 1/6] treat Annotated type arg as class-level annotation This exempts it from checks against Final and ClassVar in order to allow using them in any nesting order. --- Lib/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/typing.py b/Lib/typing.py index 7ff546fbb6492e..714d05044a7ce8 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1674,7 +1674,7 @@ def __class_getitem__(cls, params): "with at least two arguments (a type and an " "annotation).") msg = "Annotated[t, ...]: t must be a type." - origin = _type_check(params[0], msg) + origin = _type_check(params[0], msg, is_class=True) metadata = tuple(params[1:]) return _AnnotatedAlias(origin, metadata) From 0beb8aba43d89a179a8595395fc99873cc3ded42 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 24 Jan 2022 23:55:31 +0000 Subject: [PATCH 2/6] =?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-24-23-55-30.bpo-46491.jmIKHo.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst diff --git a/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst b/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst new file mode 100644 index 00000000000000..58f737dc5ba87c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst @@ -0,0 +1 @@ +Allow Annotated on outside of Final/ClassVar. \ No newline at end of file From 0bcf0015197773c25cd84309eb6ccead166f5f04 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Mon, 24 Jan 2022 17:04:25 -0800 Subject: [PATCH 3/6] add RST to NEWS --- .../next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst b/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst index 58f737dc5ba87c..042f69d5f1ff7e 100644 --- a/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst +++ b/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst @@ -1 +1 @@ -Allow Annotated on outside of Final/ClassVar. \ No newline at end of file +Allow :data:`typing.Annotated` on outside of :data:`typing.Final`/:data:`typing.ClassVar`. From 371527eb9be9416ecfaf0629bc919c17630cd2fa Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Mon, 24 Jan 2022 17:05:16 -0800 Subject: [PATCH 4/6] test special form containment --- Lib/test/test_typing.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 150d7c081c30b6..5777656552d797 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4679,6 +4679,14 @@ class C: A.x = 5 self.assertEqual(C.x, 5) + def test_special_form_containment(self): + class C: + classvar: Annotated[ClassVar[int], "a decoration"] = 4 + const: Annotated[Final[int], "Const"] = 4 + + self.assertEqual(get_type_hints(C, globals())['classvar'], ClassVar[int]) + self.assertEqual(get_type_hints(C, globals())['const'], Final[int]) + def test_hash_eq(self): self.assertEqual(len({Annotated[int, 4, 5], Annotated[int, 4, 5]}), 1) self.assertNotEqual(Annotated[int, 4, 5], Annotated[int, 5, 4]) From 040190bae1b1ce8e459cb72cc219ac1667782fd9 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Mon, 24 Jan 2022 17:07:04 -0800 Subject: [PATCH 5/6] rename is_class to more accurately portray usage --- Lib/typing.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 714d05044a7ce8..e3e098b1fcc8f3 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -151,7 +151,7 @@ def _type_convert(arg, module=None): return arg -def _type_check(arg, msg, is_argument=True, module=None, *, is_class=False): +def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=False): """Check that the argument is a type, and return it (internal helper). As a special case, accept None and return type(None) instead. Also wrap strings @@ -164,7 +164,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, is_class=False): We append the repr() of the actual value (truncated to 100 chars). """ invalid_generic_forms = (Generic, Protocol) - if not is_class: + if not allow_special_forms: invalid_generic_forms += (ClassVar,) if is_argument: invalid_generic_forms += (Final,) @@ -697,7 +697,7 @@ def _evaluate(self, globalns, localns, recursive_guard): eval(self.__forward_code__, globalns, localns), "Forward references must evaluate to types.", is_argument=self.__forward_is_argument__, - is_class=self.__forward_is_class__, + allow_special_forms=self.__forward_is_class__, ) self.__forward_value__ = _eval_type( type_, globalns, localns, recursive_guard | {self.__forward_arg__} @@ -1674,7 +1674,7 @@ def __class_getitem__(cls, params): "with at least two arguments (a type and an " "annotation).") msg = "Annotated[t, ...]: t must be a type." - origin = _type_check(params[0], msg, is_class=True) + origin = _type_check(params[0], msg, allow_special_forms=True) metadata = tuple(params[1:]) return _AnnotatedAlias(origin, metadata) From 4ead0d980dd04749067baaa0581385987a09ddce Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Mon, 24 Jan 2022 21:09:14 -0800 Subject: [PATCH 6/6] slight wording adjustment and add name to NEWS --- .../next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst b/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst index 042f69d5f1ff7e..f66e8868f753fb 100644 --- a/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst +++ b/Misc/NEWS.d/next/Library/2022-01-24-23-55-30.bpo-46491.jmIKHo.rst @@ -1 +1 @@ -Allow :data:`typing.Annotated` on outside of :data:`typing.Final`/:data:`typing.ClassVar`. +Allow :data:`typing.Annotated` to wrap :data:`typing.Final` and :data:`typing.ClassVar`. Patch by Gregory Beauregard.