mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
gh-105509: Simplify implementation of typing.Annotated
(#105510)
This commit is contained in:
parent
8f9ea43ee8
commit
a1cbace91b
3 changed files with 17 additions and 27 deletions
|
@ -2001,7 +2001,8 @@ class _AnnotatedAlias(_NotIterable, _GenericAlias, _root=True):
|
|||
return (self.__origin__,)
|
||||
|
||||
|
||||
class Annotated:
|
||||
@_SpecialForm
|
||||
def Annotated(self, params):
|
||||
"""Add context-specific metadata to a type.
|
||||
|
||||
Example: Annotated[int, runtime_check.Unsigned] indicates to the
|
||||
|
@ -2048,30 +2049,17 @@ class Annotated:
|
|||
where T1, T2 etc. are TypeVars, which would be invalid, because
|
||||
only one type should be passed to Annotated.
|
||||
"""
|
||||
|
||||
__slots__ = ()
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
raise TypeError("Type Annotated cannot be instantiated.")
|
||||
|
||||
@_tp_cache
|
||||
def __class_getitem__(cls, params):
|
||||
if not isinstance(params, tuple) or len(params) < 2:
|
||||
raise TypeError("Annotated[...] should be used "
|
||||
"with at least two arguments (a type and an "
|
||||
"annotation).")
|
||||
if _is_unpacked_typevartuple(params[0]):
|
||||
raise TypeError("Annotated[...] should not be used with an "
|
||||
"unpacked TypeVarTuple")
|
||||
msg = "Annotated[t, ...]: t must be a type."
|
||||
origin = _type_check(params[0], msg, allow_special_forms=True)
|
||||
metadata = tuple(params[1:])
|
||||
return _AnnotatedAlias(origin, metadata)
|
||||
|
||||
def __init_subclass__(cls, *args, **kwargs):
|
||||
raise TypeError(
|
||||
"Cannot subclass {}.Annotated".format(cls.__module__)
|
||||
)
|
||||
if not isinstance(params, tuple) or len(params) < 2:
|
||||
raise TypeError("Annotated[...] should be used "
|
||||
"with at least two arguments (a type and an "
|
||||
"annotation).")
|
||||
if _is_unpacked_typevartuple(params[0]):
|
||||
raise TypeError("Annotated[...] should not be used with an "
|
||||
"unpacked TypeVarTuple")
|
||||
msg = "Annotated[t, ...]: t must be a type."
|
||||
origin = _type_check(params[0], msg, allow_special_forms=True)
|
||||
metadata = tuple(params[1:])
|
||||
return _AnnotatedAlias(origin, metadata)
|
||||
|
||||
|
||||
def runtime_checkable(cls):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue