This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Subclassing of annotated types does not always work
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, kj, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-08-21 07:41 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg400021 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-21 07:41
It works only with simple types

>>> class X(Annotated[list, 'annotation']): pass
... 

But not with type aliases

>>> class X(Annotated[List[int], 'annotation']): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: _GenericAlias.__init__() takes 3 positional arguments but 4 were given
>>> class X(Annotated[list[int], 'annotation']): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: GenericAlias expected 2 arguments, got 3

And even if the original type is not subclassable, the error message is not always clear:

>>> class X(Annotated[Union[int, str], 'annotation']): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: _GenericAlias.__init__() takes 3 positional arguments but 4 were given
>>> class X(Annotated[int | str, 'annotation']): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: _GenericAlias.__init__() takes 3 positional arguments but 4 were given
msg400022 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-21 07:47
I think that __mro_entries__() for annotated types should return the result of __mro_entries__() for the original type if it has __mro_entries__. But details can be complicated, especially if user-defined generic types are involved.

Alternatively, __build_class__ can call __mro_entries__() repeatedly for the result of __mro_entries__(). I do not know what is better.
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89132
2021-08-21 07:47:25serhiy.storchakasetmessages: + msg400022
2021-08-21 07:41:02serhiy.storchakacreate