python / python/mypy

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

未关闭 适合新手
#22,013 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug
主要语言
Python
星标
20.6k
派生
3.3k
平均合并
1 天 18 小时
30 天内合并 PR
54

描述

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)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 stubtest 的 _verify_arg_default_value 检查开始,使用提供的 foo.py 和 foo.pyi 复现,并运行 python -m mypy.stubtest foo。确认 typing_extensions.sentinel 默认值会被视为 object() 标记,并且该命令不会报告任何错误,同时不兼容的普通默认值仍会被检测出来。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
testing, tooling
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
活跃
描述清晰度
描述清楚
新手友好度
75/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。