python / python/mypy

Protocol incorrectly matches when positional or keyword parameter matches *args

Đang mở
#21,014 3 bình luận 0 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

When a protocol defines a function with a keyword or positional parameter (a: int) -> None, a concrete function where said parameter is missing, but matches the *args parameter (*args: int) -> None will be seen as implementing the protocol.

I would expect (a: int) -> None to also need matching **kwargs such as (*args: int, **kwargs: int) -> None and for it to only match *args alone when specified as a positional only argument (e.g. (a: int, /) -> None)

Otherwise it cannot be called safely, pyright and ty both successfully raise errors for this case.

To Reproduce

https://mypy-play.net/?mypy=master&python=3.14&flags=strict&gist=a5f86e32e70cbc84c45a53db5426f78f

Expected Behavior

main.py:13: error: Argument 1 to "f" has incompatible type "C"; expected "P"  [arg-type]

because calling the method as foo(a=1) would fail.

Actual Behavior

mypy:

Success: no issues found in 1 source file

pyright:

Argument of type "C" cannot be assigned to parameter "o" of type "P" in function "f"
  "C" is incompatible with protocol "P"
    "foo" is an incompatible type
      Type "(*args: int) -> None" is not assignable to type "(a: int) -> None"
        Missing keyword parameter "a"  (reportArgumentType)

ty:

Argument to function `f` is incorrect: Expected `P`, found `C` (invalid-argument-type) [Ln 17, Col 3]

python:

Traceback (most recent call last):
  File "xxx.py", line 17, in <module>
    f(C())
    ~^^^^^
  File "xxx.py", line 14, in f
    o.foo(a=1)
    ~~~~~^^^^^
TypeError: C.foo() got an unexpected keyword argument 'a'

Your Environment

  • Mypy version used: 1.19.1 and master
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 1.14

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

Tái hiện ví dụ mypy-play được liên kết bằng mypy master, Python 3.14 và --strict. Theo dõi việc xử lý tính tương thích của protocol đối với các chữ ký foo được hiển thị và so sánh kết quả với pyright và ty. Được coi là hoàn tất khi mypy báo lỗi incompatible-protocol như mong đợi mà vẫn giữ nguyên trường hợp positional-only.

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
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.