python / python/mypy

Report protocol problems in no_variant_matches_arguments if possible

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

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

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

Feature

Report protocol problems in no_variant_matches_arguments if possible.

Pitch

Given following example:

from typing import Protocol, overload


class Foo(Protocol):
    def some_method(self, value: int = 0) -> None: ...


class Bar:
    def some_method(self, value: int) -> None:
        pass


@overload
def accept_foo(value: Foo, v: int) -> None:
    pass


@overload
def accept_foo(value: Foo, v: float) -> None:
    pass


def accept_foo(value: Foo, v: int | float) -> None:
    pass


accept_foo(Bar(), 1)

The Bar has conflict with the Protocol Foo, but currently Mypy won't report the protocol problems. This is the current error report:

protocol_example.py:27: error: No overload variant of "accept_foo" matches argument types "Bar", "int"  [call-overload]
protocol_example.py:27: note: Possible overload variants:
protocol_example.py:27: note:     def accept_foo(value: Foo, v: int) -> None
protocol_example.py:27: note:     def accept_foo(value: Foo, v: float) -> None
Found 1 error in 1 file (checked 1 source file)

If we can report the protocol problems in this case, it may save developers time to figure out what' wrong.

I did a proof-of-concept implementation here: https://github.com/python/mypy/commit/a2040d3680320afbac6e13b3b04593a7965242bc. This is the updated error report:

protocol_example.py:27: error: No overload variant of "accept_foo" matches argument types "Bar", "int"  [call-overload]
protocol_example.py:27: note: Following member(s) of "Bar" have conflicts:
protocol_example.py:27: note:     Expected:
protocol_example.py:27: note:         def some_method(self, value: int = ...) -> None
protocol_example.py:27: note:     Got:
protocol_example.py:27: note:         def some_method(self, value: int) -> None
protocol_example.py:27: note:     Expected:
protocol_example.py:27: note:         def some_method(self, value: int = ...) -> None
protocol_example.py:27: note:     Got:
protocol_example.py:27: note:         def some_method(self, value: int) -> None
protocol_example.py:27: note: Possible overload variants:
protocol_example.py:27: note:     def accept_foo(value: Foo, v: int) -> None
protocol_example.py:27: note:     def accept_foo(value: Foo, v: float) -> None
Found 1 error in 1 file (checked 1 source file)

If this is OK, I'd happy to refine the implementation and submit a PR.

Thanks.

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 tại điểm vào no_variant_matches_arguments và xem xét commit proof-of-concept được liên kết trong issue. Tái hiện ví dụ Protocol và overload được cung cấp, sau đó xác minh rằng chẩn đoán báo cáo chính xác các member xung đột mà không có đầu ra trùng lặp hoặc gây hiểu lầ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
compilers, devtools
Loại issue
Tính năng
Độ 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
35/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.