scipy-stubs: `scipy.optimize.least_squares` function inferred as module
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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:
optimize/__init__.pyire-exportsfrom ._lsq import least_squaresthrough__all__optimize/_lsq/__init__.pyire-exportsfrom .least_squares import least_squaresthrough__all__optimize/_lsq/least_squares.pyicontains the definition for theleast_squaresfunction
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report with scipy-stubs[scipy]==1.16.2.3 and mypy 1.18.2, using the reveal_type example. Compare optimize/init.pyi, optimize/_lsq/init.pyi, and optimize/_lsq/least_squares.pyi, then verify that importing scipy.optimize.least_squares reveals the function rather than ModuleType.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100