Skip to content

Commit 3945a95

Browse files
authored
🎨 _typing: DRY Falsy and Truthy type aliases (#390)
2 parents 2d9f5e2 + 5ea0d1f commit 3945a95

Some content is hidden

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

56 files changed

+814
-799
lines changed

‎scipy-stubs/_lib/_elementwise_iterative_method.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from collections.abc import Callable, Iterable, Mapping, Sequence
22
from types import ModuleType
3-
from typing import Any, Concatenate, Final, Literal, TypeAlias
3+
from typing import Any, Concatenate, Final, TypeAlias
44
from typing_extensions import TypeVar
55

66
import numpy as np
77
import optype as op
88
import optype.numpy as onp
9+
from scipy._typing import Falsy
910
from ._util import _RichResult
1011

1112
###
@@ -35,7 +36,7 @@ def _initialize(
3536
func: _FuncRealT,
3637
xs: Sequence[onp.ToFloat1D],
3738
args: tuple[onp.ToFloat1D, ...],
38-
complex_ok: Literal[False] = False,
39+
complex_ok: Falsy = False,
3940
preserve_shape: bool | None = None,
4041
xp: _ModuleT | None = None,
4142
) -> tuple[

‎scipy-stubs/_typing.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ __all__ = [
2020
"ConvMode",
2121
"EnterNoneMixin",
2222
"EnterSelfMixin",
23+
"Falsy",
2324
"FileLike",
2425
"FileModeRW",
2526
"FileModeRWA",
@@ -28,6 +29,7 @@ __all__ = [
2829
"OrderCF",
2930
"OrderKACF",
3031
"ToRNG",
32+
"Truthy",
3133
"_FortranFunction",
3234
]
3335

@@ -64,6 +66,10 @@ FileLike: TypeAlias = FileName | IO[_ByteSOrStr]
6466
FileModeRW: TypeAlias = Literal["r", "w"]
6567
FileModeRWA: TypeAlias = Literal[FileModeRW, "a"]
6668

69+
# TODO(jorenham): Include `np.bool[L[False]]` once we have `numpy>=2.2`
70+
Falsy: TypeAlias = Literal[False, 0]
71+
Truthy: TypeAlias = Literal[True, 1]
72+
6773
# keep in sync with `numpy._typing._scalars`
6874
AnyBool: TypeAlias = bool | np.bool_ | Literal[0, 1]
6975

‎scipy-stubs/cluster/hierarchy.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import numpy as np
77
import optype.numpy as onp
88
import optype.typing as opt
99
from scipy._lib._disjoint_set import DisjointSet
10+
from scipy._typing import Falsy, Truthy
1011
from scipy.spatial.distance import _Metric
1112

1213
__all__ = [
@@ -141,9 +142,9 @@ def cut_tree(
141142

142143
#
143144
@overload
144-
def to_tree(Z: onp.ToArray2D, rd: Literal[False] = False) -> ClusterNode: ...
145+
def to_tree(Z: onp.ToArray2D, rd: Falsy = False) -> ClusterNode: ...
145146
@overload
146-
def to_tree(Z: onp.ToArray2D, rd: Literal[True]) -> tuple[ClusterNode, list[ClusterNode]]: ...
147+
def to_tree(Z: onp.ToArray2D, rd: Truthy) -> tuple[ClusterNode, list[ClusterNode]]: ...
147148

148149
#
149150
def optimal_leaf_ordering(Z: onp.ToArray2D, y: onp.ToArrayND, metric: _Metric = "euclidean") -> _LinkageArray: ...

‎scipy-stubs/integrate/_bvp.pyi

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

55
import numpy as np
66
import optype.numpy as onp
7+
from scipy._typing import Falsy, Truthy
78
from scipy.interpolate import PPoly
89
from scipy.sparse import csc_matrix
910

11+
###
12+
1013
_SCT_fc = TypeVar("_SCT_fc", bound=np.inexact[Any], default=np.float64 | np.complex128)
1114

1215
_FunRHS: TypeAlias = Callable[[onp.Array1D, onp.Array2D[_SCT_fc]], onp.ArrayND[_SCT_fc]]
@@ -225,7 +228,7 @@ def wrap_functions(
225228
bc: _FunBCR[_SCT_fc],
226229
fun_jac: _FunRHS_jac[_SCT_fc] | None,
227230
bc_jac: _FunBCR_jac[_SCT_fc] | None,
228-
k: Literal[False, 0],
231+
k: Falsy,
229232
a: onp.ToFloat,
230233
S: onp.Array2D[np.float64] | None,
231234
D: onp.Array2D[np.float64] | None,
@@ -237,7 +240,7 @@ def wrap_functions(
237240
bc: _FunBCR_p[_SCT_fc],
238241
fun_jac: _FunRHS_jac_p[_SCT_fc] | None,
239242
bc_jac: _FunBCR_jac_p[_SCT_fc] | None,
240-
k: Literal[True, 1],
243+
k: Truthy,
241244
a: onp.ToFloat,
242245
S: onp.Array2D[np.float64] | None,
243246
D: onp.Array2D[np.float64] | None,

‎scipy-stubs/integrate/_ivp/base.pyi

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

44
import numpy as np
55
import optype.numpy as onp
6+
from scipy._typing import Truthy
67

78
_VT = TypeVar("_VT", bound=onp.ArrayND[np.inexact[Any]], default=onp.ArrayND[np.inexact[Any]])
89

@@ -32,7 +33,7 @@ class OdeSolver:
3233
y0: onp.ToFloatND,
3334
t_bound: onp.ToFloat,
3435
vectorized: bool,
35-
support_complex: bool = False,
36+
support_complex: onp.ToBool = False,
3637
) -> None: ...
3738
@overload
3839
def __init__(
@@ -43,7 +44,7 @@ class OdeSolver:
4344
y0: onp.ToComplexND,
4445
t_bound: onp.ToFloat,
4546
vectorized: bool,
46-
support_complex: Literal[True],
47+
support_complex: Truthy,
4748
) -> None: ...
4849
@property
4950
def step_size(self, /) -> float | None: ...

‎scipy-stubs/integrate/_ivp/ivp.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from typing_extensions import TypedDict, TypeVar, Unpack
55
import numpy as np
66
import optype.numpy as onp
77
from scipy._lib._util import _RichResult
8+
from scipy._typing import Falsy, Truthy
89
from scipy.sparse import sparray, spmatrix
910
from .base import DenseOutput, OdeSolver
1011
from .common import OdeSolution
@@ -78,7 +79,7 @@ def solve_ivp(
7879
t_eval: onp.ToFloat1D | None = None,
7980
dense_output: bool = False,
8081
events: _Events[_SCT_cf] | None = None,
81-
vectorized: Literal[False, 0] = False,
82+
vectorized: Falsy = False,
8283
args: tuple[object, ...] | None = None,
8384
**options: Unpack[_SolverOptions],
8485
) -> OdeResult[_SCT_cf]: ...
@@ -92,7 +93,7 @@ def solve_ivp(
9293
dense_output: bool = False,
9394
events: _Events[_SCT_cf] | None = None,
9495
*,
95-
vectorized: Literal[True, 1],
96+
vectorized: Truthy,
9697
args: tuple[object, ...] | None = None,
9798
**options: Unpack[_SolverOptions],
9899
) -> OdeResult[_SCT_cf]: ...

‎scipy-stubs/integrate/_odepack_py.pyi

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from typing_extensions import TypeVar, TypeVarTuple, Unpack
33

44
import numpy as np
55
import optype.numpy as onp
6+
from scipy._typing import Falsy, Truthy
67
from ._typing import ODEInfoDict
78

89
__all__ = ["ODEintWarning", "odeint"]
@@ -31,7 +32,7 @@ def odeint(
3132
args: tuple[()] = (),
3233
Dfun: _ODEFunc[_YT] | None = None,
3334
col_deriv: Literal[0, 1] | bool = 0,
34-
full_output: Literal[False, 0] = 0,
35+
full_output: Falsy = 0,
3536
ml: int | None = None,
3637
mu: int | None = None,
3738
rtol: float | None = None,
@@ -46,7 +47,7 @@ def odeint(
4647
mxordn: int = 12,
4748
mxords: int = 5,
4849
printmessg: Literal[0, 1] | bool = 0,
49-
tfirst: Literal[False, 0] = False,
50+
tfirst: Falsy = False,
5051
) -> onp.Array2D[np.floating[Any]]: ...
5152
@overload
5253
def odeint(
@@ -56,7 +57,7 @@ def odeint(
5657
args: tuple[()] = (),
5758
Dfun: _ODEFunc[_YT] | None = None,
5859
col_deriv: Literal[0, 1] | bool = 0,
59-
full_output: Literal[False, 0] = 0,
60+
full_output: Falsy = 0,
6061
ml: int | None = None,
6162
mu: int | None = None,
6263
rtol: float | None = None,
@@ -72,7 +73,7 @@ def odeint(
7273
mxords: int = 5,
7374
printmessg: Literal[0, 1] | bool = 0,
7475
*,
75-
tfirst: Literal[True, 1],
76+
tfirst: Truthy,
7677
) -> onp.Array2D[np.floating[Any]]: ...
7778
@overload
7879
def odeint(
@@ -83,7 +84,7 @@ def odeint(
8384
Dfun: _ODEFunc[_YT] | None = None,
8485
col_deriv: Literal[0, 1] | bool = 0,
8586
*,
86-
full_output: Literal[True, 1],
87+
full_output: Truthy,
8788
ml: int | None = None,
8889
mu: int | None = None,
8990
rtol: float | None = None,
@@ -98,7 +99,7 @@ def odeint(
9899
mxordn: int = 12,
99100
mxords: int = 5,
100101
printmessg: Literal[0, 1] | bool = 0,
101-
tfirst: Literal[False, 0] = False,
102+
tfirst: Falsy = False,
102103
) -> tuple[onp.Array2D[np.floating[Any]], ODEInfoDict]: ...
103104
@overload
104105
def odeint(
@@ -109,7 +110,7 @@ def odeint(
109110
Dfun: _ODEFunc[_YT] | None = None,
110111
col_deriv: Literal[0, 1] | bool = 0,
111112
*,
112-
full_output: Literal[True, 1],
113+
full_output: Truthy,
113114
ml: int | None = None,
114115
mu: int | None = None,
115116
rtol: float | None = None,
@@ -124,7 +125,7 @@ def odeint(
124125
mxordn: int = 12,
125126
mxords: int = 5,
126127
printmessg: Literal[0, 1] | bool = 0,
127-
tfirst: Literal[True, 1],
128+
tfirst: Truthy,
128129
) -> tuple[onp.Array2D[np.floating[Any]], ODEInfoDict]: ...
129130

130131
# specified args
@@ -136,7 +137,7 @@ def odeint(
136137
args: tuple[Unpack[_Ts]] = ...,
137138
Dfun: _ODEFunc[_YT, Unpack[_Ts]] | None = None,
138139
col_deriv: Literal[0, 1] | bool = 0,
139-
full_output: Literal[False, 0] = 0,
140+
full_output: Falsy = 0,
140141
ml: int | None = None,
141142
mu: int | None = None,
142143
rtol: float | None = None,
@@ -151,7 +152,7 @@ def odeint(
151152
mxordn: int = 12,
152153
mxords: int = 5,
153154
printmessg: Literal[0, 1] | bool = 0,
154-
tfirst: Literal[False, 0] = False,
155+
tfirst: Falsy = False,
155156
) -> onp.Array2D[np.floating[Any]]: ...
156157
@overload
157158
def odeint(
@@ -161,7 +162,7 @@ def odeint(
161162
args: tuple[Unpack[_Ts]] = ...,
162163
Dfun: _ODEFuncInv[_YT, Unpack[_Ts]] | None = None,
163164
col_deriv: Literal[0, 1] | bool = 0,
164-
full_output: Literal[False, 0] = 0,
165+
full_output: Falsy = 0,
165166
ml: int | None = None,
166167
mu: int | None = None,
167168
rtol: float | None = None,
@@ -177,7 +178,7 @@ def odeint(
177178
mxords: int = 5,
178179
printmessg: Literal[0, 1] | bool = 0,
179180
*,
180-
tfirst: Literal[True, 1],
181+
tfirst: Truthy,
181182
) -> onp.Array2D[np.floating[Any]]: ...
182183
@overload
183184
def odeint(
@@ -188,7 +189,7 @@ def odeint(
188189
Dfun: _ODEFunc[_YT, Unpack[_Ts]] | None = None,
189190
col_deriv: Literal[0, 1] | bool = 0,
190191
*,
191-
full_output: Literal[True, 1],
192+
full_output: Truthy,
192193
ml: int | None = None,
193194
mu: int | None = None,
194195
rtol: float | None = None,
@@ -203,7 +204,7 @@ def odeint(
203204
mxordn: int = 12,
204205
mxords: int = 5,
205206
printmessg: Literal[0, 1] | bool = 0,
206-
tfirst: Literal[False, 0] = False,
207+
tfirst: Falsy = False,
207208
) -> tuple[onp.Array2D[np.floating[Any]], ODEInfoDict]: ...
208209
@overload
209210
def odeint(
@@ -214,7 +215,7 @@ def odeint(
214215
Dfun: _ODEFuncInv[_YT, Unpack[_Ts]] | None = None,
215216
col_deriv: Literal[0, 1] | bool = 0,
216217
*,
217-
full_output: Literal[True, 1],
218+
full_output: Truthy,
218219
ml: int | None = None,
219220
mu: int | None = None,
220221
rtol: float | None = None,
@@ -229,5 +230,5 @@ def odeint(
229230
mxordn: int = 12,
230231
mxords: int = 5,
231232
printmessg: Literal[0, 1] | bool = 0,
232-
tfirst: Literal[True, 1],
233+
tfirst: Truthy,
233234
) -> tuple[onp.Array2D[np.floating[Any]], ODEInfoDict]: ...

‎scipy-stubs/integrate/_quad_vec.pyi

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from typing_extensions import Never, TypeVar, override
66
import numpy as np
77
import optype as op
88
import optype.numpy as onp
9+
from scipy._typing import Falsy, Truthy
910

1011
_S = TypeVar("_S")
1112
_T = TypeVar("_T")
@@ -18,9 +19,6 @@ _FloatingND: TypeAlias = onp.ArrayND[np.floating[Any]] | _Floating
1819

1920
_Fun: TypeAlias = Callable[Concatenate[float, ...], _T] | Callable[Concatenate[np.float64, ...], _T]
2021

21-
_Falsy: TypeAlias = Literal[False, 0] | None
22-
_Truthy: TypeAlias = Literal[True, 1]
23-
2422
_Norm: TypeAlias = Literal["max", "2"]
2523
_Quadrature: TypeAlias = Literal["gk21", "gk15", "trapezoid"]
2624

@@ -89,7 +87,7 @@ def quad_vec( # scalar function, full_output=False (default)
8987
workers: onp.ToJustInt | _DoesMap = 1,
9088
points: onp.ToFloat1D | None = None,
9189
quadrature: _Quadrature | None = None,
92-
full_output: _Falsy = False,
90+
full_output: Falsy = False,
9391
*,
9492
args: tuple[object, ...] = (),
9593
) -> tuple[_Floating, float]: ...
@@ -107,7 +105,7 @@ def quad_vec(
107105
points: onp.ToFloat1D | None = None,
108106
quadrature: _Quadrature | None = None,
109107
*,
110-
full_output: _Truthy,
108+
full_output: Truthy,
111109
args: tuple[object, ...] = (),
112110
) -> tuple[np.floating[Any], float, _Bunch[np.floating[Any]]]: ...
113111
@overload # vector function, full_output=False (default)
@@ -124,7 +122,7 @@ def quad_vec(
124122
points: onp.ToFloat1D | None = None,
125123
quadrature: _Quadrature | None = None,
126124
*,
127-
full_output: _Falsy,
125+
full_output: Falsy,
128126
args: tuple[object, ...] = (),
129127
) -> tuple[onp.Array1D[np.floating[Any]], float]: ...
130128
@overload # vector function, full_output=True
@@ -141,6 +139,6 @@ def quad_vec(
141139
points: onp.ToFloat1D | None = None,
142140
quadrature: _Quadrature | None = None,
143141
*,
144-
full_output: _Truthy,
142+
full_output: Truthy,
145143
args: tuple[object, ...] = (),
146144
) -> tuple[onp.Array1D[np.floating[Any]], float, _Bunch[np.floating[Any]]]: ...

0 commit comments

Comments
 (0)