python / python/mypy

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

Abierto
#20,079 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-runtime-semantics
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce el informe con scipy-stubs[scipy]==1.16.2.3 y mypy 1.18.2, utilizando el ejemplo de reveal_type. Compara optimize/init.pyi, optimize/_lsq/init.pyi y optimize/_lsq/least_squares.pyi; después, verifica que importar scipy.optimize.least_squares revela la función en lugar de ModuleType.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
tooling
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.