Skip to content

Commit b0b8388

Browse files
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 (cherry picked from commit e1abffc) Co-authored-by: Gregory Beauregard <[email protected]>
1 parent 3178efb commit b0b8388

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
@@ -4219,6 +4219,14 @@ class C:
42194219
A.x = 5
42204220
self.assertEqual(C.x, 5)
42214221

4222+
def test_special_form_containment(self):
4223+
class C:
4224+
classvar: Annotated[ClassVar[int], "a decoration"] = 4
4225+
const: Annotated[Final[int], "Const"] = 4
4226+
4227+
self.assertEqual(get_type_hints(C, globals())['classvar'], ClassVar[int])
4228+
self.assertEqual(get_type_hints(C, globals())['const'], Final[int])
4229+
42224230
def test_hash_eq(self):
42234231
self.assertEqual(len({Annotated[int, 4, 5], Annotated[int, 4, 5]}), 1)
42244232
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
@@ -134,7 +134,7 @@ def _type_convert(arg, module=None):
134134
return arg
135135

136136

137-
def _type_check(arg, msg, is_argument=True, module=None, *, is_class=False):
137+
def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=False):
138138
"""Check that the argument is a type, and return it (internal helper).
139139
140140
As a special case, accept None and return type(None) instead. Also wrap strings
@@ -147,7 +147,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, is_class=False):
147147
We append the repr() of the actual value (truncated to 100 chars).
148148
"""
149149
invalid_generic_forms = (Generic, Protocol)
150-
if not is_class:
150+
if not allow_special_forms:
151151
invalid_generic_forms += (ClassVar,)
152152
if is_argument:
153153
invalid_generic_forms += (Final,)
@@ -554,7 +554,7 @@ def _evaluate(self, globalns, localns, recursive_guard):
554554
eval(self.__forward_code__, globalns, localns),
555555
"Forward references must evaluate to types.",
556556
is_argument=self.__forward_is_argument__,
557-
is_class=self.__forward_is_class__,
557+
allow_special_forms=self.__forward_is_class__,
558558
)
559559
self.__forward_value__ = _eval_type(
560560
type_, globalns, localns, recursive_guard | {self.__forward_arg__}
@@ -1336,7 +1336,7 @@ def __class_getitem__(cls, params):
13361336
"with at least two arguments (a type and an "
13371337
"annotation).")
13381338
msg = "Annotated[t, ...]: t must be a type."
1339-
origin = _type_check(params[0], msg)
1339+
origin = _type_check(params[0], msg, allow_special_forms=True)
13401340
metadata = tuple(params[1:])
13411341
return _AnnotatedAlias(origin, metadata)
13421342

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)