Skip to content

Commit 337b0a7

Browse files
authored
🎨 optype 0.9 simplifications (#424)
2 parents f681e55 + 158316d commit 337b0a7

18 files changed

+86
-92
lines changed

‎scipy-stubs/_lib/_util.pyi

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import multiprocessing.pool as mpp
22
import types
33
from collections.abc import Callable, Iterable, Mapping, Sequence
4-
from typing import Any, Concatenate, Final, Generic, NamedTuple, TypeAlias, overload
4+
from typing import Concatenate, Final, Generic, NamedTuple, TypeAlias, overload
55
from typing_extensions import TypeVar, override
66

77
import numpy as np
88
import optype as op
99
import optype.numpy as onp
10+
import optype.numpy.compat as npc
1011
from scipy._typing import RNG, EnterSelfMixin
1112

1213
_AnyRNG = TypeVar("_AnyRNG", np.random.RandomState, np.random.Generator)
@@ -16,16 +17,16 @@ _T_co = TypeVar("_T_co", covariant=True, default=object)
1617
_T_contra = TypeVar("_T_contra", contravariant=True, default=object)
1718
_VT = TypeVar("_VT")
1819
_RT = TypeVar("_RT")
19-
_AxisT = TypeVar("_AxisT", bound=np.integer[Any])
20+
_AxisT = TypeVar("_AxisT", bound=npc.integer)
2021

2122
###
2223

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

27-
IntNumber: TypeAlias = int | np.integer[Any]
28-
DecimalNumber: TypeAlias = float | np.floating[Any] | np.integer[Any]
28+
IntNumber: TypeAlias = int | npc.integer
29+
DecimalNumber: TypeAlias = float | npc.floating | npc.integer
2930

3031
_RNG: TypeAlias = np.random.Generator | np.random.RandomState
3132
SeedType: TypeAlias = IntNumber | _RNG | None
@@ -89,7 +90,7 @@ def getfullargspec_no_self(func: Callable[..., object]) -> FullArgSpec: ...
8990
@overload
9091
def check_random_state(seed: _AnyRNG) -> _AnyRNG: ...
9192
@overload
92-
def check_random_state(seed: int | np.integer[Any] | types.ModuleType | None) -> np.random.RandomState: ...
93+
def check_random_state(seed: int | npc.integer | types.ModuleType | None) -> np.random.RandomState: ...
9394

9495
#
9596
@overload
@@ -100,7 +101,7 @@ def rng_integers(
100101
size: tuple[()] | None = None,
101102
dtype: onp.AnyIntegerDType = "int64",
102103
endpoint: op.CanBool = False,
103-
) -> np.integer[Any]: ...
104+
) -> npc.integer: ...
104105
@overload
105106
def rng_integers(
106107
gen: RNG | None,
@@ -109,7 +110,7 @@ def rng_integers(
109110
size: op.CanIndex | Sequence[op.CanIndex] | None = None,
110111
dtype: onp.AnyIntegerDType = "int64",
111112
endpoint: op.CanBool = False,
112-
) -> np.integer[Any] | onp.ArrayND[np.integer[Any]]: ...
113+
) -> npc.integer | onp.ArrayND[npc.integer]: ...
113114

114115
#
115116
@overload

‎scipy-stubs/io/_fortran.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from typing import Any, TypedDict, final, overload, type_check_only
1+
from typing import TypedDict, final, overload, type_check_only
22
from typing_extensions import Unpack
33

44
import numpy as np
55
import numpy.typing as npt
66
import optype.numpy as onp
7+
import optype.numpy.compat as npc
78
from scipy._typing import EnterSelfMixin, FileLike, FileModeRW
89

910
__all__ = ["FortranEOFError", "FortranFile", "FortranFormattingError"]
@@ -27,12 +28,12 @@ class FortranFile(EnterSelfMixin):
2728
@overload
2829
def read_ints(self, /) -> onp.Array1D[np.int32]: ...
2930
@overload
30-
def read_ints(self, /, dtype: onp.AnyIntegerDType) -> onp.Array1D[np.integer[Any]]: ...
31+
def read_ints(self, /, dtype: onp.AnyIntegerDType) -> onp.Array1D[npc.integer]: ...
3132
@overload
3233
def read_ints(self, /, dtype: npt.DTypeLike) -> onp.Array1D: ...
3334
@overload
3435
def read_reals(self, /) -> onp.Array1D[np.float64]: ...
3536
@overload
36-
def read_reals(self, /, dtype: onp.AnyFloatingDType) -> onp.Array1D[np.floating[Any]]: ...
37+
def read_reals(self, /, dtype: onp.AnyFloatingDType) -> onp.Array1D[npc.floating]: ...
3738
@overload
3839
def read_reals(self, /, dtype: npt.DTypeLike) -> onp.Array1D: ...

