python / python/cpython

`inspect` does not correctly handle over-length `__defaults__` for functions

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

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

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

Mô tả

Bug report

Bug description:

This is largely a re-report of #81244 which I believe was inappropriately dismissed. This is a bug because it causes functions to be incorrectly documented and understood. For example, it breaks pydoc and therefore the built-in help.

Python explicitly allows for the __defaults__ of a function to be re-assigned. Presumably this was for a reason. Having modified a function this way, it would make sense to have an accurate signature for the result.

However, consider the output of the following (3.8+; requires modification for far-EOL versions):

import inspect

def example(a, b, /, c, d): return (a, b, c, d)

for count in range(10):
    example.__defaults__ = tuple(range(count))
    signature = inspect.signature(example)
    result = example(*'abcd'[:max(0, 4-count)])
    print(f'with {count} defaults: {signature=!s:23}, {result=}')

We get:

with 0 defaults: signature=(a, b, /, c, d)        , result=('a', 'b', 'c', 'd')
with 1 defaults: signature=(a, b, /, c, d=0)      , result=('a', 'b', 'c', 0)
with 2 defaults: signature=(a, b, /, c=0, d=1)    , result=('a', 'b', 0, 1)
with 3 defaults: signature=(a, b=0, /, c=1, d=2)  , result=('a', 0, 1, 2)
with 4 defaults: signature=(a=0, b=1, /, c=2, d=3), result=(0, 1, 2, 3)
with 5 defaults: signature=(a, b, /, c, d=0)      , result=(1, 2, 3, 4)
with 6 defaults: signature=(a, b, /, c=0, d=1)    , result=(2, 3, 4, 5)
with 7 defaults: signature=(a, b=0, /, c=1, d=2)  , result=(3, 4, 5, 6)
with 8 defaults: signature=(a=0, b=1, /, c=2, d=3), result=(4, 5, 6, 7)
with 9 defaults: signature=(a=0, b=1, /, c=2, d=3), result=(5, 6, 7, 8)

As can be seen, when there are more __defaults__ than parameters, inspect's logic for pairing default values with parameters differs from what the runtime does. With up to 2N values it behaves as though the last N were missing; with even more, it behaves as if all but the first N were missing. Whereas the runtime consistently uses the last N values of the tuple. There is no distinction between positional-only and positional-or-keyword arguments.

Consequently, help(example) shows an incorrect signature as well. In my testing, there has been an issue with help used this way since 3.4, though it works correctly in 2.7 and 3.3.

If this is not deemed a bug in inspect's logic (why does it not simply have access to the interpreter's logic?), then there is a problem with the runtime. Arguably, it should reject attempts to set __defaults__ to an over-long tuple, since its actual behaviour (taking only the last N values) is not unambiguously the right way to handle it. "In the face of ambiguity, refuse the temptation to guess."

N.B. From further testing, it appears that inspect.signature is still mistaken in 3.3 (the version where it was added), but the builtin help() still works, even though pydoc still imports inspect. Presumably a legacy method was used to compute the signature for help output, which was then changed in 3.4.

CPython versions tested on:

3.3 (pydoc/help() works but inspect.signature does not), 3.4 .. 3.14 inclusive (not working)

(pydoc/help() works in 2.7; unclear if/how inspect is involved)

Operating systems tested on:

Linux (but most likely platform-agnostic)

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

Issue xác định inspect.signature, pydoc và help() là các entry point bị ảnh hưởng; trước tiên hãy lần theo cách inspect ghép defaults với các tham số. Xác minh hành vi đối với các defaults quá dài so với runtime, sau đó bổ sung coverage cho thấy signature được hiển thị sử dụng nhất quán các giá trị cuối cùng.

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