bpo-46491: Allow Annotated on outside of Final/ClassVar (GH-30864) · python/cpython@e1abffc

@@ -151,7 +151,7 @@ def _type_convert(arg, module=None):

151151

return arg

152152153153154-

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).

156156157157

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