8000 `typing.Annotated[int, ...]` is subclassable · Issue #96771 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

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

Closed
sobolevn opened this issue 8000 Sep 12, 2022 · 6 comments
Closed

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

sobolevn opened this issue Sep 12, 2022 · 6 comments
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

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

@sobolevn
Copy link
Member Author

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

@sobolevn
Copy link
Member Author

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'>)

@sobolevn
Copy link
Member Author
sobolevn commented Sep 12, 2022

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

@AlexWaygood
Copy link
Member
AlexWaygood commented Sep 12, 2022

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): ...

@AlexWaygood
Copy link
Member
AlexWaygood commented Sep 12, 2022

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

@sobolevn
Copy link
Member Author

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
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants
0