stubtest flags PEP 661 `sentinel` defaults, but not `object()` markers

Đang mở Phù hợp với người mới
#22,013 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.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
75/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
testing, tooling

Hướng nghiên cứu

Bắt đầu với kiểm tra _verify_arg_default_value của stubtest, sử dụng reproduction được cung cấp với foo.py và foo.pyi rồi chạy python -m mypy.stubtest foo. Xác nhận rằng giá trị mặc định typing_extensions.sentinel được xử lý như một marker object() và lệnh không báo lỗi nào, trong khi các giá trị mặc định thông thường không tương thích vẫn tiếp tục được phát hiện.

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

Mô tả

bug

Bug Report

I was eager to try PEP661 support on master, but found what might be an oversight in stubtest. It flags sentinel usage against the stub's parameter type, while it does allow object-style marker objects. Since PEP661's sentinel is now the canonical way to write such a marker, I'd expect it to be allowed in such places:

To Reproduce

# foo.py
from typing_extensions import sentinel
_MISSING = sentinel("_MISSING")


def f(x=_MISSING):
    pass
# foo.pyi
def f(x: int = ...) -> None: ...
python -m mypy.stubtest foo

Expected Behavior

No errors, the same as with _MISSING = object():

Success: no issues found in 1 module

Actual Behavior

error: foo.f is inconsistent, runtime parameter "x" has a default value of type typing_extensions.sentinel, which is incompatible with stub parameter type int
Stub: in file foo.pyi:1
def (x: int =)
Runtime: in file foo.py:5
def (x=_MISSING)

Found 1 error (checked 1 module)

I get the same on Python 3.15 beta.

As described above, adding the sentinel to the stub's type (x: int | _MISSING = ...) isn't an option.

The exemption seems to be in _verify_arg_default_value:

# Avoid false positives for marker objects
and type(runtime_arg.default) is not object

It could be adjusted to include sentinel the same way.

My Environment

  • Mypy version used: master at 8d30064580c4273542f56ab2e9ed6a17caeea8ac (2.4.0+dev)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.14.7 (typing_extensions 4.16.0)
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.