Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manual typing fixes on numpy 1.24 and python 3.10 #131

Merged
merged 10 commits into from
Oct 22, 2024
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ cmd = """
stubtest
--mypy-config-file=pyproject.toml
--allowlist=tests/stubtest/allowlist.txt
--ignore-unused-allowlist
$modules
"""
args = [{name = "modules", positional = true, multiple = true, default = "scipy"}]
Expand Down
20 changes: 14 additions & 6 deletions scipy-stubs/_lib/_util.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import numpy as np
import numpy.typing as npt
import optype.numpy as onpt
from numpy._typing import _ArrayLikeInt
from numpy.exceptions import (
AxisError as AxisError,
ComplexWarning as ComplexWarning,
DTypePromotionError as DTypePromotionError,
VisibleDeprecationWarning as VisibleDeprecationWarning,
)
from numpy.random import Generator as Generator # noqa: ICN003
from scipy._typing import RNG, EnterSelfMixin, Seed

Expand All @@ -23,13 +17,27 @@ _VT = TypeVar("_VT")
_RT = TypeVar("_RT")
_AxisT = TypeVar("_AxisT", bound=int | np.integer[Any])

###

np_long: Final[type[np.int32 | np.int64]] = ...
np_ulong: Final[type[np.uint32 | np.uint64]] = ...
copy_if_needed: Final[bool | None] = ...

IntNumber: TypeAlias = int | np.integer[Any]
DecimalNumber: TypeAlias = float | np.floating[Any] | np.integer[Any]

class ComplexWarning(RuntimeWarning): ...
class VisibleDeprecationWarning(UserWarning): ...
class DTypePromotionError(TypeError): ...

class AxisError(ValueError, IndexError):
axis: None | int
ndim: None | int
@overload
def __init__(self, /, axis: str, ndim: None = None, msg_prefix: None = None) -> None: ...
@overload
def __init__(self, /, axis: int, ndim: int, msg_prefix: str | None = None) -> None: ...

class FullArgSpec(NamedTuple):
args: Sequence[str]
varargs: str | None
Expand Down
11 changes: 9 additions & 2 deletions scipy-stubs/fftpack/helper.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# This module is not meant for public use and will be removed in SciPy v2.0.0.
import sys
from typing import Final
from typing_extensions import deprecated

__all__ = ["fftfreq", "fftshift", "ifftshift", "next_fast_len", "rfftfreq"]

__MESSAGE: Final = "will be removed in SciPy v2.0.0"

@deprecated(__MESSAGE)
def fftfreq(n: object, d: object = ..., device: object = ...) -> object: ...
if sys.version_info >= (3, 13):
# `device` was added in numpy 2
@deprecated(__MESSAGE)
def fftfreq(n: object, d: object = ..., device: object = ...) -> object: ...
else:
@deprecated(__MESSAGE)
def fftfreq(n: object, d: object = ...) -> object: ...

@deprecated(__MESSAGE)
def fftshift(x: object, axes: object = ...) -> object: ...
@deprecated(__MESSAGE)
Expand Down
4 changes: 2 additions & 2 deletions scipy-stubs/integrate/_quad_vec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ _S = TypeVar("_S")
_T = TypeVar("_T")
_VT = TypeVar("_VT", default=object)

_FloatND: TypeAlias = np.ndarray[tuple[int, ...], np.dtype[np.floating[Any]]] | float | np.floating[Any]
_FloatND: TypeAlias = np.ndarray[Any, np.dtype[np.floating[Any]]] | float | np.floating[Any]
_NDT_f = TypeVar("_NDT_f", bound=_FloatND)
_NDT_f_co = TypeVar("_NDT_f_co", bound=_FloatND, covariant=True, default=_FloatND)
_SCT_f_co = TypeVar("_SCT_f_co", bound=np.floating[Any], covariant=True, default=np.float64)
Expand Down Expand Up @@ -147,7 +147,7 @@ def quad_vec(
) -> tuple[np.floating[_NBT], float, _Bunch[np.floating[_NBT]]]: ...
@overload
def quad_vec(
f: _QuadVecFunc[onpt.Array[_ShapeT, np.floating[_NBT]], Unpack[_Ts]],
f: _QuadVecFunc[np.ndarray[_ShapeT, np.dtype[np.floating[_NBT]]], Unpack[_Ts]],
a: float,
b: float,
epsabs: float = 1e-200,
Expand Down
11 changes: 6 additions & 5 deletions scipy-stubs/io/matlab/_mio5_params.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ __all__ = [
"mxUINT64_CLASS",
]

_ShapeT_co = TypeVar("_ShapeT_co", covariant=True, bound=tuple[int, ...], default=tuple[int, ...])
# NOTE: explicit covariance can't be used, because shape types are invariant in `numpy<2.1`
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...], default=tuple[int, ...])

