python / python/mypy

`@overload` incorrectly reports error if overload param would go to different params of implementation depending on positional vs keyword calling style

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

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

bug topic-overloads
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

Type-checking on the @overload decorator incorrectly reports an error in the implementation function's signature when an argument would go to different parameters of an implementation function depending on whether that argument is passed as a positional or keyword argument, even if both outcomes are still valid.

To Reproduce

Here's a minimal possible example:

from typing import overload

@overload
def func(value: int):
    ...

def func(positional_only: int | None = None, /, value: int | None = None):
    pass

func can be correctly called with either of these signatures:

func(1)  # results in call with parameters(1, None)
func(value=1)  # results in call with parameters (None, 1)

Either of these calls is valid.

Expected Behavior

The type-checker considers the code valid. (Excluding the error that there's only one overload, which is expected in this minimal example).

Actual Behavior

type_test.py:8: error: Overloaded function implementation does not accept all possible arguments of signature 1  [misc]

Your Environment

  • Mypy version used: mypy 1.7.1 (compiled: yes)
  • Mypy command-line flags: python3 -m mypy ./type_test_2.py
  • Mypy configuration options from mypy.ini (and other config files): Not changed from the default
  • Python version used: Python 3.11.6

A motivating example

I wanted to define a lookup function that can lookup an object by id (an int) or name (a str), something like this:

from typing import overload

@overload
def lookup(id: int):
    ...

@overload
def lookup(name: str):
    ...

def lookup(id_or_name: int | str | None = None, /, id: int | None = None, name: str | None = None):
    if id is None and name is None:
        if isinstance(id_or_name, int): id = id_or_name
        else: name = id_or_name  # name is str

    if id is not None:
        return _lookup_by_id(id)
    else:
        return _lookup_by_name(name)


## Possible calls:

lookup(1)  # integer positional
lookup(id=1)  # integer keyword

lookup("foo")  # string positional
lookup(name="foo")  # string keyword

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 bằng cách chạy lệnh python3 -m mypy ./type_test_2.py đã được báo cáo với ví dụ overload tối thiểu và xác nhận lỗi chữ ký triển khai. Theo dõi quá trình kiểm tra tính tương thích của triển khai overload và bổ sung coverage cho các lời gọi theo vị trí và theo từ khóa đi tới các tham số triển khai khác nhau. Hoàn thành khi các lời gọi hợp lệ không còn tạo ra lỗi đã báo cáo, trong khi các triển khai overload không hợp lệ vẫn bị từ chối.

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
Đặc tả rõ ràng
Mức phù hợp với người mới
45/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.