SciPy Type Stubs
scipy-stubs provides comprehensive type annotations for the SciPy library, enabling static type checking for SciPy-based projects and enhancing IDE features like autocompletion and error detection. It offers full coverage of the public SciPy API with precise shape-typing and dtype-typing without any runtime overhead. The current version is 1.17.1.3, designed to be compatible with SciPy 1.17.1 and supporting Python 3.11-3.14. Releases are independent but target specific SciPy versions.
Warnings
- breaking In `scipy-stubs` v1.16.3.3, the internal `_lib._util._RichResult` class was changed to be a covariant (immutable) type and no longer a subtype of `dict`. Code relying on `_RichResult` behaving like a `dict` at the type level will break.
- gotcha Ensuring version compatibility between `scipy-stubs` and `scipy` is crucial. Each `scipy-stubs` release targets a specific `SciPy` version (e.g., `scipy-stubs 1.17.1.3` targets `SciPy 1.17.1`), and also specifies supported Python and NumPy versions. Mismatched versions can lead to incorrect or missing type hints, causing false positives or negatives from type checkers.
- gotcha Type stubs are for static analysis only; they have no runtime impact or overhead. You do not import from `scipy-stubs` in your code. Instead, you import `scipy` as usual, and your type checker (e.g., MyPy, Pyright) automatically discovers and uses the stub files for type checking.
- gotcha Earlier versions of `scipy-stubs` (and `SciPy`) had known issues with type hints for `scipy.sparse` array/matrix slicing and multi-indexing operations. These were fixed in recent stub releases (e.g., v1.17.1.2, v1.17.1.3).
- gotcha Certain generic types provided by `scipy-stubs` (e.g., for `Rotation` or `RigidTransform` in `scipy.spatial.transform`) might not be subscriptable at runtime in older SciPy versions. This could require `from __future__ import annotations` or stringifying type annotations to avoid runtime `TypeError` when using generic types directly in type hints.
Install
-
pip install scipy-stubs -
pip install scipy-stubs[scipy]
Imports
- scipy
import scipy
- optimize
from scipy import optimize
- linalg
from scipy import linalg
Quickstart
import numpy as np
from scipy.optimize import minimize, OptimizeResult
from typing import Tuple
def rosen(x: np.ndarray) -> np.float64:
"""The Rosenbrock function"""
return np.sum(100.0 * (x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)
def rosen_der(x: np.ndarray) -> np.ndarray:
"""The Rosenbrock function gradient"""
xm = x[1:-1]
xm_m1 = x[:-2]
xm_p1 = x[2:]
grad = np.zeros_like(x)
grad[1:-1] = 200.0 * (xm - xm_m1**2) - 400.0 * (xm_p1 - xm**2) * xm - 2.0 * (1 - xm)
grad[0] = -400.0 * x[0] * (x[1] - x[0]**2) - 2.0 * (1 - x[0])
grad[-1] = 200.0 * (x[-1] - x[-2]**2)
return grad
x0: np.ndarray = np.array([0.5, 0.5])
res: OptimizeResult = minimize(rosen, x0, method='BFGS', jac=rosen_der, options={'disp': True})
print(f"Optimization successful: {res.success}")
print(f"Function value at minimum: {res.fun}")
print(f"Optimal parameters: {res.x}")