Skip to content

gh-105509: Simplify the implementation of typing.Annotated #105510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8177,8 +8177,7 @@ class AnnotatedTests(BaseTestCase):

def test_new(self):
with self.assertRaisesRegex(
TypeError,
'Type Annotated cannot be instantiated',
TypeError, 'Cannot instantiate typing.Annotated',
):
Annotated()

Expand Down
38 changes: 13 additions & 25 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,8 @@ def __mro_entries__(self, bases):
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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:data:`typing.Annotated` is now implemented as an instance of
``typing._SpecialForm`` rather than a class. This should have no user-facing
impact for users of the :mod:`typing` module public API.