[New-bugs-announce] [issue44969] Subclassing of annotated types does not always work

Serhiy Storchaka report at bugs.python.org
Sat Aug 21 03:41:02 EDT 2021


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

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

----------
components: Library (Lib)
messages: 400021
nosy: gvanrossum, kj, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Subclassing of annotated types does not always work
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44969>
_______________________________________


More information about the New-bugs-announce mailing list