‎scipy-stubs/linalg/_special_matrices.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ _To1D: TypeAlias = Sequence[_To0D[_SCT]] | onp.CanArrayND[_SCT]
4848
_ToStrict1D: TypeAlias = Sequence[_To0D[_SCT]] | onp.CanArray1D[_SCT]
4949
_ToStrict2ND: TypeAlias = onp.SequenceND[_To1D[_SCT]] | onp.CanArrayND[_SCT, onp.AtLeast2D]
5050
_ToND: TypeAlias = onp.SequenceND[_To0D[_SCT]] | onp.SequenceND[_To1D[_SCT]] | onp.CanArrayND[_SCT]
51-
_ToDType: TypeAlias = type[_SCT] | np.dtype[_SCT] | onp.HasDType[np.dtype[_SCT]]
5251

5352
_ToJustIntStrict2ND: TypeAlias = onp.SequenceND[onp.ToJustInt1D] | onp.CanArrayND[npc.integer, onp.AtLeast2D]
5453
_ToJustFloatStrict2ND: TypeAlias = onp.SequenceND[onp.ToJustFloat1D] | onp.CanArrayND[npc.floating, onp.AtLeast2D]
@@ -255,7 +254,7 @@ def hadamard(n: onp.ToInt, dtype: type[op.JustFloat]) -> _Float2D: ...
255254
@overload
256255
def hadamard(n: onp.ToInt, dtype: type[op.JustComplex]) -> _Complex2D: ...
257256
@overload
258-
def hadamard(n: onp.ToInt, dtype: _ToDType[_SCT]) -> onp.Array2D[_SCT]: ...
257+
def hadamard(n: onp.ToInt, dtype: onp.ToDType[_SCT]) -> onp.Array2D[_SCT]: ...
259258
@overload
260259
def hadamard(n: onp.ToInt, dtype: npt.DTypeLike = ...) -> onp.Array2D: ...
261260

‎scipy-stubs/optimize/_trustregion_constr/qp_subproblem.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ _ScalarFloat_co: TypeAlias = np.floating[Any] | _ScalarInt_co
2727

2828
_ScalarLikeInt_co: TypeAlias = int | _ScalarInt_co
2929
_ScalarLikeFloat_co: TypeAlias = float | _ScalarFloat_co
30-
_VectorLikeFloat_co: TypeAlias = Sequence[_ScalarLikeFloat_co] | onp.CanArray[tuple[int], np.dtype[_ScalarFloat_co]]
30+
_VectorLikeFloat_co: TypeAlias = Sequence[_ScalarLikeFloat_co] | onp.CanArray1D[_ScalarFloat_co]
3131

3232
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
3333
_SCT_float = TypeVar("_SCT_float", bound=_ScalarFloat_co)

‎scipy-stubs/sparse/_bsr.pyi

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ __all__ = ["bsr_array", "bsr_matrix", "isspmatrix_bsr"]
1616
_T = TypeVar("_T")
1717
_SCT = TypeVar("_SCT", bound=Scalar, default=Any)
1818

19-
_ToDType: TypeAlias = type[_SCT] | np.dtype[_SCT] | onp.HasDType[np.dtype[_SCT]]
2019
_ToMatrix: TypeAlias = _spbase[_SCT] | onp.CanArrayND[_SCT] | Sequence[onp.CanArrayND[_SCT]] | _ToMatrixPy[_SCT]
2120
_ToMatrixPy: TypeAlias = Sequence[_T] | Sequence[Sequence[_T]]
2221