@type_check_only
class _CodecTemplateValue(TypedDict):
Expand Down Expand Up @@ -141,7 +142,7 @@ _MXName: TypeAlias = Literal[
MDTYPES: Final[_MDTypes] = ...
NP_TO_MTYPES: Final[_NP2M] = ...
NP_TO_MXTYPES: Final[_NP2MX] = ...
# TODO: use `np.dtypes.VoidDType[Literal[32]]` once we have `numpy >= 2.1.0`
# TODO(jorenham): use `np.dtypes.VoidDType[Literal[32]]` once we have `numpy >= 2.1.0`
OPAQUE_DTYPE: Final[np.dtype[np.void]] = ...

codecs_template: Final[_CodecsTemplate] = ...
Expand Down Expand Up @@ -187,18 +188,18 @@ mxOBJECT_CLASS_FROM_MATRIX_H: Final[_MXType] = 18
@final
class mat_struct: ...

class MatlabObject(np.ndarray[_ShapeT_co, np.dtype[np.void]], Generic[_ShapeT_co]):
class MatlabObject(np.ndarray[_ShapeT, np.dtype[np.void]], Generic[_ShapeT]):
classname: Final[str | None]

def __new__(cls, input_array: onpt.AnyVoidArray, classname: str | None = None) -> Self: ...
@override
def __array_finalize__(self, /, obj: None | npt.NDArray[np.void]) -> None: ...

class MatlabFunction(np.ndarray[_ShapeT_co, np.dtype[np.void]], Generic[_ShapeT_co]):
class MatlabFunction(np.ndarray[_ShapeT, np.dtype[np.void]], Generic[_ShapeT]):
@override
def __new__(cls, input_array: onpt.AnyVoidArray) -> Self: ...

class MatlabOpaque(np.ndarray[_ShapeT_co, np.dtype[np.void]], Generic[_ShapeT_co]):
class MatlabOpaque(np.ndarray[_ShapeT, np.dtype[np.void]], Generic[_ShapeT]):
@override
def __new__(cls, input_array: onpt.AnyVoidArray) -> Self: ...

Expand Down
61 changes: 32 additions & 29 deletions scipy-stubs/linalg/_special_matrices.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Literal, TypeAlias, overload
from typing_extensions import TypeVar

import numpy as np
import numpy.typing as npt
import optype.numpy as onpt
import scipy._typing as spt

__all__ = [
Expand All @@ -24,42 +26,43 @@ __all__ = [
"toeplitz",
]

_Array_2d: TypeAlias = np.ndarray[tuple[int, int], np.dtype[np.generic]]
_Array_O_2d: TypeAlias = np.ndarray[tuple[int, int], np.dtypes.ObjectDType]
_Array_u8_2d: TypeAlias = np.ndarray[tuple[int, int], np.dtypes.UInt64DType]
_Array_i8_2d: TypeAlias = np.ndarray[tuple[int, int], np.dtypes.Int64DType]
_Array_f8_2d: TypeAlias = np.ndarray[tuple[int, int], np.dtypes.Float64DType]
_Array_c16_2d: TypeAlias = np.ndarray[tuple[int, int], np.dtypes.Complex128DType]
_SCT = TypeVar("_SCT", bound=np.generic, default=np.generic)

_SymmetryKind: TypeAlias = Literal["symmetric", "upper", "lower"]
_Matrix: TypeAlias = onpt.Array[tuple[int, int], _SCT]
_Kind: TypeAlias = Literal["symmetric", "upper", "lower"]

def toeplitz(c: npt.ArrayLike, r: npt.ArrayLike | None = None) -> _Array_2d: ...
def circulant(c: npt.ArrayLike) -> _Array_2d: ...
def hankel(c: npt.ArrayLike, r: npt.ArrayLike | None = None) -> _Array_2d: ...
def hadamard(n: spt.AnyInt, dtype: npt.DTypeLike = ...) -> _Array_2d: ...
def leslie(f: npt.ArrayLike, s: npt.ArrayLike) -> _Array_2d: ...
def kron(a: npt.ArrayLike, b: npt.ArrayLike) -> _Array_2d: ...
def block_diag(*arrs: npt.ArrayLike) -> _Array_2d: ...
def companion(a: npt.ArrayLike) -> _Array_2d: ...
def helmert(n: spt.AnyInt, full: bool = False) -> _Array_f8_2d: ...
def hilbert(n: spt.AnyInt) -> _Array_f8_2d: ...
###

# TODO(jorenham): transparent dtypes
def toeplitz(c: npt.ArrayLike, r: npt.ArrayLike | None = None) -> _Matrix: ...
def circulant(c: npt.ArrayLike) -> _Matrix: ...
def hankel(c: npt.ArrayLike, r: npt.ArrayLike | None = None) -> _Matrix: ...
def hadamard(n: spt.AnyInt, dtype: npt.DTypeLike = ...) -> _Matrix: ...
def leslie(f: npt.ArrayLike, s: npt.ArrayLike) -> _Matrix: ...
def kron(a: npt.ArrayLike, b: npt.ArrayLike) -> _Matrix: ...
def block_diag(*arrs: npt.ArrayLike) -> _Matrix: ...
def companion(a: npt.ArrayLike) -> _Matrix: ...
def helmert(n: spt.AnyInt, full: bool = False) -> _Matrix[np.float64]: ...
def hilbert(n: spt.AnyInt) -> _Matrix[np.float64]: ...
def fiedler(a: npt.ArrayLike) -> _Matrix: ...
def fiedler_companion(a: npt.ArrayLike) -> _Matrix: ...
def convolution_matrix(a: npt.ArrayLike, n: spt.AnyInt, mode: spt.CorrelateMode = "full") -> _Matrix: ...

#
@overload
def invhilbert(n: spt.AnyInt, exact: Literal[False] = False) -> _Array_f8_2d: ...
def invhilbert(n: spt.AnyInt, exact: Literal[False] = False) -> _Matrix[np.float64]: ...
@overload
def invhilbert(n: spt.AnyInt, exact: Literal[True]) -> _Array_i8_2d | _Array_O_2d: ...
def invhilbert(n: spt.AnyInt, exact: Literal[True]) -> _Matrix[np.int64] | _Matrix[np.object_]: ...
@overload
def pascal(n: spt.AnyInt, kind: _SymmetryKind = "symmetric", exact: Literal[True] = True) -> _Array_u8_2d | _Array_O_2d: ...
def pascal(n: spt.AnyInt, kind: _Kind = "symmetric", exact: Literal[True] = True) -> _Matrix[np.uint64 | np.object_]: ...
@overload
def pascal(n: spt.AnyInt, kind: _SymmetryKind = "symmetric", *, exact: Literal[False]) -> _Array_f8_2d: ...
def pascal(n: spt.AnyInt, kind: _Kind = "symmetric", *, exact: Literal[False]) -> _Matrix[np.float64]: ...
@overload
def pascal(n: spt.AnyInt, kind: _SymmetryKind, exact: Literal[False]) -> _Array_f8_2d: ...
def pascal(n: spt.AnyInt, kind: _Kind, exact: Literal[False]) -> _Matrix[np.float64]: ...
@overload
def invpascal(n: spt.AnyInt, kind: _SymmetryKind = "symmetric", exact: Literal[True] = True) -> _Array_i8_2d | _Array_O_2d: ...
def invpascal(n: spt.AnyInt, kind: _Kind = "symmetric", exact: Literal[True] = True) -> _Matrix[np.int64 | np.object_]: ...
@overload
def invpascal(n: spt.AnyInt, kind: _SymmetryKind = "symmetric", *, exact: Literal[False]) -> _Array_f8_2d: ...
def invpascal(n: spt.AnyInt, kind: _Kind = "symmetric", *, exact: Literal[False]) -> _Matrix[np.float64]: ...
@overload
def invpascal(n: spt.AnyInt, kind: _SymmetryKind, exact: Literal[False]) -> _Array_f8_2d: ...
def dft(n: spt.AnyInt, scale: Literal["sqrtn", "n"] | None = None) -> _Array_c16_2d: ...
def fiedler(a: npt.ArrayLike) -> _Array_2d: ...
def fiedler_companion(a: npt.ArrayLike) -> _Array_2d: ...
def convolution_matrix(a: npt.ArrayLike, n: spt.AnyInt, mode: spt.CorrelateMode = "full") -> _Array_2d: ...
def invpascal(n: spt.AnyInt, kind: _Kind, exact: Literal[False]) -> _Matrix[np.float64]: ...
def dft(n: spt.AnyInt, scale: Literal["sqrtn", "n"] | None = None) -> _Matrix[np.complex128]: ...
12 changes: 6 additions & 6 deletions scipy-stubs/linalg/blas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import scipy._typing as spt

__all__ = ["find_best_blas_type", "get_blas_funcs"]

# see `scipy.linalg.blas._type_conv`
def find_best_blas_type(
arrays: Sequence[npt.NDArray[np.generic]] = (),
dtype: npt.DTypeLike | None = None,
) -> (
# see `scipy.linalg.blas._type_conv`
tuple[Literal["s"], np.dtypes.Float32DType, bool]
| tuple[Literal["f"], np.dtypes.Float64DType, bool]
| tuple[Literal["c"], np.dtypes.Complex64DType, bool]
| tuple[Literal["z"], np.dtypes.Complex128DType, bool]
tuple[Literal["s"], np.dtype[np.float32], bool]
| tuple[Literal["f"], np.dtype[np.float64], bool]
| tuple[Literal["c"], np.dtype[np.complex64], bool]
| tuple[Literal["z"], np.dtype[np.complex128], bool]
): ...
def get_blas_funcs(
names: Iterable[str] | str,
arrays: Sequence[npt.NDArray[np.generic]] = (),
dtype: npt.DTypeLike | None = None,
ilp64: Literal[True, False, "preferred"] = False,
ilp64: Literal[False, True, "preferred"] = False,
) -> list[spt._FortranFunction] | spt._FortranFunction: ...
6 changes: 3 additions & 3 deletions scipy-stubs/signal/_bsplines.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ from scipy._typing import AnyInt, AnyReal

__all__ = ["cspline1d", "cspline1d_eval", "gauss_spline", "qspline1d", "qspline1d_eval", "spline_filter"]

_SCT_fc = TypeVar("_SCT_fc", np.float64, np.float32, np.complex128, np.complex64)
_SCT_fc = TypeVar("_SCT_fc", bound=np.inexact[Any])

@overload
def gauss_spline(x: _ArrayLikeInt_co, n: AnyInt) -> npt.NDArray[np.float64]: ...
@overload
def gauss_spline(x: _ArrayLike[_SCT_fc], n: AnyInt) -> npt.NDArray[_SCT_fc]: ...
@overload
def gauss_spline(x: _ArrayLikeFloat_co, n: AnyInt) -> npt.NDArray[np.floating[Any]]: ...
@overload
def gauss_spline(x: _ArrayLike[_SCT_fc], n: AnyInt) -> npt.NDArray[_SCT_fc]: ...
@overload
def gauss_spline(x: _ArrayLikeComplex_co, n: AnyInt) -> npt.NDArray[np.inexact[Any]]: ...
def spline_filter(Iin: _ArrayLike[_SCT_fc], lmbda: AnyReal = 5.0) -> npt.NDArray[_SCT_fc]: ...
def cspline1d(signal: npt.NDArray[_SCT_fc], lamb: AnyReal = 0.0) -> npt.NDArray[_SCT_fc]: ...
Expand Down
24 changes: 15 additions & 9 deletions scipy-stubs/sparse/bsr.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This module is not meant for public use and will be removed in SciPy v2.0.0.
import sys
from typing_extensions import deprecated

__all__ = [
Expand Down Expand Up @@ -57,15 +58,20 @@ def bsr_transpose(*args: object, **kwargs: object) -> object: ...
def csr_matmat_maxnnz(*args: object, **kwargs: object) -> object: ...
@deprecated("will be removed in SciPy v2.0.0")
def isspmatrix_bsr(x: object) -> object: ...
@deprecated("will be removed in SciPy v2.0.0")
def warn(
message: object,
category: object = ...,
stacklevel: object = ...,
source: object = ...,
*,
skip_file_prefixes: object = ...,
) -> None: ...

if sys.version_info >= (3, 12):
@deprecated("will be removed in SciPy v2.0.0")
def warn(
message: object,
category: object = ...,
stacklevel: object = ...,
source: object = ...,
*,
skip_file_prefixes: object = ...,
) -> None: ...
else:
@deprecated("will be removed in SciPy v2.0.0")
def warn(message: object, category: object = ..., stacklevel: object = ..., source: object = ...) -> None: ...

# sputils
@deprecated("will be removed in SciPy v2.0.0")
Expand Down
24 changes: 15 additions & 9 deletions scipy-stubs/sparse/compressed.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This module is not meant for public use and will be removed in SciPy v2.0.0.
import operator
import sys
from typing_extensions import deprecated

__all__ = [
Expand Down Expand Up @@ -37,15 +38,20 @@ class IndexMixin:
def __getitem__(self, key: object, /) -> object: ...
def __setitem__(self, key: object, x: object, /) -> None: ...

@deprecated("will be removed in SciPy v2.0.0")
def warn(
message: object,
category: object = ...,
stacklevel: object = ...,
source: object = ...,
*,
skip_file_prefixes: object = ...,
) -> None: ...
if sys.version_info >= (3, 12):
@deprecated("will be removed in SciPy v2.0.0")
def warn(
message: object,
category: object = ...,
stacklevel: object = ...,
source: object = ...,
*,
skip_file_prefixes: object = ...,
) -> None: ...
else:
@deprecated("will be removed in SciPy v2.0.0")
def warn(message: object, category: object = ..., stacklevel: object = ..., source: object = ...) -> None: ...

@deprecated("will be removed in SciPy v2.0.0")
def csr_column_index1(*args: object, **kwargs: object) -> object: ...
@deprecated("will be removed in SciPy v2.0.0")
Expand Down
24 changes: 15 additions & 9 deletions scipy-stubs/sparse/coo.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This module is not meant for public use and will be removed in SciPy v2.0.0.
import operator
import sys
from typing import type_check_only
from typing_extensions import deprecated

Expand Down Expand Up @@ -55,15 +56,20 @@ def coo_matvec(*args: object, **kwargs: object) -> object: ...
def coo_tocsr(*args: object, **kwargs: object) -> object: ...
@deprecated("will be removed in SciPy v2.0.0")
def coo_todense(*args: object, **kwargs: object) -> object: ...
@deprecated("will be removed in SciPy v2.0.0")
def warn(
message: object,
category: object = ...,
stacklevel: object = ...,
source: object = ...,
*,
skip_file_prefixes: object = ...,
) -> None: ...

if sys.version_info >= (3, 12):
@deprecated("will be removed in SciPy v2.0.0")
def warn(
message: object,
category: object = ...,
stacklevel: object = ...,
source: object = ...,
*,
skip_file_prefixes: object = ...,
) -> None: ...
else:
@deprecated("will be removed in SciPy v2.0.0")
def warn(message: object, category: object = ..., stacklevel: object = ..., source: object = ...) -> None: ...

# sputils
@type_check_only
Expand Down
Loading