summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
authorGregory Beauregard <greg@greg.red>2022-01-25 06:37:15 (GMT)
committerGitHub <noreply@github.com>2022-01-25 06:37:15 (GMT)
commite1abffca45b60729c460e3e2ad50c8c1946cfd4e (patch)
treee4eba6ba266355518f84b57f91cd55130b3ddc47 /Lib/typing.py
parent9d3c9788a6ccd4f2f53a147dd0026a316c396976 (diff)
downloadcpython-e1abffca45b60729c460e3e2ad50c8c1946cfd4e.zip
cpython-e1abffca45b60729c460e3e2ad50c8c1946cfd4e.tar.gz
cpython-e1abffca45b60729c460e3e2ad50c8c1946cfd4e.tar.bz2
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
Diffstat (limited to 'Lib/typing.py')
-rw-r--r--Lib/typing.py8
1 files changed, 4 insertions, 4 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 7ff546f..e3e098b 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 @@ class ForwardRef(_Final, _root=True):
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 @@ class Annotated:
"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, allow_special_forms=True)
metadata = tuple(params[1:])
return _AnnotatedAlias(origin, metadata)