Open
Description
Code sample:
>>> from typing import TypeVar
>>> X = TypeVar('X')
>>> class Subtype(type[X]): ...
...
>>> Subtype[int]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type 'Subtype' is not subscriptable
This is quite strange. To make it generic, you must have explicit Generic
base class:
>>> from typing import Generic
>>> class Subtype(type[X], Generic[X]): ...
...
>>> Subtype[int]
__main__.Subtype[int]
It looks like a bug to me because of two reasons:
- Because other stdlib generics work differently:
>>> class Sublist(list[X]): ...
...
>>> Sublist[int]
- Because
typing.Type
works as it should:
>>> from typing import Type
>>> class Subtype(Type[X]): ...
...
>>> Subtype[int]
__main__.Subtype[int]
But, I think that type
is special enough to be handled extra carefully:
class S(type): ...
is not generic and does not have__class_getitem__
, hastype
inmro
class S(type[X]): ...
is generic and has__class_getitem__
, hastype
inmro
I can work on a fix if others agree that this is a bug 😊