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

Đang mở
#100,246 9 bình luận 0 reaction 1 người được giao Xem trên GitHub

@sobolevn đang làm issue này rồi.

Từ ngày 14/12/2022.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

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 😊

Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
558

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.