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

オープン
#100,246 コメント 9 件 リアクション 0 件 担当者 1 名 GitHub で見る

@sobolevn がすでに取り組んでいます。

2022年12月14日 から。

評価

この issue はまだ評価されていません。

説明

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 😊

主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。