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.

Author lukasz.langa
Recipients BvB93, JelleZijlstra, farcat, gvanrossum, kj, lars2, lukasz.langa, miss-islington, pablogsal, serhiy.storchaka, uriyyo
Date 2021-08-07.11:20:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1628335254.9.0.578184235145.issue44524@roundup.psfhosted.org>
In-reply-to
Content
Curiously, while the root cause for the refleaks is in BPO-44856, while hunting down how test_typing.py triggered them, I found that for a while now this exception has been kind of broken:

>>> class C(Union[int, str]): ...
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() takes 2 positional arguments but 4 were given
>>>


It's still a TypeError but the message is cryptic. This regressed in Python 3.9. In Python 3.8 and before, this used to be more descriptive:

>>> class C(Union[int, str]): ...
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ambv/.pyenv/versions/3.8.9/lib/python3.8/typing.py", line 317, in __new__
    raise TypeError(f"Cannot subclass {cls!r}")
TypeError: Cannot subclass <class 'typing._SpecialForm'>


Interestingly, after the Bas' last change, the exception is now yet different:

>>> class C(Union[int, str]): ...
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases


This makes sense, the conflict is due to bases being (typing.Union, <class 'typing.Generic'>) where "typing.Union" is really a _UnionGenericAlias since this is a subscripted Union (unlike bare "typing.Union" which is an instance of _SpecialForm). And in _GenericAlias' __mro_entries__ we're finding:

https://github.com/python/cpython/blob/a40675c659cd8c0699f85ee9ac31660f93f8c2f5/Lib/typing.py#L1089-L1090


Clearly Ivan only intended _name to be used for shadowing builtins and ABCs.



BTW, the "__init__() takes 2 positional arguments but 4 were given" is about _SpecialForm's __init__. It's called with 4 arguments through here in builtin___build_class__:

https://github.com/python/cpython/blob/a40675c659cd8c0699f85ee9ac31660f93f8c2f5/Python/bltinmodule.c#L223-L224


This isn't high priority since the end result is a TypeError anyway, but it's something I will be investigating to make the error message sensible again.
History
Date User Action Args
2021-08-07 11:20:54lukasz.langasetrecipients: + lukasz.langa, gvanrossum, farcat, serhiy.storchaka, JelleZijlstra, pablogsal, miss-islington, uriyyo, BvB93, kj, lars2
2021-08-07 11:20:54lukasz.langasetmessageid: <1628335254.9.0.578184235145.issue44524@roundup.psfhosted.org>
2021-08-07 11:20:54lukasz.langalinkissue44524 messages
2021-08-07 11:20:54lukasz.langacreate