Skip to content

Commit a4bfa01

Browse files
authored
♻️ always use optype.numpy.compat for abstract scalar types (#730)
2 parents f81050c + c7954c9 commit a4bfa01

File tree

86 files changed

+512
-454
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+512
-454
lines changed

scipy-stubs/_lib/_elementwise_iterative_method.pyi

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ from typing_extensions import TypeVar
66
import numpy as np
77
import optype as op
88
import optype.numpy as onp
9+
import optype.numpy.compat as npc
910

1011
from ._util import _RichResult
1112
from scipy._typing import Falsy
1213

1314
###
1415

15-
_FloatT = TypeVar("_FloatT", bound=np.floating[Any], default=np.float64)
16+
_FloatT = TypeVar("_FloatT", bound=npc.floating, default=np.float64)
1617
_ShapeT = TypeVar("_ShapeT", bound=onp.AtLeast1D, default=onp.AtLeast0D[Any])
1718
_FuncRealT = TypeVar("_FuncRealT", bound=Callable[Concatenate[onp.ArrayND[np.float64], ...], object])
1819
_ModuleT = TypeVar("_ModuleT", bound=ModuleType, default=ModuleType)
@@ -44,7 +45,7 @@ def _initialize(
4445
_FuncRealT, # func
4546
list[onp.Array1D[_FloatT]], # xs
4647
list[onp.Array1D[_FloatT]], # fs
47-
list[onp.Array1D[np.floating[Any]]], # args
48+
list[onp.Array1D[npc.floating]], # args
4849
onp.AtLeast1D, # shape
4950
_FloatT, # xfat
5051
_ModuleT, # xp
@@ -57,10 +58,10 @@ def _loop(
5758
shape: Sequence[op.CanIndex],
5859
maxiter: int,
5960
func: Callable[[onp.Array[_ShapeT, _FloatT]], onp.ToComplexND],
60-
args: tuple[onp.ArrayND[np.floating[Any]], ...],
61-
dtype: np.inexact[Any],
61+
args: tuple[onp.ArrayND[npc.floating], ...],
62+
dtype: npc.inexact,
6263
pre_func_eval: Callable[[_ResT], onp.Array[_ShapeT, _FloatT]],
63-
post_func_eval: Callable[[onp.Array[_ShapeT, _FloatT], onp.Array[_ShapeT, np.floating[Any]], _ResT], _Ignored],
64+
post_func_eval: Callable[[onp.Array[_ShapeT, _FloatT], onp.Array[_ShapeT, npc.floating], _ResT], _Ignored],
6465
check_termination: Callable[[_ResT], onp.Array[_ShapeT, np.bool_]],
6566
post_termination_check: Callable[[_ResT], _Ignored],
6667
customize_result: Callable[[_ResT, _ToShapeT], tuple[int, ...]],
@@ -74,7 +75,7 @@ def _check_termination(
7475
work: _WorkT,
7576
res: Mapping[str, onp.Array[_ShapeT, _FloatT]],
7677
res_work_pairs: Iterable[tuple[str, str]],
77-
active: onp.Array[_ShapeT, np.integer[Any]],
78+
active: onp.Array[_ShapeT, npc.integer],
7879
check_termination: Callable[[_WorkT], onp.Array[_ShapeT, np.bool_]],
7980
preserve_shape: bool | None,
8081
xp: ModuleType,
@@ -85,7 +86,7 @@ def _update_active(
8586
work: Mapping[str, onp.Array[_ShapeT, _FloatT]],
8687
res: Mapping[str, onp.Array[_ShapeT, _FloatT]],
8788
res_work_pairs: Iterable[tuple[str, str]],
88-
active: onp.Array[_ShapeT, np.integer[Any]],
89+
active: onp.Array[_ShapeT, npc.integer],
8990
mask: onp.Array[_ShapeT, np.bool_] | None,
9091
preserve_shape: bool | None,
9192
xp: ModuleType,
@@ -96,7 +97,7 @@ def _prepare_result(
9697
work: Mapping[str, onp.Array[_ShapeT, _FloatT]],
9798
res: _ResT,
9899
res_work_pairs: Iterable[tuple[str, str]],
99-
active: onp.Array[_ShapeT, np.integer[Any]],
100+
active: onp.Array[_ShapeT, npc.integer],
100101
shape: _ToShapeT,
101102
customize_result: Callable[[_ResT, _ToShapeT], tuple[int, ...]],
102103
preserve_shape: bool | None,

scipy-stubs/_typing.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ from _typeshed import Incomplete
44
from collections.abc import Iterator, Sequence
55
from os import PathLike
66
from types import TracebackType
7-
from typing import IO, Any, Literal, LiteralString, Protocol, Self, SupportsIndex, TypeAlias, overload, type_check_only
7+
from typing import IO, Literal, LiteralString, Protocol, Self, SupportsIndex, TypeAlias, overload, type_check_only
88
from typing_extensions import TypeVar
99

1010
import numpy as np
1111
import optype as op
1212
import optype.numpy as onp
13+
import optype.numpy.compat as npc
1314

1415
__all__ = [
1516
"RNG",
@@ -52,7 +53,7 @@ class _FortranFunction(Protocol):
5253
@property
5354
def dtype(self, /) -> np.dtype[Incomplete]: ...
5455
@property
55-
def int_dtype(self, /) -> np.dtype[np.integer[Any]]: ...
56+
def int_dtype(self, /) -> np.dtype[npc.integer]: ...
5657
@property
5758
def module_name(self, /) -> LiteralString: ...
5859
@property
@@ -99,9 +100,9 @@ RNG: TypeAlias = np.random.Generator | np.random.RandomState
99100
# NOTE: This is less incorrect and more accurate than the current `np.random.default_rng` `seed` param annotation.
100101
ToRNG: TypeAlias = (
101102
int
102-
| np.integer[Any]
103+
| npc.integer
103104
| np.timedelta64
104-
| onp.ArrayND[np.integer[Any] | np.timedelta64 | np.flexible | np.object_]
105+
| onp.ArrayND[npc.integer | np.timedelta64 | np.flexible | np.object_]
105106
| np.random.SeedSequence
106107
| np.random.BitGenerator
107108
| RNG

scipy-stubs/cluster/hierarchy.pyi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from collections.abc import Callable
22
from types import ModuleType
3-
from typing import Any, Final, Literal, TypeAlias, TypedDict, overload, type_check_only
3+
from typing import Final, Literal, TypeAlias, TypedDict, overload, type_check_only
44
from typing_extensions import TypeVar, override
55

66
import numpy as np
77
import optype as op
88
import optype.numpy as onp
9+
import optype.numpy.compat as npc
910

1011
from scipy._lib._disjoint_set import DisjointSet
1112
from scipy._typing import Falsy, Truthy
@@ -47,7 +48,7 @@ __all__ = [
4748
]
4849

4950
_T = TypeVar("_T")
50-
_SCT = TypeVar("_SCT", bound=np.number[Any], default=np.float64)
51+
_SCT = TypeVar("_SCT", bound=npc.number, default=np.float64)
5152

5253
_LinkageArray: TypeAlias = onp.Array2D[_SCT]
5354
_LinkageMethod: TypeAlias = Literal["single", "complete", "average", "weighted", "centroid", "median", "ward"]
@@ -196,7 +197,7 @@ def dendrogram(
196197
Z: onp.ToArray2D,
197198
p: int = 30,
198199
truncate_mode: _TruncateMode | None = None,
199-
color_threshold: float | np.floating[Any] | None = None,
200+
color_threshold: float | npc.floating | None = None,
200201
get_leaves: bool = True,
201202
orientation: _Orientation = "top",
202203
labels: onp.ToArrayND | None = None,
@@ -205,8 +206,8 @@ def dendrogram(
205206
show_leaf_counts: bool = True,
206207
no_plot: bool = False,
207208
no_labels: bool = False,
208-
leaf_font_size: float | np.floating[Any] | None = None,
209-
leaf_rotation: float | np.floating[Any] | None = None,
209+
leaf_font_size: float | npc.floating | None = None,
210+
leaf_rotation: float | npc.floating | None = None,
210211
leaf_label_func: Callable[[int], str] | None = None,
211212
show_contracted: bool = False,
212213
link_color_func: Callable[[int], str] | None = None,

scipy-stubs/differentiate/_differentiate.pyi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ from typing_extensions import TypeVar
44

55
import numpy as np
66
import optype.numpy as onp
7+
import optype.numpy.compat as npc
78
import optype.typing as opt
89

910
from scipy._lib._util import _RichResult
1011

11-
_FloatT = TypeVar("_FloatT", bound=np.floating[Any], default=np.float64)
12-
_FloatT_co = TypeVar("_FloatT_co", bound=np.floating[Any], default=np.float64, covariant=True)
12+
_FloatT = TypeVar("_FloatT", bound=npc.floating, default=np.float64)
13+
_FloatT_co = TypeVar("_FloatT_co", bound=npc.floating, default=np.float64, covariant=True)
1314
_ShapeT = TypeVar("_ShapeT", bound=onp.AtLeast1D, default=onp.AtLeast0D[Any])
1415
_ShapeT_co = TypeVar("_ShapeT_co", bound=onp.AtLeast1D, default=onp.AtLeast0D[Any], covariant=True)
1516
_ShapeT2_co = TypeVar("_ShapeT2_co", bound=onp.AtLeast2D, default=onp.AtLeast0D[Any], covariant=True)
@@ -67,7 +68,7 @@ class _HessianResult(_RichResult, Generic[_FloatT_co, _ShapeT2_co]):
6768
@overload # 0-d float64
6869
def derivative(
6970
f: _Function00[np.float64],
70-
x: float | np.float64 | np.integer[Any] | onp.CanArray0D[np.float64 | np.integer[Any]],
71+
x: float | np.float64 | npc.integer | onp.CanArray0D[np.float64 | npc.integer],
7172
*,
7273
args: tuple[onp.ToScalar, ...] = (),
7374
tolerances: _Tolerances | None = None,
@@ -103,9 +104,9 @@ def derivative(
103104
tolerances: _Tolerances | None = None,
104105
maxiter: opt.AnyInt = 10,
105106
order: opt.AnyInt = 8,
106-
initial_step: onp.ToFloat | onp.ToFloat1D | onp.CanArrayND[np.floating[Any], _ShapeT] = 0.5,
107+
initial_step: onp.ToFloat | onp.ToFloat1D | onp.CanArrayND[npc.floating, _ShapeT] = 0.5,
107108
step_factor: onp.ToFloat = 2.0,
108-
step_direction: onp.ToJustInt | onp.ToJustInt1D | onp.CanArrayND[np.integer[Any], _ShapeT] = 0,
109+
step_direction: onp.ToJustInt | onp.ToJustInt1D | onp.CanArrayND[npc.integer, _ShapeT] = 0,
109110
preserve_shape: bool = False,
110111
callback: Callable[[_DerivativeResultND[_FloatT, _ShapeT]], _Ignored] | None = None,
111112
) -> _DerivativeResultND[_FloatT, _ShapeT]: ...

scipy-stubs/integrate/_bvp.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from collections.abc import Callable
2-
from typing import Any, Final, Generic, Literal, TypeAlias, overload
2+
from typing import Final, Generic, Literal, TypeAlias, overload
33
from typing_extensions import TypeVar
44

55
import numpy as np
66
import optype.numpy as onp
7+
import optype.numpy.compat as npc
78

89
from scipy._typing import Falsy, Truthy
910
from scipy.interpolate import PPoly
1011
from scipy.sparse import csc_matrix
1112

1213
###
1314

14-
_SCT_fc = TypeVar("_SCT_fc", bound=np.inexact[Any], default=np.float64 | np.complex128)
15+
_SCT_fc = TypeVar("_SCT_fc", bound=npc.inexact, default=np.float64 | np.complex128)
1516

1617
_FunRHS: TypeAlias = Callable[[onp.Array1D, onp.Array2D[_SCT_fc]], onp.ArrayND[_SCT_fc]]
1718
_FunRHS_p: TypeAlias = Callable[[onp.Array1D, onp.Array2D[_SCT_fc], onp.Array1D], onp.ArrayND[_SCT_fc]]

scipy-stubs/integrate/_ivp/base.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ from typing import Any, ClassVar, Final, Generic, Literal, TypeVar, overload
33

44
import numpy as np
55
import optype.numpy as onp
6+
import optype.numpy.compat as npc
67

78
from scipy._typing import Truthy
89

9-
_VT = TypeVar("_VT", bound=onp.ArrayND[np.inexact[Any]], default=onp.ArrayND[Any])
10+
_VT = TypeVar("_VT", bound=onp.ArrayND[npc.inexact], default=onp.ArrayND[Any])
1011

1112
class OdeSolver:
1213
TOO_SMALL_STEP: ClassVar[str] = ...
@@ -60,9 +61,9 @@ class DenseOutput:
6061

6162
def __init__(self, /, t_old: onp.ToFloat, t: onp.ToFloat) -> None: ...
6263
@overload
63-
def __call__(self, /, t: onp.ToFloat) -> onp.Array1D[np.inexact[Any]]: ...
64+
def __call__(self, /, t: onp.ToFloat) -> onp.Array1D[npc.inexact]: ...
6465
@overload
65-
def __call__(self, /, t: onp.ToFloatND) -> onp.ArrayND[np.inexact[Any]]: ...
66+
def __call__(self, /, t: onp.ToFloatND) -> onp.ArrayND[npc.inexact]: ...
6667

6768
class ConstantDenseOutput(DenseOutput, Generic[_VT]):
6869
value: _VT

scipy-stubs/integrate/_ivp/bdf.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
from collections.abc import Callable
2-
from typing import Any, Final, Generic, Never, TypeAlias
2+
from typing import Final, Generic, Never, TypeAlias
33
from typing_extensions import TypeVar
44

55
import numpy as np
66
import optype.numpy as onp
7+
import optype.numpy.compat as npc
78

89
from .base import DenseOutput, OdeSolver
910
from scipy.sparse import sparray, spmatrix
1011

1112
###
1213

13-
_SCT_co = TypeVar("_SCT_co", covariant=True, bound=np.inexact[Any], default=np.float64 | np.complex128)
14+
_SCT_co = TypeVar("_SCT_co", covariant=True, bound=npc.inexact, default=np.float64 | np.complex128)
1415

15-
_LU: TypeAlias = tuple[onp.ArrayND[np.inexact[Any]], onp.ArrayND[np.integer[Any]]]
16+
_LU: TypeAlias = tuple[onp.ArrayND[npc.inexact], onp.ArrayND[npc.integer]]
1617
_FuncLU: TypeAlias = Callable[[onp.ArrayND[np.float64]], _LU] | Callable[[onp.ArrayND[np.complex128]], _LU]
17-
_FuncSolveLU: TypeAlias = Callable[[_LU, onp.ArrayND], onp.ArrayND[np.inexact[Any]]]
18+
_FuncSolveLU: TypeAlias = Callable[[_LU, onp.ArrayND], onp.ArrayND[npc.inexact]]
1819

1920
_ToJac: TypeAlias = onp.ToComplex2D | spmatrix | sparray
2021

scipy-stubs/integrate/_ivp/common.pyi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from collections.abc import Callable, Sequence
2-
from typing import Any, Final, Literal, TypeAlias, TypeVar, overload
2+
from typing import Final, Literal, TypeAlias, TypeVar, overload
33

44
import numpy as np
55
import optype as op
66
import optype.numpy as onp
7+
import optype.numpy.compat as npc
78

89
from .base import DenseOutput
910
from scipy.sparse import csc_matrix
@@ -15,7 +16,7 @@ _Side: TypeAlias = Literal["left", "right"]
1516
_Interpolants: TypeAlias = Sequence[DenseOutput]
1617

1718
_To1D: TypeAlias = Sequence[_SCT] | onp.CanArrayND[_SCT]
18-
_ToFloat64: TypeAlias = np.float16 | np.float32 | np.float64 | np.integer[Any] | np.bool_
19+
_ToFloat64: TypeAlias = np.float16 | np.float32 | np.float64 | npc.integer | np.bool_
1920

2021
###
2122

@@ -61,9 +62,9 @@ def validate_first_step(first_step: _ToFloatT, t0: onp.ToFloat, t_bound: onp.ToF
6162
def validate_max_step(max_step: _ToFloatT) -> _ToFloatT: ...
6263
def warn_extraneous(extraneous: dict[str, object]) -> None: ...
6364
def validate_tol(
64-
rtol: onp.ArrayND[np.floating[Any]], atol: onp.ArrayND[np.floating[Any]], n: int
65-
) -> tuple[onp.Array1D[np.floating[Any]], onp.Array1D[np.floating[Any]]]: ...
66-
def norm(x: onp.ToFloatND) -> np.floating[Any]: ...
65+
rtol: onp.ArrayND[npc.floating], atol: onp.ArrayND[npc.floating], n: int
66+
) -> tuple[onp.Array1D[npc.floating], onp.Array1D[npc.floating]]: ...
67+
def norm(x: onp.ToFloatND) -> npc.floating: ...
6768
def select_initial_step(
6869
fun: Callable[[np.float64, onp.Array1D[np.float64]], onp.Array1D[np.float64]],
6970
t0: float | np.float64,

scipy-stubs/integrate/_ivp/ivp.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from collections.abc import Callable, Sequence
2-
from typing import Any, Concatenate, Final, Generic, Literal, TypeAlias, overload, type_check_only
2+
from typing import Concatenate, Final, Generic, Literal, TypeAlias, overload, type_check_only
33
from typing_extensions import TypeVar, TypedDict, Unpack
44

55
import numpy as np
66
import optype.numpy as onp
7+
import optype.numpy.compat as npc
78

89
from .base import DenseOutput, OdeSolver
910
from .common import OdeSolution
1011
from scipy._lib._util import _RichResult
1112
from scipy._typing import Falsy, Truthy
1213
from scipy.sparse import sparray, spmatrix
1314

14-
_SCT_cf = TypeVar("_SCT_cf", bound=np.inexact[Any], default=np.float64 | np.complex128)
15+
_SCT_cf = TypeVar("_SCT_cf", bound=npc.inexact, default=np.float64 | np.complex128)
1516

1617
_FuncSol: TypeAlias = Callable[[float], onp.ArrayND[_SCT_cf]]
1718
_FuncEvent: TypeAlias = Callable[[float, onp.ArrayND[_SCT_cf]], float]

scipy-stubs/integrate/_ode.pyi

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ from typing_extensions import TypeVar, TypeVarTuple, Unpack, override
44

55
import numpy as np
66
import optype.numpy as onp
7+
import optype.numpy.compat as npc
78

89
__all__ = ["complex_ode", "ode"]
910

10-
_SCT_co = TypeVar("_SCT_co", covariant=True, bound=np.inexact[Any], default=np.float64 | np.complex128)
11+
_SCT_co = TypeVar("_SCT_co", covariant=True, bound=npc.inexact, default=np.float64 | np.complex128)
1112
_Ts = TypeVarTuple("_Ts", default=Unpack[tuple[()]])
1213

1314
@type_check_only
@@ -35,15 +36,15 @@ class _IntegratorParams(TypedDict, total=False):
3536

3637
@type_check_only
3738
class _ODEFuncF(Protocol[*_Ts]):
38-
def __call__(self, t: float, y: float | onp.ArrayND[np.float64], /, *args: *_Ts) -> float | onp.ArrayND[np.floating[Any]]: ...
39+
def __call__(self, t: float, y: float | onp.ArrayND[np.float64], /, *args: *_Ts) -> float | onp.ArrayND[npc.floating]: ...
3940

4041
@type_check_only
4142
class _ODEFuncC(Protocol[*_Ts]):
4243
def __call__(
4344
self, t: float, y: complex | onp.ArrayND[np.complex128], /, *args: *_Ts
44-
) -> complex | onp.ArrayND[np.complexfloating[Any, Any]]: ...
45+
) -> complex | onp.ArrayND[npc.complexfloating]: ...
4546

46-
_SolOutFunc: TypeAlias = Callable[[float, onp.Array1D[np.inexact[Any]]], Literal[0, -1]]
47+
_SolOutFunc: TypeAlias = Callable[[float, onp.Array1D[npc.inexact]], Literal[0, -1]]
4748

4849
###
4950

@@ -187,7 +188,7 @@ class dopri5(IntegratorBase[np.float64]):
187188
dfactor: Final[float]
188189
beta: Final[float]
189190
verbosity: Final[int]
190-
solout: Callable[[float, onp.Array1D[np.inexact[Any]]], Literal[0, -1]] | None
191+
solout: Callable[[float, onp.Array1D[npc.inexact]], Literal[0, -1]] | None
191192
solout_cmplx: bool
192193
iout: int
193194
work: onp.Array1D[np.float64]
@@ -216,7 +217,7 @@ class dopri5(IntegratorBase[np.float64]):
216217
nr: int, # unused
217218
xold: object, # unused
218219
x: float,
219-
y: onp.Array1D[np.floating[Any]],
220+
y: onp.Array1D[npc.floating],
220221
nd: int, # unused
221222
icomp: int, # unused
222223
con: object, # unused

0 commit comments

Comments
 (0)