Skip to content

gh-89132: Fix subclassing of annotated types - #155360

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-89132-annotated-mro-entries
Open

gh-89132: Fix subclassing of annotated types#155360
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-89132-annotated-mro-entries

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

Subclassing an annotated type only worked if the annotated type was a class:

>>> from typing import Annotated, List
>>> class X(Annotated[List[int], 'metadata']): pass
...
TypeError: _GenericAlias.__init__() takes 3 positional arguments but 4 were given
>>> class X(Annotated[list[int], 'metadata']): pass
...
TypeError: GenericAlias expected 2 arguments, got 3

__mro_entries__() returned the annotated type itself, but it can need a resolution too. It now delegates to __mro_entries__() of the annotated type, so subclassing Annotated[X, ...] is the same as subclassing X, including the error if X cannot be subclassed.

__mro_entries__() of an annotated type now delegates to __mro_entries__()
of the annotated type itself if it is not a class, e.g. a generic alias.
Subclassing Annotated[X, ...] is now the same as subclassing X.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant