python / python/typing

Assignability to a Protocol Using `Self` Must Respect Variance

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

Chưa có ai nhận issue này.

topic: typing spec
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ả

I just stumbled across this paragraph:

https://github.com/python/typing/blob/2d88da2a407b556f5b52b4c6ad1bd659873bb9ac/docs/spec/generics.rst?plain=1#L2540-L2544

If I understand it correctly, then the following code is fine, i.e., Foo is assignable to Proto according to the explanation:

from __future__ import annotations

from typing import Protocol, Self


class Proto(Protocol):
    def f(self, x: Self) -> None: ...


class Foo:
    def f(self, x: Sub) -> None:
        pass


class Sub(Foo):
    pass


x: Proto = Foo()

However, type checkers like mypy and pyright reject this code. This aligns with my expectation: If I have an instance x: Proto, then I should be able to call x.f(x). But for y: Foo, I cannot call y.f(y).

The paragraph in question should distinguish between covariant, contravariant, and invariant occurences of Self:

from __future__ import annotations

from typing import Protocol, Self


class Proto(Protocol):
    def f(self, x: Self) -> None: ...  # contravariant
    def g(self, x: Self) -> Self: ...  # x: contravariant, return type: covariant
    def h(self, x: list[Self]) -> None: ...  # invariant


class Sup:
    pass


class Foo(Sup):
    def f(self, x: Sup) -> None:  # x can be of type Foo or any superclass
        pass

    def g(self, x: Sup) -> Sub:  # return type can be Foo or any subclass
        raise RuntimeError()

    def h(self, x: list[Foo]) -> None:  # no sub-/superclass allowed as argument to list
        pass


class Sub(Foo):
    pass


x: Proto = Foo()  # OK, also according to mypy and pyright

I assume that all the uses of Self are valid in this example, at least mypy and pyright do not complain and I couldn’t find any statement that would restrict Self in protocols to covariant positions or even return types only. Maybe it also makes sense to adjust the example after the paragraph in question. Currently, it only uses Self in a return type.

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

  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.

Hướng nghiên cứu

Bắt đầu với docs/spec/generics.rst tại các dòng được liên kết 2540-2544 và xem lại phần thảo luận xung quanh về Self trong các protocol. Chạy các ví dụ được cung cấp bằng mypy và pyright, sau đó cập nhật đoạn văn và ví dụ gần đó để giải thích các cách sử dụng Self mang tính phản biến, đồng biến và bất biến; hoàn tất khi đặc tả khớp với hành vi được các checker minh họa.

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
documentation
Loại issue
Tài liệu
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
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
38/100

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.