Class is not generic if it extends `type[X]`

Aperta
#100,246 9 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@sobolevn ci sta già lavorando.

Dal 14/12/2022.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

topic-typing type-bug

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:

  1. Because other stdlib generics work differently:
>>> class Sublist(list[X]): ...
... 
>>> Sublist[int]
  1. 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__, has type in mro
  • class S(type[X]): ... is generic and has __class_getitem__, has type in mro

I can work on a fix if others agree that this is a bug 😊

Lingua principale
Python
Stelle
77.2k
Fork
36k
Merge medio
1g 9h
PR unite (30g)
558

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.