Skip to content

Commit

Permalink
🐛 fix typing errors on numpy<1.24 (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenham authored Dec 10, 2024
1 parent d036ca9 commit 4706b2f
Show file tree
Hide file tree
Showing 42 changed files with 299 additions and 360 deletions.
4 changes: 4 additions & 0 deletions .mypyignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ scipy\.(_lib|integrate|stats)\.((_|\w)+\.)+__replace__ # `NamedTuple` on `pytho
scipy\.fftpack\.helper\.fftfreq
scipy\.sparse\.(\w+)\.warn

# numpy re-export with wrong annotations on numpy<1.24
scipy.fftpack.fftfreq
scipy.fftpack._helper.fftfreq

# mypy fails recognize type-check-only ufunc subtypes as ufuncs
# https://github.com/KotlinIsland/basedmypy/issues/816
scipy\.special\._basic\.digamma
Expand Down
16 changes: 4 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ ci:
- typos
- ruff
- ruff-format
- typetest
- basedmypy
- basedpyright

Expand Down Expand Up @@ -74,21 +73,14 @@ repos:

- repo: local
hooks:
- id: typetest
name: typetest
entry: uv run poe typetest
- id: basedpyright
name: basedpyright
entry: uv run --no-sync basedpyright
language: system
always_run: true
pass_filenames: false
types_or: [python, pyi]

- id: basedmypy
name: basedmypy
entry: uv run poe mypy
language: system
types_or: [python, pyi]

- id: basedpyright
name: basedpyright
entry: uv run poe pyright
language: system
types_or: [python, pyi]
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"--config-file=${workspaceFolder}/pyproject.toml"
],
"mypy-type-checker.cwd": "${fileDirname}",
"mypy-type-checker.path": ["uv", "run", "--no-editable", "mypy"]
"mypy-type-checker.path": ["uv", "run", "--no-sync", "mypy"]
}
3 changes: 1 addition & 2 deletions scipy-stubs/_lib/_array_api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ from typing import Any, Literal, Protocol, TypeAlias, overload, type_check_only
from typing_extensions import TypeVar

import numpy as np
import numpy.typing as npt
import optype.numpy as onp
from scipy._typing import AnyBool, OrderKACF

