Suggestion: Allow free type variables in type variable bounds that permeate beyond
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 1.8k
- Fork
- 302
- Merge trung bình
- 23 giờ
- Pull request đã merge (30 ngày)
- 8
Mô tả
Suppose we have the following code:
T_co = TypeVar("T_co", covariant=True)
class CanProduce(Protocol[T_co]):
def produce(self) -> T_co:
...
T_in = TypeVar("T_in")
U = TypeVar("U")
@dataclass
class Container(Generic[T_in]):
value: T_in
def produce_from_value(self: Container[CanProduce[U]]) -> U:
return self.value.produce()
This code has an undesirable property: Since T_in is invariant, produce_from_value can only be called on instances of Container whose type parameter is exactly CanProduce[U], for some U:
class IntProducer(CanProduce[int]):
def produce(self) -> int:
return 42
c: Container[IntProducer] = Container(IntProducer())
c.produce_from_value() # this produces a type error
It would be ideal if we could communicate that in this method (which need not be an instance method, it could be a discrete function also), the type parameter behaves as though it were covariant, where any subtype of CanProduce[U] is valid.
In fact, there exists a mechanism for doing this in other situations. Consider if we have the following non-generic protocol:
class SupportsIndex(Protocol):
def __index__(self) -> int:
...
We can then make a method index_from_value that works with all subtypes of SupportsIndex:
SI = TypeVar("SI", bound=SupportsIndex)
@dataclass
class Container(Generic[T_in]):
value: T_in
def index_from_value(self: Container[SI]):
return self.value.__index__
Now, the following is valid:
c: Container[int] = Container(27)
c.index_from_value()
Unfortunately, this method cannot be used with generic protocols, because the following is invalid:
U = TypeVar("U")
CPU = TypeVar("CPU", bound=CanProduce[U]) # this produces a type error
I suggest altering the restriction to allow this trick to work.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
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 với các ví dụ generic về protocol, Container và TypeVar trong issue, sau đó so sánh chúng với trường hợp không generic SupportsIndex. Issue chỉ sẵn sàng để thực hiện khi thay đổi restriction được đề xuất và hành vi của nó đối với các subtype được đặc tả đủ rõ ràng để có thể đánh giá.
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
- devtools
- 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
- 25/100