@@ -82,7 +81,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
8281
/,
8382
arg1: _ToMatrixPy[op.JustInt],
8483
shape: ToShape2d | None = None,
85-
dtype: type[op.JustInt] | onp.AnyIntPDType | None = None,
84+
dtype: onp.AnyIntDType | None = None,
8685
copy: bool = False,
8786
blocksize: tuple[int, int] | None = None,
8887
*,
@@ -94,7 +93,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
9493
/,
9594
arg1: _ToMatrixPy[op.JustFloat],
9695
shape: ToShape2d | None = None,
97-
dtype: type[op.JustFloat] | onp.AnyFloat64DType | None = None,
96+
dtype: onp.AnyFloat64DType | None = None,
9897
copy: bool = False,
9998
blocksize: tuple[int, int] | None = None,
10099
*,
@@ -106,7 +105,7 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
106105
/,
107106
arg1: _ToMatrixPy[op.JustComplex],
108107
shape: ToShape2d | None = None,
109-
dtype: type[op.JustComplex] | onp.AnyComplex128DType | None = None,
108+
dtype: onp.AnyComplex128DType | None = None,
110109
copy: bool = False,
111110
blocksize: tuple[int, int] | None = None,
112111
*,
@@ -118,7 +117,6 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
118117
/,
119118
arg1: onp.ToComplexND,
120119
shape: ToShape2d | None,
121-
dtype: _ToDType[_SCT],
122120
copy: bool = False,
123121
blocksize: tuple[int, int] | None = None,
124122
*,
@@ -131,7 +129,6 @@ class _bsr_base(_cs_matrix[_SCT, tuple[int, int]], _minmax_mixin[_SCT, tuple[int
131129
arg1: onp.ToComplexND,
132130
shape: ToShape2d | None = None,
133131
*,
134-
dtype: _ToDType[_SCT],
135132
copy: bool = False,
136133
blocksize: tuple[int, int] | None = None,
137134
maxprint: int | None = None,

‎scipy-stubs/sparse/_dia.pyi

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ __all__ = ["dia_array", "dia_matrix", "isspmatrix_dia"]
1515
_T = TypeVar("_T")
1616
_SCT = TypeVar("_SCT", bound=Scalar, default=Any)
1717

18-
_ToDType: TypeAlias = type[_SCT] | np.dtype[_SCT] | onp.HasDType[np.dtype[_SCT]]
1918
_ToMatrix: TypeAlias = _spbase[_SCT] | onp.CanArrayND[_SCT] | Sequence[onp.CanArrayND[_SCT]] | _ToMatrixPy[_SCT]
2019
_ToMatrixPy: TypeAlias = Sequence[_T] | Sequence[Sequence[_T]]
2120
_ToData: TypeAlias = tuple[onp.ArrayND[_SCT], onp.ArrayND[Int]]
@@ -76,7 +75,7 @@ class _dia_base(_data_matrix[_SCT, tuple[int, int]], Generic[_SCT]):
7675
/,
7776
arg1: _ToMatrixPy[op.JustInt],
7877
shape: ToShape2d | None = None,
79-
dtype: type[op.JustInt] | onp.AnyIntPDType | None = None,
78+
dtype: onp.AnyIntDType | None = None,
8079
copy: bool = False,
8180
*,
8281
maxprint: int | None = None,
@@ -87,7 +86,7 @@ class _dia_base(_data_matrix[_SCT, tuple[int, int]], Generic[_SCT]):
8786
/,
8887
arg1: _ToMatrixPy[op.JustFloat],
8988
shape: ToShape2d | None = None,
90-
dtype: type[op.JustFloat] | onp.AnyFloat64DType | None = None,
89+
dtype: onp.AnyFloat64DType | None = None,
9190
copy: bool = False,
9291
*,
9392
maxprint: int | None = None,
@@ -98,7 +97,7 @@ class _dia_base(_data_matrix[_SCT, tuple[int, int]], Generic[_SCT]):
9897
/,
9998
arg1: _ToMatrixPy[op.JustComplex],
10099
shape: ToShape2d | None = None,
101-
dtype: type[op.JustComplex] | onp.AnyComplex128DType | None = None,
100+
dtype: onp.AnyComplex128DType | None = None,
102101
copy: bool = False,
103102
*,
104103
maxprint: int | None = None,
@@ -109,7 +108,7 @@ class _dia_base(_data_matrix[_SCT, tuple[int, int]], Generic[_SCT]):
109108
/,
110109
arg1: onp.ToComplexND,
111110
shape: ToShape2d | None,
112-
dtype: _ToDType[_SCT],
111+
dtype: onp.ToDType[_SCT],
113112
copy: bool = False,
114113
*,
115114
maxprint: int | None = None,
@@ -121,7 +120,7 @@ class _dia_base(_data_matrix[_SCT, tuple[int, int]], Generic[_SCT]):
121120
arg1: onp.ToComplexND,
122121
shape: ToShape2d | None = None,
123122
*,
124-
dtype: _ToDType[_SCT],
123+
dtype: onp.ToDType[_SCT],
125124
copy: bool = False,
126125
maxprint: int | None = None,
127126
) -> None: ...

‎scipy-stubs/sparse/_dok.pyi

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ _ShapeT_co = TypeVar("_ShapeT_co", bound=ShapeDOK, default=ShapeDOK, covariant=T
2525
_1D: TypeAlias = tuple[int] # noqa: PYI042
2626
_2D: TypeAlias = tuple[int, int] # noqa: PYI042
2727

28-
_ToDType: TypeAlias = type[_SCT] | np.dtype[_SCT] | onp.HasDType[np.dtype[_SCT]]
2928
_ToMatrix: TypeAlias = _spbase[_SCT] | onp.CanArrayND[_SCT] | Sequence[onp.CanArrayND[_SCT]] | _ToMatrixPy[_SCT]
3029
_ToMatrixPy: TypeAlias = Sequence[_T] | Sequence[Sequence[_T]]
3130

@@ -90,7 +89,7 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
9089
/,
9190
arg1: _ToMatrixPy[op.JustInt],
9291
shape: ToShape1dNd | None = None,
93-
dtype: type[op.JustInt] | onp.AnyIntPDType | None = None,
92+
dtype: onp.AnyIntDType | None = None,
9493
copy: bool = False,
9594
*,
9695
maxprint: int | None = None,
@@ -101,7 +100,7 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
101100
/,
102101
arg1: _ToMatrixPy[op.JustFloat],
103102
shape: ToShape1dNd | None = None,
104-
dtype: type[op.JustFloat] | onp.AnyFloat64DType | None = None,
103+
dtype: onp.AnyFloat64DType | None = None,
105104
copy: bool = False,
106105
*,
107106
maxprint: int | None = None,
@@ -112,7 +111,7 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
112111
/,
113112
arg1: _ToMatrixPy[op.JustComplex],
114113
shape: ToShape1dNd | None = None,
115-
dtype: type[op.JustComplex] | onp.AnyComplex128DType | None = None,
114+
dtype: onp.AnyComplex128DType | None = None,
116115
copy: bool = False,
117116
*,
118117
maxprint: int | None = None,
@@ -123,7 +122,6 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
123122
/,
124123
arg1: onp.ToComplexND,
125124
shape: ToShape1dNd | None,
126-
dtype: _ToDType[_SCT],
127125
copy: bool = False,
128126
*,
129127
maxprint: int | None = None,
@@ -135,7 +133,6 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
135133
arg1: onp.ToComplexND,
136134
shape: ToShape1dNd | None = None,
137135
*,
138-
dtype: _ToDType[_SCT],
139136
copy: bool = False,
140137
maxprint: int | None = None,
141138
) -> None: ...

‎scipy-stubs/sparse/_lil.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ __all__ = ["isspmatrix_lil", "lil_array", "lil_matrix"]
1717
_T = TypeVar("_T")
1818
_SCT = TypeVar("_SCT", bound=Scalar, default=Any)
1919

20-
_ToDType: TypeAlias = type[_SCT] | np.dtype[_SCT] | onp.HasDType[np.dtype[_SCT]]
2120
_ToMatrix: TypeAlias = _spbase[_SCT] | onp.CanArrayND[_SCT] | Sequence[onp.CanArrayND[_SCT]] | _ToMatrixPy[_SCT]
2221
_ToMatrixPy: TypeAlias = Sequence[_T] | Sequence[Sequence[_T]]
2322

@@ -111,7 +110,7 @@ class _lil_base(_spbase[_SCT, tuple[int, int]], IndexMixin[_SCT, tuple[int, int]
111110
/,
112111
arg1: onp.ToComplexND,
113112
shape: ToShape2d | None,
114-
dtype: _ToDType[_SCT],
113+
dtype: onp.ToDType[_SCT],
115114
copy: bool = False,
116115
*,
117116
maxprint: int | None = None,
@@ -123,7 +122,7 @@ class _lil_base(_spbase[_SCT, tuple[int, int]], IndexMixin[_SCT, tuple[int, int]
123122
arg1: onp.ToComplexND,
124123
shape: ToShape2d | None = None,
125124
*,
126-
dtype: _ToDType[_SCT],
125+
dtype: onp.ToDType[_SCT],
127126
copy: bool = False,
128127
maxprint: int | None = None,
129128
) -> None: ...

‎scipy-stubs/sparse/_matrix_io.pyi

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Final, Literal, Protocol, TypeAlias, TypedDict, TypeVar, overload, type_check_only
1+
from typing import Final, Literal, Protocol, TypeAlias, TypedDict, type_check_only
22

33
import optype as op
44
from ._bsr import bsr_array, bsr_matrix
@@ -10,12 +10,8 @@ from ._dia import dia_array, dia_matrix
1010

1111
__all__ = ["load_npz", "save_npz"]
1212

13-
_StrOrBytesT = TypeVar("_StrOrBytesT", bound=bytes | str, default=bytes | str)
14-
_StrOrBytesT_co = TypeVar("_StrOrBytesT_co", bound=bytes | str, default=bytes | str, covariant=True)
15-
16-
@type_check_only
17-
class _CanWriteBytes(Protocol):
18-
def write(self, s: bytes, /) -> object: ...
13+
_DataArrayOut: TypeAlias = bsr_array | coo_array | csc_array | csr_array | dia_array
14+
_DataMatrixOut: TypeAlias = bsr_matrix | coo_matrix | csc_matrix | csr_matrix | dia_matrix
1915

2016
@type_check_only
2117
class _CanReadAndSeekBytes(Protocol):
@@ -26,25 +22,9 @@ class _CanReadAndSeekBytes(Protocol):
2622
class _PickleKwargs(TypedDict):
2723
allow_pickle: Literal[False]
2824

29-
# A superior version of `os.PathLike` that's actually valid:
30-
# Never parametrize generic types with "a TypeVar with constraints"!
31-
@type_check_only
32-
class _CanFSPath(Protocol[_StrOrBytesT_co]):
33-
@overload
34-
def __fspath__(self: _CanFSPath[str], /) -> str: ...
35-
@overload
36-
def __fspath__(self: _CanFSPath[bytes], /) -> bytes: ...
37-
@overload
38-
def __fspath__(self: _CanFSPath[bytes | str], /) -> bytes | str: ...
39-
40-
_ToPath: TypeAlias = _StrOrBytesT | _CanFSPath[_StrOrBytesT]
41-
42-
_DataArrayOut: TypeAlias = bsr_array | coo_array | csc_array | csr_array | dia_array
43-
_DataMatrixOut: TypeAlias = bsr_matrix | coo_matrix | csc_matrix | csr_matrix | dia_matrix
44-
4525
###
4626

4727
PICKLE_KWARGS: Final[_PickleKwargs] = ...
4828

49-
def load_npz(file: _ToPath | _CanReadAndSeekBytes) -> _DataArrayOut | _DataMatrixOut: ...
50-
def save_npz(file: _ToPath[str] | _CanWriteBytes, matrix: _data_matrix, compressed: op.CanBool = True) -> None: ...
29+
def load_npz(file: op.io.ToPath | _CanReadAndSeekBytes) -> _DataArrayOut | _DataMatrixOut: ...
30+
def save_npz(file: op.io.ToPath[str] | op.io.CanWrite[bytes], matrix: _data_matrix, compressed: op.CanBool = True) -> None: ...

‎scipy-stubs/sparse/_typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ SPFormat: TypeAlias = Literal["bsr", "coo", "csc", "csr", "dia", "dok", "lil"]
5454
_SCT = TypeVar("_SCT", bound=np.generic)
5555
ToDType: TypeAlias = type[_SCT] | np.dtype[_SCT] | onp.HasDType[np.dtype[_SCT]]
5656
ToDTypeBool: TypeAlias = onp.AnyBoolDType
57-
ToDTypeInt: TypeAlias = type[op.JustInt] | onp.AnyIntDType # see https://github.com/jorenham/optype/issues/235
57+
ToDTypeInt: TypeAlias = onp.AnyIntDType
5858
ToDTypeFloat: TypeAlias = onp.AnyFloat64DType
5959
ToDTypeComplex: TypeAlias = onp.AnyComplex128DType
6060

0 commit comments

Comments
 (0)