Skip to content

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

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

Activity

  1. sobolevn commented on Sep 12, 2022

    @sobolevn
    MemberAuthor

    Related: #96080
    This PR introduced explicit __mro_entries__ to _AnnotatedAlias.

  2. sobolevn commented on Sep 12, 2022

    @sobolevn
    MemberAuthor

    But, Annotated was subclassable even in 3.10.0:

    Python 3.10.0 (default, Nov  1 2021, 10:24:06) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import typing
    >>> class Some(typing.Annotated[int, 'meta']): ...
    ... 
    >>> Some.__mro__
    (<class '__main__.Some'>, <class 'int'>, <class 'object'>)
  3. sobolevn commented on Sep 12, 2022

    @sobolevn
    MemberAuthor

    Looks like there's a test for quite the opposite: https://github.com/python/cpython/blame/53a54b781d1f05f2d0b40ce88b3da92d5d23e9d2/Lib/test/test_typing.py#L6713-L6717

    It ensures that Annotated can be subclassed. Related: #27841

  4. AlexWaygood commented on Sep 12, 2022

    @AlexWaygood
    Member

    I disagree that it is a bug that you can subclass Annotated[int, ...]. I think you're meant to be able to use Annotated[int, ...] at runtime at all sites where you could use int. Why should code like this be disallowed?

    SpecialInt: TypeAlias = Annotated[int, "very special"]
    class Foo(SpecialInt): ...

    This works fine, and I don't see why the cases should be treated differently:

    SpecialInt: TypeAlias = int
    class Foo(SpecialInt): ...
  5. AlexWaygood commented on Sep 12, 2022

    @AlexWaygood
    Member

    In any case, it would surely be backwards-incompatible to change this behaviour now?

  6. sobolevn commented on Sep 12, 2022

    @sobolevn
    MemberAuthor

    Yes, changing this will be backward incompatible. Even type-checkers can understand this:

    from typing import Annotated
    
    class Some(Annotated[int, 'meta']):
        pass
    
    reveal_type(Some)
    # ex.py:6: note: Revealed type is "Overload(def (Union[builtins.str, builtins.bytes, array.array[Any], mmap.mmap, ctypes._CData, pickle.PickleBuffer, typing.SupportsInt, typing_extensions.SupportsIndex, _typeshed.SupportsTrunc] =) -> ex.Some, def (Union[builtins.str, builtins.bytes], base: typing_extensions.SupportsIndex) -> ex.Some)"

    So, it probably should stay as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions