python / python/cpython

`singledispatchmethod` inspected signature incorrectly includes `self`

Đang mở Phù hợp với người mới
#117,735 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

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

type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

The result of singledispatchmethod.__get__ is wrapped to imitate the underlying function, instead of the result of func.__get__. This leads to an incorrect signature being reported by inspect.signature, which includes the leading self argument, thus misleading introspection tools as to the real arguments the method accepts.

import inspect
from functools import singledispatchmethod

class A:
    @singledispatchmethod
    def a(self, arg):
        pass

sig = inspect.signature(A().a)
# <Signature (self, arg)> but we expect <Signature (arg)>

sig.bind(None)  # should be OK because `A().a(None)` is OK. Instead:
# TypeError: missing a required argument: 'arg'
# because the first argument is erroneously bound to `self`.

One way to fix this might be to edit https://github.com/python/cpython/blob/6258844c27e3b5a43816e7c559089a5fe0a47123/Lib/functools.py#L979 to read instead

update_wrapper(_method, self.func.__get__(obj, cls))
CPython versions tested on:

3.10, 3.12

Operating systems tested on:

Linux, Windows

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

Đọc Lib/functools.py quanh dòng 979, tập trung vào cách singledispatchmethod.get bọc phương thức đã liên kết. Chạy bản tái hiện inspect.signature và sig.bind được cung cấp; được xem là hoàn tất khi chữ ký đã liên kết bỏ qua self, chấp nhận đối số thực tế của phương thức và các bài kiểm thử liên quan đều đạt.

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ó
2/5
Thời gian dự kiến
1-3 giờ
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
68/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.