Skip to content

Commit 7c51738

Browse files
committed
🎨 _typing: DRY Falsy and Truthy type aliases
1 parent 2d9f5e2 commit 7c51738

36 files changed

+619
-675
lines changed

‎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/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]]]: ...

‎scipy-stubs/integrate/_quadpack_py.pyi

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import numpy as np
66
import optype as op
77
import optype.numpy as onp
88
from scipy._lib._ccallback import LowLevelCallable
9+
from scipy._typing import Falsy, Truthy
910
from ._typing import QuadInfoDict, QuadOpts, QuadWeights
1011

1112
__all__ = ["IntegrationWarning", "dblquad", "nquad", "quad", "tplquad"]
@@ -15,9 +16,6 @@ _T_co = TypeVar("_T_co", covariant=True)
1516
_T_f_contra = TypeVar("_T_f_contra", contravariant=True, default=float)
1617
_BT_co = TypeVar("_BT_co", bound=bool, covariant=True, default=bool)
1718

18-
_Falsy: TypeAlias = Literal[False, 0]
19-
_Truthy: TypeAlias = Literal[True, 1]
20-
2119
# NOTE: Technically `integer[Any]` and `bool_` are also allowed, but there's no valid usecase for that.
2220
_IntLike: TypeAlias = int | np.integer[Any]
2321
_FloatLike: TypeAlias = float | np.floating[Any]
@@ -133,7 +131,7 @@ def quad(
133131
a: onp.ToFloat,
134132
b: onp.ToFloat,
135133
args: tuple[()] = (),
136-
full_output: _Falsy = 0,
134+
full_output: Falsy = 0,
137135
epsabs: _FloatLike = 1.49e-08,
138136
epsrel: _FloatLike = 1.49e-08,
139137
limit: _IntLike = 50,
@@ -143,15 +141,15 @@ def quad(
143141
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None = None,
144142
maxp1: _IntLike = 50,
145143
limlst: _IntLike = 50,
146-
complex_func: _Falsy = False,
144+
complex_func: Falsy = False,
147145
) -> tuple[float, float]: ...
148146
@overload
149147
def quad(
150148
func: _QuadFunc1N[_FloatLike],
151149
a: onp.ToFloat,
152150
b: onp.ToFloat,
153151
args: tuple[object, ...],
154-
full_output: _Falsy = 0,
152+
full_output: Falsy = 0,
155153
epsabs: _FloatLike = 1.49e-08,
156154
epsrel: _FloatLike = 1.49e-08,
157155
limit: _IntLike = 50,
@@ -161,15 +159,15 @@ def quad(
161159
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None = None,
162160
maxp1: _IntLike = 50,
163161
limlst: _IntLike = 50,
164-
complex_func: _Falsy = False,
162+
complex_func: Falsy = False,
165163
) -> tuple[float, float]: ...
166164
@overload
167165
def quad(
168166
func: _QuadFunc10[_FloatLike],
169167
a: onp.ToFloat,
170168
b: onp.ToFloat,
171169
args: tuple[()],
172-
full_output: _Truthy,
170+
full_output: Truthy,
173171
epsabs: _FloatLike = 1.49e-08,
174172
epsrel: _FloatLike = 1.49e-08,
175173
limit: _IntLike = 50,
@@ -179,7 +177,7 @@ def quad(
179177
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None = None,
180178
maxp1: _IntLike = 50,
181179
limlst: _IntLike = 50,
182-
complex_func: _Falsy = False,
180+
complex_func: Falsy = False,
183181
) -> (
184182
tuple[float, float, QuadInfoDict]
185183
| tuple[float, float, QuadInfoDict, str]
@@ -192,7 +190,7 @@ def quad(
192190
b: onp.ToFloat,
193191
args: tuple[()] = (),
194192
*,
195-
full_output: _Truthy,
193+
full_output: Truthy,
196194
epsabs: _FloatLike = 1.49e-08,
197195
epsrel: _FloatLike = 1.49e-08,
198196
limit: _IntLike = 50,
@@ -202,7 +200,7 @@ def quad(
202200
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None = None,
203201
maxp1: _IntLike = 50,
204202
limlst: _IntLike = 50,
205-
complex_func: _Falsy = False,
203+
complex_func: Falsy = False,
206204
) -> (
207205
tuple[float, float, QuadInfoDict]
208206
| tuple[float, float, QuadInfoDict, str]
@@ -214,7 +212,7 @@ def quad(
214212
a: onp.ToFloat,
215213
b: onp.ToFloat,
216214
args: tuple[object, ...],
217-
full_output: _Truthy,
215+
full_output: Truthy,
218216
epsabs: _FloatLike = 1.49e-08,
219217
epsrel: _FloatLike = 1.49e-08,
220218
limit: _IntLike = 50,
@@ -224,7 +222,7 @@ def quad(
224222
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None = None,
225223
maxp1: _IntLike = 50,
226224
limlst: _IntLike = 50,
227-
complex_func: _Falsy = False,
225+
complex_func: Falsy = False,
228226
) -> (
229227
tuple[float, float, QuadInfoDict]
230228
| tuple[float, float, QuadInfoDict, str]
@@ -236,7 +234,7 @@ def quad(
236234
a: onp.ToComplex,
237235
b: onp.ToComplex,
238236
args: tuple[()],
239-
full_output: _Falsy,
237+
full_output: Falsy,
240238
epsabs: _FloatLike,
241239
epsrel: _FloatLike,
242240
limit: _IntLike,
@@ -246,15 +244,15 @@ def quad(
246244
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None,
247245
maxp1: _IntLike,
248246
limlst: _IntLike,
249-
complex_func: _Truthy,
247+
complex_func: Truthy,
250248
) -> tuple[complex, complex]: ...
251249
@overload
252250
def quad(
253251
func: _QuadFunc10[_ComplexLike],
254252
a: onp.ToComplex,
255253
b: onp.ToComplex,
256254
args: tuple[()] = (),
257-
full_output: _Falsy = 0,
255+
full_output: Falsy = 0,
258256
epsabs: _FloatLike = 1.49e-08,
259257
epsrel: _FloatLike = 1.49e-08,
260258
limit: _IntLike = 50,
@@ -265,15 +263,15 @@ def quad(
265263
maxp1: _IntLike = 50,
266264
limlst: _IntLike = 50,
267265
*,
268-
complex_func: _Truthy,
266+
complex_func: Truthy,
269267
) -> tuple[complex, complex]: ...
270268
@overload
271269
def quad(
272270
func: _QuadFunc10[_ComplexLike],
273271
a: onp.ToComplex,
274272
b: onp.ToComplex,
275273
args: tuple[()],
276-
full_output: _Truthy,
274+
full_output: Truthy,
277275
epsabs: _FloatLike,
278276
epsrel: _FloatLike,
279277
limit: _IntLike,
@@ -283,15 +281,15 @@ def quad(
283281
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None,
284282
maxp1: _IntLike,
285283
limlst: _IntLike,
286-
complex_func: _Truthy,
284+
complex_func: Truthy,
287285
) -> tuple[complex, complex, _QuadComplexFullOutput]: ...
288286
@overload
289287
def quad(
290288
func: _QuadFunc10[_ComplexLike],
291289
a: onp.ToComplex,
292290
b: onp.ToComplex,
293291
args: tuple[()],
294-
full_output: _Truthy,
292+
full_output: Truthy,
295293
epsabs: _FloatLike = 1.49e-08,
296294
epsrel: _FloatLike = 1.49e-08,
297295
limit: _IntLike = 50,
@@ -302,7 +300,7 @@ def quad(
302300
maxp1: _IntLike = 50,
303301
limlst: _IntLike = 50,
304302
*,
305-
complex_func: _Truthy,
303+
complex_func: Truthy,
306304
) -> tuple[complex, complex, _QuadComplexFullOutput]: ...
307305
@overload
308306
def quad(
@@ -311,7 +309,7 @@ def quad(
311309
b: onp.ToComplex,
312310
args: tuple[()] = (),
313311
*,
314-
full_output: _Truthy,
312+
full_output: Truthy,
315313
epsabs: _FloatLike = 1.49e-08,
316314
epsrel: _FloatLike = 1.49e-08,
317315
limit: _IntLike = 50,
@@ -321,15 +319,15 @@ def quad(
321319
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None = None,
322320
maxp1: _IntLike = 50,
323321
limlst: _IntLike = 50,
324-
complex_func: _Truthy,
322+
complex_func: Truthy,
325323
) -> tuple[complex, complex, _QuadComplexFullOutput]: ...
326324
@overload
327325
def quad(
328326
func: _QuadFunc1N[_ComplexLike],
329327
a: onp.ToComplex,
330328
b: onp.ToComplex,
331329
args: tuple[object, ...],
332-
full_output: _Falsy,
330+
full_output: Falsy,
333331
epsabs: _FloatLike,
334332
epsrel: _FloatLike,
335333
limit: _IntLike,
@@ -339,15 +337,15 @@ def quad(
339337
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None,
340338
maxp1: _IntLike,
341339
limlst: _IntLike,
342-
complex_func: _Truthy,
340+
complex_func: Truthy,
343341
) -> tuple[complex, complex]: ...
344342
@overload
345343
def quad(
346344
func: _QuadFunc1N[_ComplexLike],
347345
a: onp.ToComplex,
348346
b: onp.ToComplex,
349347
args: tuple[object, ...],
350-
full_output: _Falsy = 0,
348+
full_output: Falsy = 0,
351349
epsabs: _FloatLike = 1.49e-08,
352350
epsrel: _FloatLike = 1.49e-08,
353351
limit: _IntLike = 50,
@@ -358,15 +356,15 @@ def quad(
358356
maxp1: _IntLike = 50,
359357
limlst: _IntLike = 50,
360358
*,
361-
complex_func: _Truthy,
359+
complex_func: Truthy,
362360
) -> tuple[complex, complex]: ...
363361
@overload
364362
def quad(
365363
func: _QuadFunc1N[_ComplexLike],
366364
a: onp.ToComplex,
367365
b: onp.ToComplex,
368366
args: tuple[object, ...],
369-
full_output: _Truthy,
367+
full_output: Truthy,
370368
epsabs: _FloatLike,
371369
epsrel: _FloatLike,
372370
limit: _IntLike,
@@ -376,15 +374,15 @@ def quad(
376374
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]] | None,
377375
maxp1: _IntLike,
378376
limlst: _IntLike,
379-
complex_func: _Truthy,
377+
complex_func: Truthy,
380378
) -> tuple[complex, complex, _QuadComplexFullOutput]: ...
381379
@overload
382380
def quad(
383381
func: _QuadFunc1N[_ComplexLike],
384382
a: onp.ToComplex,
385383
b: onp.ToComplex,
386384
args: tuple[object, ...],
387-
full_output: _Truthy,
385+
full_output: Truthy,
388386
epsabs: _FloatLike = 1.49e-08,
389387
epsrel: _FloatLike = 1.49e-08,
390388
limit: _IntLike = 50,
@@ -395,7 +393,7 @@ def quad(
395393
maxp1: _IntLike = 50,
396394
limlst: _IntLike = 50,
397395
*,
398-
complex_func: _Truthy,
396+
complex_func: Truthy,
399397
) -> tuple[complex, complex, _QuadComplexFullOutput]: ...
400398

401399
# 2-dimensional quadrature
@@ -460,15 +458,15 @@ def nquad(
460458
ranges: _SizedIterable[_QuadRange | _RangeCallable[float]],
461459
args: Iterable[object] | None = None,
462460
opts: QuadOpts | Callable[..., QuadOpts] | Iterable[QuadOpts | Callable[..., QuadOpts]] | None = None,
463-
full_output: _Falsy = False,
461+
full_output: Falsy = False,
464462
) -> tuple[float, float]: ...
465463
@overload
466464
def nquad(
467465
func: _QuadFuncN,
468466
ranges: _SizedIterable[_QuadRange | _RangeCallable[float]],
469467
args: Iterable[object] | None,
470468
opts: QuadOpts | _OptCallable | Iterable[QuadOpts | _OptCallable] | None,
471-
full_output: _Truthy,
469+
full_output: Truthy,
472470
) -> tuple[float, float, _QuadOutputNC]: ...
473471
@overload
474472
def nquad(
@@ -477,5 +475,5 @@ def nquad(
477475
args: Iterable[object] | None = None,
478476
opts: QuadOpts | _OptCallable | Iterable[QuadOpts | _OptCallable] | None = None,
479477
*,
480-
full_output: _Truthy,
478+
full_output: Truthy,
481479
) -> tuple[float, float, _QuadOutputNC]: ...

0 commit comments

Comments
 (0)