python / python/mypy

Protocol restriction in generics do not work in methods. But work on functions.

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

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

bug
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ả

Bug Report

Protocol restriction in generics do not work in methods. But work on functions.

To Reproduce

from typing import *

CanCompare = TypeVar("CanCompare", bound='Comparable')


class Comparable(Protocol):
    def __lt__(self: CanCompare, other: CanCompare) -> bool:
        pass

    def __gt__(self: CanCompare, other: CanCompare) -> bool:
        pass

    def __le__(self: CanCompare, other: CanCompare) -> bool:
        pass

    def __ge__(self: CanCompare, other: CanCompare) -> bool:
        pass


A = TypeVar('A', covariant=True)


class CustomList(list, Generic[A]):

    def getMax(self: 'CustomList'[CanCompare]) -> Optional[CanCompare]:
        return functionGetMax(self)


def functionGetMax(aList: 'CustomList'[CanCompare]) -> Optional[CanCompare]:
    pass


canCompareMe: CustomList[int] = CustomList([1, 2, 3])
canCompareMe.getMax()  # ok
functionGetMax(canCompareMe)  # ok


class NotComparable:
    pass  # no comparisons implemented


impossibleToCompare: CustomList[NotComparable] = CustomList([NotComparable(), NotComparable()])
impossibleToCompare.getMax() # Bug: No error raised
functionGetMax(impossibleToCompare)  # Correctly raised error: Value of type variable "CanCompare" of "functionGetMax" cannot be "NotComparable"

Expected Behaviou
Mypy should raise an error on the .getMax() method when called on a CustomList[NotComparable].

Actual Behavior
Instead, no error was raised.
Curiously, the functionGetMax function correctly raises the error.

(Write what happened.)

Your Environment

  • Mypy version used: Both mypy 0.910 and mypy-0.920+dev.58fb493b04c61f6202ac8ba0811d3ff8546a8e60
  • Mypy command-line flags: No flags
  • Mypy configuration options from mypy.ini (and other config files): no config options
  • Python version used: Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:49:53)
  • Operating system and version: Macosx 10.15.7

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.

Hướng nghiên cứu

Bắt đầu với reproducer được cung cấp và so sánh cách mypy kiểm tra ràng buộc protocol generic trong getMax so với functionGetMax. Theo dõi đường dẫn kiểm tra kiểu của lời gọi phương thức và cách nó xử lý CustomList[NotComparable]; hoàn tất khi lời gọi phương thức tạo ra cùng lỗi biến kiểu như lời gọi hàm.

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
Lỗi
Độ 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
42/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.