python / python/mypy

stubtest regression for NewType in compiled extensions

Offen
#19,877 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

For my compiled extension, I have introduced a NewType to prevent confusion with other int-like arguments. So I have something like this:

from typing import NewType

FooType = NewType('FooType', int)

def foo(x: FooType, /) -> FooType: ...

stubtest rightfully complains that this does not exist at runtime:

$ pip install --no-build-isolation . && stubtest newtype --ignore-disjoint-bases
error: newtype.newtype.FooType is not present at runtime
Stub: in file .../venv/lib64/python3.13/site-packages/newtype/newtype.pyi:3
<TypeInfo newtype.newtype.FooType>
Runtime:
MISSING

so I added it to the extension (as the Py_mod_exec function for the module):

static int
newtype_mod_exec(PyObject *module)
{
    PyObject *int_obj = (PyObject *)&PyLong_Type;
    return PyModule_AddObjectRef(module, "FooType", int_obj);
}

Strictly speaking, this is incorrect because NewType at runtime is supposed to produce a lambda that returns its argument directly. However, stubtest 1.17.1 and older are happy with it.

To Reproduce

Please see the simplified example repo here.

Expected Behavior

No complaints from stubtest.

Actual Behavior

stubtest 1.18.1 now complains about the signature:

$ pip install --no-build-isolation . && stubtest newtype --ignore-disjoint-bases
error: newtype.newtype.FooType.__init__ is inconsistent, runtime does not have parameter "item"
Stub: in file .../venv/lib64/python3.13/site-packages/newtype/newtype.pyi:-1
def (self: newtype.newtype.FooType, item: builtins.int)
Runtime:
def (self)

Your Environment

  • Mypy version used: 1.17.1/1.18.1
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13.7

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

Beginne mit dem im Issue verlinkten vereinfachten Beispiel-Repository und führe die gemeldeten stubtest-Befehle mit mypy 1.17.1 und 1.18.1 aus. Verfolge die Laufzeit-Signaturprüfung von NewType und ihr Regressionsverhalten; abgeschlossen ist die Aufgabe, wenn das Beispiel mit der kompilierten Erweiterung stubtest ohne die gemeldete Signaturbeschwerde besteht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing-qa
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

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