Skip to content

Commit e1abffc

Browse files
authored
bpo-46491: Allow Annotated on outside of Final/ClassVar (GH-30864)
We 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. Automerge-Triggered-By: GH:gvanrossum
1 parent 9d3c978 commit e1abffc

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

Lib/test/test_typing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4679,6 +4679,14 @@ class C:
46794679
A.x = 5
46804680
self.assertEqual(C.x, 5)
46814681

4682+
def test_special_form_containment(self):
4683+
class C:
4684+
classvar: Annotated[ClassVar[int], "a decoration"] = 4
4685+
const: Annotated[Final[int], "Const"] = 4
4686+
4687+
self.assertEqual(get_type_hints(C, globals())['classvar'], ClassVar[int])
4688+
self.assertEqual(get_type_hints(C, globals())['const'], Final[int])
4689+
46824690
def test_hash_eq(self):
46834691
self.assertEqual(len({Annotated[int, 4, 5], Annotated[int, 4, 5]}), 1)
46844692
self.assertNotEqual(Annotated[int, 4, 5], Annotated[int, 5, 4])

Lib/typing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def _type_convert(arg, module=None):
151151
return arg
152152

153153

154-
def _type_check(arg, msg, is_argument=True, module=None, *, is_class=False):
154+
def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=False):
155155
"""Check that the argument is a type, and return it (internal helper).
156156
157157
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):
164164
We append the repr() of the actual value (truncated to 100 chars).
165165
"""
166166
invalid_generic_forms = (Generic, Protocol)
167-
if not is_class:
167+
if not allow_special_forms:
168168
invalid_generic_forms += (ClassVar,)
169169
if is_argument:
170170
invalid_generic_forms += (Final,)
@@ -697,7 +697,7 @@ def _evaluate(self, globalns, localns, recursive_guard):
697697
eval(self.__forward_code__, globalns, localns),
698698
"Forward references must evaluate to types.",
699699
is_argument=self.__forward_is_argument__,
700-
is_class=self.__forward_is_class__,
700+
allow_special_forms=self.__forward_is_class__,
701701
)
702702
self.__forward_value__ = _eval_type(
703703
type_, globalns, localns, recursive_guard | {self.__forward_arg__}
@@ -1674,7 +1674,7 @@ def __class_getitem__(cls, params):
16741674
"with at least two arguments (a type and an "
16751675
"annotation).")
16761676
msg = "Annotated[t, ...]: t must be a type."
1677-
origin = _type_check(params[0], msg)
1677+
origin = _type_check(params[0], msg, allow_special_forms=True)
16781678
metadata = tuple(params[1:])
16791679
return _AnnotatedAlias(origin, metadata)
16801680

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow :data:`typing.Annotated` to wrap :data:`typing.Final` and :data:`typing.ClassVar`. Patch by Gregory Beauregard.

0 commit comments

Comments
 (0)