python / python/mypy

scipy-stubs: `scipy.optimize.least_squares` function inferred as module

Offen
#20,079 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

As reported in https://github.com/scipy/scipy-stubs/issues/939, mypy seems to infer scipy.optimize.least_squares as a ModuleType. Other type-checkers (pyright, basedpyright, pyrefly) correctly infer it as a function.

from typing import reveal_type
from scipy.optimize import least_squares

reveal_type(least_squares)
  • mypy: Revealed type is "types.ModuleType"
  • pyrefly: revealed type: (fun: (🍝) -> OptimizeResult
  • (based)pyright: Type of "least_squares" is "(fun: (🍝) -> OptimizeResult"

(🍝 = the huge function signature)

This seems to occur on (at least) scipy-stubs 1.16.2.0 and 1.16.2.3, using mypy 1.18.2.

To repro, install scipy-stubs[scipy]==1.16.2.3 (currently the latest) from pypi or scipy-typed from conda.

Here's are the relevant bits of code:

  1. optimize/__init__.pyi re-exports from ._lsq import least_squares through __all__
  2. optimize/_lsq/__init__.pyi re-exports from .least_squares import least_squares through __all__
  3. optimize/_lsq/least_squares.pyi contains the definition for the least_squares function

It looks like mypy ignores step optimize/_lsq/__init__.pyi from 2., causing it to resolve the optimize._lsq.least_squares module, instead of (explicitly re-exported) optimize._lsq.least_squares.least_squares function.

I expect to be able to work around this by changing from ._lsq import least_squares in optimize/__init__.pyi to from ._lsq.least_squares import least_squares.

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

Reproduziere den Bericht mit scipy-stubs[scipy]==1.16.2.3 und mypy 1.18.2 unter Verwendung des reveal_type-Beispiels. Vergleiche optimize/init.pyi, optimize/_lsq/init.pyi und optimize/_lsq/least_squares.pyi und überprüfe anschließend, dass das Importieren von scipy.optimize.least_squares die Funktion statt ModuleType offenlegt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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