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

オープン
#20,079 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
tooling

調査の方向性

reveal_type の例を使用して、scipy-stubs[scipy]==1.16.2.3 と mypy 1.18.2 で報告を再現します。optimize/init.pyi、optimize/_lsq/init.pyi、optimize/_lsq/least_squares.pyi を比較し、その後 scipy.optimize.least_squares をインポートすると ModuleType ではなく関数が明らかになることを確認します。

索引モデルが issue の本文から書いたものです。

説明

bug topic-runtime-semantics

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.

主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。