Class is not generic if it extends `type[X]`
@sobolevn is already working on this.
Since Dec 14, 2022.
Assessment
This issue has not been assessed yet.
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.Typeworks 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__, hastypeinmroclass S(type[X]): ...is generic and has__class_getitem__, hastypeinmro
I can work on a fix if others agree that this is a bug 😊
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 36k
- Avg merge
- 1d 9h
- Merged PRs (30d)
- 558
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/cpython
-
docs pending
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
stdlib type-feature
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
stdlib type-feature
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
build type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
stdlib topic-email type-feature
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
OpenHands/extensions#626 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
CSCfi/sd-search-api#39 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
StevenBlack/hosts#3255 ·