Suggest using Protocol when user tries to use keyword args with Callable

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

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

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
devtools

Hướng nghiên cứu

Start by reproducing the two examples in sample_python.py and inspect how mypy handles Callable annotations and keyword arguments. Review the issue's proposed Protocol direction and establish expected behavior for both examples; done means the agreed behavior is implemented and covered by tests for these cases.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

false-positive priority-1-normal topic-usability

If I define a passed in function using the Callable syntax, mypy complains if I then call a keyword argument on that function. This is makes writing things like decorators problematic

Sample code:

from typing import Callable

def test_func(a, b, c=0, d=0):
    # type: (int, int, int, int) -> int
    return a + b + c + d

def check_func(method):
    # type: (Callable[[int, int, int], int]) -> int
    return method(1, 2, d=5)

print(check_func(test_func))
$ mypy sample_python.py
sample_python.py: note: In function "check_func":
sample_python.py:9: error: Unexpected keyword argument "d"

As an aside here, I the following function code also raises mypy errors:

from typing import Callable

def test_func(a, b, *, c, d):
    # type: (int, int, int, int) -> int
    return a + b + c + d

def check_func(method):
    # type: (Callable[[int, int, int, int], int]) -> int
    return method(1, 2, c=5, d=10)

print(check_func(test_func))
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
54

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.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

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.