Message 403826 - Python tracker (original) (raw)
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 "", line 1, in 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 "", line 2, in 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 "", line 2, in 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?