Łukasz Langa <luk...@langa.pl> added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44524>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to