Message403826
The cause is that isinstance(list[int], type) returns True. It can cause bugs in other parts of the code which test for instance of type. For example:
>>> types.resolve_bases((typing.List[int],))
(<class 'list'>, <class 'typing.Generic'>)
>>> types.resolve_bases((list[int],))
(list[int],)
>>> types.prepare_class('A', (int,), {'metaclass': typing.Type[int]})
(typing.Type[int], {}, {})
>>> types.prepare_class('A', (int,), {'metaclass': type[int]})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython/Lib/types.py", line 125, in prepare_class
meta = _calculate_meta(meta, bases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/serhiy/py/cpython/Lib/types.py", line 139, in _calculate_meta
if issubclass(base_meta, winner):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() argument 2 cannot be a parameterized generic
>>> @functools.singledispatch
... def g(a): pass
...
>>> @g.register
... def g2(a: typing.List[int]): pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/serhiy/py/cpython/Lib/functools.py", line 863, in register
raise TypeError(
^^^^^^^^^^^^^^^^
TypeError: Invalid annotation for 'a'. typing.List[int] is not a class.
>>> @g.register(list[int])
... def g2(a): pass
...
>>> @g.register
... def g3(a: typing.List[int]): pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/serhiy/py/cpython/Lib/functools.py", line 863, in register
raise TypeError(
^^^^^^^^^^^^^^^^
TypeError: Invalid annotation for 'a'. typing.List[int] is not a class.
>>> @g.register
... def g3(a: list[int]): pass
...
And many other examples, too many to show here.
Was it mistake to make isinstance(list[int], type) returning True? |
|
Date |
User |
Action |
Args |
2021-10-13 11:54:59 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, gvanrossum, rhettinger, levkivskyi, kj |
2021-10-13 11:54:59 | serhiy.storchaka | set | messageid: <1634126099.57.0.58909911192.issue45438@roundup.psfhosted.org> |
2021-10-13 11:54:59 | serhiy.storchaka | link | issue45438 messages |
2021-10-13 11:54:59 | serhiy.storchaka | create | |
|