python / python/mypy

stubtest regression for NewType in compiled extensions

Aperta
#19,877 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-stubtest
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il repository di esempio semplificato collegato nell’issue ed esegui i comandi stubtest riportati con mypy 1.17.1 e 1.18.1. Traccia il controllo della firma a runtime di NewType e il relativo comportamento di regressione; il lavoro è completato quando l’esempio dell’estensione compilata supera stubtest senza la segnalazione relativa alla firma.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
testing-qa
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.