python / python/cpython

inspect.signature crashes with NameError for TYPE_CHECKING-only annotations

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

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

stdlib 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:
if typing.TYPE_CHECKING:
    from somelib import SomeType

def f(arg: SomeType): ...

inspect.signature(f)  # NameError: name 'SomeType' is not defined

Some linters have rules that move imports into TYPE_CHECKING blocks and after this seemingly benign change inspect.signature will unexpectedly fail, even if its caller never cared about annotations.

At the very least, the documentation of inspect.signature should warn about this (currently it only talks about string annotations and makes no mention of Python 3.14 lazy annotations). But perhaps there’s a way of making inspect.signature work without such a surprising behavior, maybe by making some Signature fields lazy.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-153055

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 với entry point inspect.signature và hành vi lazy-annotation của Python 3.14 được mô tả trong báo cáo. Xem xét PR gh-153055 được liên kết trước khi xác định phạm vi được chấp nhận là tài liệu hay thay đổi hành vi; issue không xác định một điều kiện hoàn thành duy nhấ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
backend
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/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.