-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Comments
Related: #96080 |
But, 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'>) |
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 |
I disagree that it is a bug that you can subclass 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): ... |
In any case, it would surely be backwards-incompatible to change this behaviour now? |
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. |
You can subclass
Annotated[int, "meta"]
:But, here are a couple of problems:
Annotated
in the first placeAnnotated
cannot be subclassed: https://github.com/python/cpython/blame/53a54b781d1f05f2d0b40ce88b3da92d5d23e9d2/Lib/test/test_typing.py#L6562-L6565 hereI think that
Annotated
should not be subclasses and this is a bug.The PR is incoming :)
I found this while working on #96769
The text was updated successfully, but these errors were encountered: