Covariant type variables should be allowed in class method
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Currently, covariant type variables are allowed in __init__ so that you can put create an immutable containers with something inside of it. See https://github.com/python/mypy/issues/2850.
It would be useful if covariant types are also allowed in class method that serve as alternative constructors. Currently mypy errors on this example, which is an extension of the other issues example:
from typing import Generic, TypeVar
T_co = TypeVar("T_co", covariant=True)
class C(Generic[T_co]):
def __init__(self, x: T_co) -> None:
...
@classmethod
def custom_init(cls, x: T_co) -> "C[T_co]":
# error: Cannot use a covariant type variable as a parameter
return cls(x)
My use case is trying to define an immutable container protocol that has a create class method, which will initialize the container with some contents, like this:
T_cov = typing.TypeVar("T_cov", covariant=True)
class ListProtocol(typing_extensions.Protocol[T_cov]):
@classmethod
def create(cls, *items: T_cov) -> "ListProtocol[T_cov]":
...
def getitem(self) -> T_cov:
...
...
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện vấn đề với các ví dụ classmethod trong báo cáo và kiểm tra cách mypy xác thực các biến kiểu đồng biến trong tham số phương thức. Xác định hành vi dự kiến cho các hàm tạo thay thế, sau đó thêm kiểm thử hồi quy cho thấy các trường hợp được chấp nhận và chạy các bài kiểm thử trình kiểm tra kiểu liên quan.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- tooling
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100