python / python/mypy

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

Offen Anfängerfreundlich
#22,013 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der Prüfung _verify_arg_default_value von stubtest, verwenden Sie die bereitgestellte Reproduktion mit foo.py und foo.pyi und führen Sie python -m mypy.stubtest foo aus. Bestätigen Sie, dass ein Standardwert typing_extensions.sentinel wie eine object()-Markierung behandelt wird und dass der Befehl keine Fehler meldet, während inkompatible gewöhnliche Standardwerte weiterhin erkannt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing, tooling
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Aktiv
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
75/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.