Expand Down Expand Up @@ -55,7 +54,7 @@ class _HasArrayAttrs(_HasShape[_ShapeT_co], _HasDevice[_DeviceT_co], Protocol[_S
###

Array: TypeAlias = _HasArrayAttrs[_ShapeT_co, _DTypeT_co, _DeviceT_co]
ArrayLike: TypeAlias = Array | npt.ArrayLike
ArrayLike: TypeAlias = Array | onp.ToFloatND

def _asarray(
array: ArrayLike,
Expand Down
33 changes: 30 additions & 3 deletions scipy-stubs/cluster/vq.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ from typing import Any, Literal, overload
from typing_extensions import TypeVar

import numpy as np
import numpy.typing as npt
import optype.numpy as onp
from scipy._typing import Seed

Expand All @@ -27,25 +26,53 @@ def vq(
code_book: onp.ToComplex2D,
check_finite: bool = True,
) -> tuple[onp.Array1D[np.int32 | np.intp], onp.Array1D[_SCT_fc]]: ...

#
def py_vq(
obs: onp.ToComplex2D,
code_book: onp.ToComplex2D,
check_finite: bool = True,
) -> tuple[onp.Array1D[np.intp], onp.Array1D[_SCT_fc]]: ...

#
@overload # real
def kmeans(
obs: onp.ToFloat2D,
k_or_guess: onp.ToJustInt | onp.ToFloatND,
iter: int = 20,
thresh: float = 1e-05,
check_finite: bool = True,
*,
seed: Seed | None = None,
) -> tuple[onp.Array2D[np.floating[Any]], float]: ...
@overload # complex
def kmeans(
obs: onp.ToComplex2D,
k_or_guess: npt.ArrayLike,
k_or_guess: onp.ToJustInt | onp.ToFloatND,
iter: int = 20,
thresh: float = 1e-05,
check_finite: bool = True,
*,
seed: Seed | None = None,
) -> tuple[onp.Array2D[np.inexact[Any]], float]: ...

#
@overload # real
def kmeans2(
data: onp.ToFloat1D | onp.ToFloat2D,
k: onp.ToJustInt | onp.ToFloatND,
iter: int = 10,
thresh: float = 1e-05,
minit: Literal["random", "points", "++", "matrix"] = "random",
missing: Literal["warn", "raise"] = "warn",
check_finite: bool = True,
*,
seed: Seed | None = None,
) -> tuple[onp.Array2D[np.floating[Any]], onp.Array1D[np.int32]]: ...
@overload # complex
def kmeans2(
data: onp.ToComplex1D | onp.ToComplex2D,
k: npt.ArrayLike,
k: onp.ToJustInt | onp.ToFloatND,
iter: int = 10,
thresh: float = 1e-05,
minit: Literal["random", "points", "++", "matrix"] = "random",
Expand Down
3 changes: 3 additions & 0 deletions scipy-stubs/fftpack/_helper.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# NOTE: this ignore is required for `numpy==1.23.5` compat
# pyright: reportUnknownVariableType=false

import numpy as np
import optype.numpy as onp
from numpy.fft import fftfreq, fftshift, ifftshift
Expand Down
4 changes: 2 additions & 2 deletions scipy-stubs/interpolate/_pade.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import numpy.typing as npt
import optype.numpy as onp

__all__ = ["pade"]

def pade(an: npt.ArrayLike, m: int, n: int | None = None) -> tuple[np.poly1d, np.poly1d]: ...
def pade(an: onp.ToComplex1D, m: onp.ToJustInt, n: onp.ToJustInt | None = None) -> tuple[np.poly1d, np.poly1d]: ...
6 changes: 3 additions & 3 deletions scipy-stubs/io/_fortran.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ from scipy._typing import EnterSelfMixin, FileLike, FileModeRW

__all__ = ["FortranEOFError", "FortranFile", "FortranFormattingError"]

@type_check_only
@final
class _DTypeKwargs(TypedDict):
@type_check_only
class _DTypeKwargs(TypedDict, total=False):
dtype: npt.DTypeLike

class FortranEOFError(TypeError, OSError): ...
Expand All @@ -19,7 +19,7 @@ class FortranFormattingError(TypeError, OSError): ...
class FortranFile(EnterSelfMixin):
def __init__(self, /, filename: FileLike[bytes], mode: FileModeRW = "r", header_dtype: npt.DTypeLike = ...) -> None: ...
def close(self, /) -> None: ...
def write_record(self, /, *items: npt.ArrayLike) -> None: ...
def write_record(self, /, *items: onp.ToArrayND) -> None: ...
@overload
def read_record(self, /, *dtypes: npt.DTypeLike) -> onp.Array1D[np.void]: ...
@overload
Expand Down
5 changes: 2 additions & 3 deletions scipy-stubs/io/_mmio.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ from typing import Any, ClassVar, Literal, TypeAlias, TypedDict, type_check_only
from typing_extensions import Unpack

import numpy as np
import numpy.typing as npt
import optype.numpy as onp
from scipy._typing import FileLike
from scipy.sparse import coo_matrix, sparray, spmatrix
Expand All @@ -28,7 +27,7 @@ def mminfo(source: FileLike[bytes]) -> _Info: ...
def mmread(source: FileLike[bytes]) -> onp.ArrayND[np.number[Any]] | coo_matrix: ...
def mmwrite(
target: FileLike[bytes],
a: spmatrix | sparray | npt.ArrayLike,
a: spmatrix | sparray | onp.ToArrayND,
comment: str = "",
field: _Field | None = None,
precision: int | None = None,
Expand Down Expand Up @@ -75,7 +74,7 @@ class MMFile:
self,
/,
target: FileLike[bytes],
a: spmatrix | sparray | npt.ArrayLike,
a: spmatrix | sparray | onp.ToArrayND,
comment: str = "",
field: _Field | None = None,
precision: int | None = None,
Expand Down
3 changes: 1 addition & 2 deletions scipy-stubs/io/matlab/_mio4.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ from typing import IO, Any, Final, Generic, Literal, Protocol, TypeAlias, type_c
from typing_extensions import LiteralString, TypeVar

import numpy as np
import numpy.typing as npt
import optype.numpy as onp
from scipy.sparse import coo_matrix, sparray, spmatrix
from ._miobase import MatFileReader
Expand Down Expand Up @@ -119,7 +118,7 @@ class VarWriter4:
def write_bytes(self, /, arr: onp.ArrayND) -> None: ...
def write_string(self, /, s: str) -> None: ...
def write_header(self, /, name: str, shape: Sequence[int], P: _MDType = 0, T: _MClass = 0, imagf: int = 0) -> None: ...
def write(self, /, arr: npt.ArrayLike, name: str) -> None: ...
def write(self, /, arr: onp.ToArrayND, name: str) -> None: ...
def write_numeric(self, /, arr: onp.ArrayND[np.number[Any]], name: str) -> None: ...
def write_char(self, /, arr: onp.ArrayND[np.character], name: str) -> None: ...
def write_sparse(self, /, arr: spmatrix | sparray, name: str) -> None: ...
Expand Down
5 changes: 2 additions & 3 deletions scipy-stubs/io/matlab/_mio5.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ from collections.abc import Iterable, Mapping
from typing import IO, Any, Final, Literal, TypeAlias, TypedDict, final, type_check_only

import numpy as np
import numpy.typing as npt
import optype.numpy as onp
from scipy._typing import AnyShape, ByteOrder
from scipy.sparse import sparray, spmatrix
Expand Down Expand Up @@ -75,8 +74,8 @@ class VarWriter5:
nzmax: int = 0,
) -> None: ...
def update_matrix_tag(self, /, start_pos: int) -> None: ...
def write_top(self, /, arr: npt.ArrayLike, name: str, is_global: bool) -> None: ...
def write(self, /, arr: npt.ArrayLike) -> None: ...
def write_top(self, /, arr: onp.ToArrayND, name: str, is_global: bool) -> None: ...
def write(self, /, arr: onp.ToArrayND) -> None: ...
def write_numeric(self, /, arr: onp.ArrayND[np.bool_ | np.number[Any]]) -> None: ...
def write_char(self, /, arr: onp.ArrayND[np.str_], codec: str = "ascii") -> None: ...
def write_sparse(self, /, arr: spmatrix | sparray) -> None: ...
Expand Down
10 changes: 5 additions & 5 deletions scipy-stubs/linalg/_misc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ def norm(
) -> np.float64: ...
@overload # float64-coercible array, keepdims: True (positional)
def norm(
a: onp.CanArrayND[_SubScalar, _ShapeT],
a: onp.ArrayND[_SubScalar, _ShapeT],
ord: _Order | None,
axis: _Axis | None,
keepdims: _Truthy,
check_finite: AnyBool = True,
) -> onp.ArrayND[np.float64, _ShapeT]: ...
@overload # float64-coercible array, keepdims: True (keyword)
def norm(
a: onp.CanArrayND[_SubScalar, _ShapeT],
a: onp.ArrayND[_SubScalar, _ShapeT],
ord: _Order | None = None,
axis: _Axis | None = None,
*,
Expand All @@ -85,15 +85,15 @@ def norm(
) -> onp.ArrayND[np.float64]: ...
@overload # shaped inexact array, keepdims: True (positional)
def norm(
a: onp.CanArrayND[np.inexact[_NBitT], _ShapeT],
a: onp.ArrayND[np.inexact[_NBitT], _ShapeT],
ord: _Order | None,
axis: _Axis | None,
keepdims: _Truthy,
check_finite: AnyBool = True,
) -> onp.ArrayND[np.floating[_NBitT], _ShapeT]: ...
@overload # shaped inexact array, keepdims: True (keyword)
def norm(
a: onp.CanArrayND[np.inexact[_NBitT], _ShapeT],
a: onp.ArrayND[np.inexact[_NBitT], _ShapeT],
ord: _Order | None = None,
axis: _Axis | None = None,
*,
Expand Down Expand Up @@ -144,7 +144,7 @@ def norm(
) -> onp.ArrayND[np.floating[Any]]: ...
@overload # catch-all
def norm(
a: npt.ArrayLike,
a: onp.ToArrayND,
ord: _Order | None = None,
axis: _Axis | None = None,
keepdims: AnyBool = False,
Expand Down
Loading

0 comments on commit 4706b2f

Please sign in to comment.