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

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

`

151

151

`return arg

`

152

152

``

153

153

``

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

`

155

155

`"""Check that the argument is a type, and return it (internal helper).

`

156

156

``

157

157

` 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):

`

164

164

` We append the repr() of the actual value (truncated to 100 chars).

`

165

165

` """

`

166

166

`invalid_generic_forms = (Generic, Protocol)

`

167

``

`-

if not is_class:

`

``

167

`+

if not allow_special_forms:

`

168

168

`invalid_generic_forms += (ClassVar,)

`

169

169

`if is_argument:

`

170

170

`invalid_generic_forms += (Final,)

`

`@@ -697,7 +697,7 @@ def _evaluate(self, globalns, localns, recursive_guard):

`

697

697

`eval(self.forward_code, globalns, localns),

`

698

698

`"Forward references must evaluate to types.",

`

699

699

`is_argument=self.forward_is_argument,

`

700

``

`-

is_class=self.forward_is_class,

`

``

700

`+

allow_special_forms=self.forward_is_class,

`

701

701

` )

`

702

702

`self.forward_value = _eval_type(

`

703

703

`type_, globalns, localns, recursive_guard | {self.forward_arg}

`

`@@ -1674,7 +1674,7 @@ def class_getitem(cls, params):

`

1674

1674

`"with at least two arguments (a type and an "

`

1675

1675

`"annotation).")

`

1676

1676

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

`

1678

1678

`metadata = tuple(params[1:])

`

1679

1679

`return _AnnotatedAlias(origin, metadata)

`

1680

1680

``