Skip to content

typing.Annotated[int, ...] is subclassable #96771

Closed
@sobolevn

Description

@sobolevn

You can subclass Annotated[int, "meta"]:

>>> import typing
>>> class Some(typing.Annotated[int, 'meta']): ...
... 
>>> Some.__mro__
(<class '__main__.Some'>, <class 'int'>, <class 'object'>)

But, here are a couple of problems:

  1. All metadata is lost: so, no reason to use Annotated in the first place
  2. https://peps.python.org/pep-0593/ never mentions this use-case
  3. We assume that Annotated cannot be subclassed: https://github.com/python/cpython/blame/53a54b781d1f05f2d0b40ce88b3da92d5d23e9d2/Lib/test/test_typing.py#L6562-L6565 here

I think that Annotated should not be subclasses and this is a bug.
The PR is incoming :)

I found this while working on #96769

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions