Skip to content

Commit

Permalink
🎨 _typing: replace inline truthy/falsy literals
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenham committed Dec 31, 2024
1 parent a9a79f4 commit 5ea0d1f
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 105 deletions.
5 changes: 3 additions & 2 deletions scipy-stubs/_lib/_elementwise_iterative_method.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from collections.abc import Callable, Iterable, Mapping, Sequence
from types import ModuleType
from typing import Any, Concatenate, Final, Literal, TypeAlias
from typing import Any, Concatenate, Final, TypeAlias
from typing_extensions import TypeVar

import numpy as np
import optype as op
import optype.numpy as onp
from scipy._typing import Falsy
from ._util import _RichResult

###
Expand Down Expand Up @@ -35,7 +36,7 @@ def _initialize(
func: _FuncRealT,
xs: Sequence[onp.ToFloat1D],
args: tuple[onp.ToFloat1D, ...],
complex_ok: Literal[False] = False,
complex_ok: Falsy = False,
preserve_shape: bool | None = None,
xp: _ModuleT | None = None,
) -> tuple[
Expand Down
5 changes: 3 additions & 2 deletions scipy-stubs/cluster/hierarchy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import numpy as np
import optype.numpy as onp
import optype.typing as opt
from scipy._lib._disjoint_set import DisjointSet
from scipy._typing import Falsy, Truthy
from scipy.spatial.distance import _Metric

__all__ = [
Expand Down Expand Up @@ -141,9 +142,9 @@ def cut_tree(

#
@overload
def to_tree(Z: onp.ToArray2D, rd: Literal[False] = False) -> ClusterNode: ...
def to_tree(Z: onp.ToArray2D, rd: Falsy = False) -> ClusterNode: ...
@overload
def to_tree(Z: onp.ToArray2D, rd: Literal[True]) -> tuple[ClusterNode, list[ClusterNode]]: ...
def to_tree(Z: onp.ToArray2D, rd: Truthy) -> tuple[ClusterNode, list[ClusterNode]]: ...

#
def optimal_leaf_ordering(Z: onp.ToArray2D, y: onp.ToArrayND, metric: _Metric = "euclidean") -> _LinkageArray: ...
Expand Down
7 changes: 5 additions & 2 deletions scipy-stubs/integrate/_bvp.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ from typing_extensions import TypeVar

import numpy as np
import optype.numpy as onp
from scipy._typing import Falsy, Truthy
from scipy.interpolate import PPoly
from scipy.sparse import csc_matrix

###

_SCT_fc = TypeVar("_SCT_fc", bound=np.inexact[Any], default=np.float64 | np.complex128)

_FunRHS: TypeAlias = Callable[[onp.Array1D, onp.Array2D[_SCT_fc]], onp.ArrayND[_SCT_fc]]
Expand Down Expand Up @@ -225,7 +228,7 @@ def wrap_functions(
bc: _FunBCR[_SCT_fc],
fun_jac: _FunRHS_jac[_SCT_fc] | None,
bc_jac: _FunBCR_jac[_SCT_fc] | None,
k: Literal[False, 0],
k: Falsy,
a: onp.ToFloat,
S: onp.Array2D[np.float64] | None,
D: onp.Array2D[np.float64] | None,
Expand All @@ -237,7 +240,7 @@ def wrap_functions(
bc: _FunBCR_p[_SCT_fc],
fun_jac: _FunRHS_jac_p[_SCT_fc] | None,
bc_jac: _FunBCR_jac_p[_SCT_fc] | None,
k: Literal[True, 1],
k: Truthy,
a: onp.ToFloat,
S: onp.Array2D[np.float64] | None,
D: onp.Array2D[np.float64] | None,
Expand Down
5 changes: 3 additions & 2 deletions scipy-stubs/integrate/_ivp/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import Any, ClassVar, Final, Generic, Literal, TypeVar, overload

import numpy as np
import optype.numpy as onp
from scipy._typing import Truthy

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

Expand Down Expand Up @@ -32,7 +33,7 @@ class OdeSolver:
y0: onp.ToFloatND,
t_bound: onp.ToFloat,
vectorized: bool,
support_complex: bool = False,
support_complex: onp.ToBool = False,
) -> None: ...
@overload
def __init__(
Expand All @@ -43,7 +44,7 @@ class OdeSolver:
y0: onp.ToComplexND,
t_bound: onp.ToFloat,
vectorized: bool,
support_complex: Literal[True],
support_complex: Truthy,
) -> None: ...
@property
def step_size(self, /) -> float | None: ...
Expand Down
5 changes: 3 additions & 2 deletions scipy-stubs/integrate/_ivp/ivp.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from typing_extensions import TypedDict, TypeVar, Unpack
import numpy as np
import optype.numpy as onp
from scipy._lib._util import _RichResult
from scipy._typing import Falsy, Truthy
from scipy.sparse import sparray, spmatrix
from .base import DenseOutput, OdeSolver
from .common import OdeSolution
Expand Down Expand Up @@ -78,7 +79,7 @@ def solve_ivp(
t_eval: onp.ToFloat1D | None = None,
dense_output: bool = False,
events: _Events[_SCT_cf] | None = None,
vectorized: Literal[False, 0] = False,
vectorized: Falsy = False,
args: tuple[object, ...] | None = None,
**options: Unpack[_SolverOptions],
) -> OdeResult[_SCT_cf]: ...
Expand All @@ -92,7 +93,7 @@ def solve_ivp(
dense_output: bool = False,
events: _Events[_SCT_cf] | None = None,
*,
vectorized: Literal[True, 1],
vectorized: Truthy,
args: tuple[object, ...] | None = None,
**options: Unpack[_SolverOptions],
) -> OdeResult[_SCT_cf]: ...
33 changes: 17 additions & 16 deletions scipy-stubs/integrate/_odepack_py.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing_extensions import TypeVar, TypeVarTuple, Unpack

import numpy as np
import optype.numpy as onp
from scipy._typing import Falsy, Truthy
from ._typing import ODEInfoDict

__all__ = ["ODEintWarning", "odeint"]
Expand Down Expand Up @@ -31,7 +32,7 @@ def odeint(
args: tuple[()] = (),
Dfun: _ODEFunc[_YT] | None = None,
col_deriv: Literal[0, 1] | bool = 0,
full_output: Literal[False, 0] = 0,
full_output: Falsy = 0,
ml: int | None = None,
mu: int | None = None,
rtol: float | None = None,
Expand All @@ -46,7 +47,7 @@ def odeint(
mxordn: int = 12,
mxords: int = 5,
printmessg: Literal[0, 1] | bool = 0,
tfirst: Literal[False, 0] = False,
tfirst: Falsy = False,
) -> onp.Array2D[np.floating[Any]]: ...
@overload
def odeint(
Expand All @@ -56,7 +57,7 @@ def odeint(
args: tuple[()] = (),
Dfun: _ODEFunc[_YT] | None = None,
col_deriv: Literal[0, 1] | bool = 0,
full_output: Literal[False, 0] = 0,
full_output: Falsy = 0,
ml: int | None = None,
mu: int | None = None,
rtol: float | None = None,
Expand All @@ -72,7 +73,7 @@ def odeint(
mxords: int = 5,
printmessg: Literal[0, 1] | bool = 0,
*,
tfirst: Literal[True, 1],
tfirst: Truthy,
) -> onp.Array2D[np.floating[Any]]: ...
@overload
def odeint(
Expand All @@ -83,7 +84,7 @@ def odeint(
Dfun: _ODEFunc[_YT] | None = None,
col_deriv: Literal[0, 1] | bool = 0,
*,
full_output: Literal[True, 1],
full_output: Truthy,
ml: int | None = None,
mu: int | None = None,
rtol: float | None = None,
Expand All @@ -98,7 +99,7 @@ def odeint(
mxordn: int = 12,
mxords: int = 5,
printmessg: Literal[0, 1] | bool = 0,
tfirst: Literal[False, 0] = False,
tfirst: Falsy = False,
) -> tuple[onp.Array2D[np.floating[Any]], ODEInfoDict]: ...
@overload
def odeint(
Expand All @@ -109,7 +110,7 @@ def odeint(
Dfun: _ODEFunc[_YT] | None = None,
col_deriv: Literal[0, 1] | bool = 0,
*,
full_output: Literal[True, 1],
full_output: Truthy,
ml: int | None = None,
mu: int | None = None,
rtol: float | None = None,
Expand All @@ -124,7 +125,7 @@ def odeint(
mxordn: int = 12,
mxords: int = 5,
printmessg: Literal[0, 1] | bool = 0,
tfirst: Literal[True, 1],
tfirst: Truthy,
) -> tuple[onp.Array2D[np.floating[Any]], ODEInfoDict]: ...

# specified args
Expand All @@ -136,7 +137,7 @@ def odeint(
args: tuple[Unpack[_Ts]] = ...,
Dfun: _ODEFunc[_YT, Unpack[_Ts]] | None = None,
col_deriv: Literal[0, 1] | bool = 0,
full_output: Literal[False, 0] = 0,
full_output: Falsy = 0,
ml: int | None = None,
mu: int | None = None,
rtol: float | None = None,
Expand All @@ -151,7 +152,7 @@ def odeint(
mxordn: int = 12,
mxords: int = 5,
printmessg: Literal[0, 1] | bool = 0,
tfirst: Literal[False, 0] = False,
tfirst: Falsy = False,
) -> onp.Array2D[np.floating[Any]]: ...
@overload
def odeint(
Expand All @@ -161,7 +162,7 @@ def odeint(
args: tuple[Unpack[_Ts]] = ...,
Dfun: _ODEFuncInv[_YT, Unpack[_Ts]] | None = None,
col_deriv: Literal[0, 1] | bool = 0,
full_output: Literal[False, 0] = 0,
full_output: Falsy = 0,
ml: int | None = None,
mu: int | None = None,
rtol: float | None = None,
Expand All @@ -177,7 +178,7 @@ def odeint(
mxords: int = 5,
printmessg: Literal[0, 1] | bool = 0,
*,
tfirst: Literal[True, 1],
tfirst: Truthy,
) -> onp.Array2D[np.floating[Any]]: ...
@overload
def odeint(
Expand All @@ -188,7 +189,7 @@ def odeint(
Dfun: _ODEFunc[_YT, Unpack[_Ts]] | None = None,
col_deriv: Literal[0, 1] | bool = 0,
*,
full_output: Literal[True, 1],
full_output: Truthy,
ml: int | None = None,
mu: int | None = None,
rtol: float | None = None,
Expand All @@ -203,7 +204,7 @@ def odeint(
mxordn: int = 12,
mxords: int = 5,
printmessg: Literal[0, 1] | bool = 0,
tfirst: Literal[False, 0] = False,
tfirst: Falsy = False,
) -> tuple[onp.Array2D[np.floating[Any]], ODEInfoDict]: ...
@overload
def odeint(
Expand All @@ -214,7 +215,7 @@ def odeint(
Dfun: _ODEFuncInv[_YT, Unpack[_Ts]] | None = None,
col_deriv: Literal[0, 1] | bool = 0,
*,
full_output: Literal[True, 1],
full_output: Truthy,
ml: int | None = None,
mu: int | None = None,
rtol: float | None = None,
Expand All @@ -229,5 +230,5 @@ def odeint(
mxordn: int = 12,
mxords: int = 5,
printmessg: Literal[0, 1] | bool = 0,
tfirst: Literal[True, 1],
tfirst: Truthy,
) -> tuple[onp.Array2D[np.floating[Any]], ODEInfoDict]: ...
7 changes: 4 additions & 3 deletions scipy-stubs/linalg/_expm_frechet.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from typing import Literal, TypeAlias, overload

import numpy as np
import optype.numpy as onp
from scipy._typing import Falsy, Truthy

__all__ = ["expm_cond", "expm_frechet"]

Expand All @@ -16,15 +17,15 @@ def expm_frechet(
A: onp.ToComplex2D,
E: onp.ToComplex2D,
method: _Method | None = None,
compute_expm: Literal[True] = True,
compute_expm: Truthy = True,
check_finite: bool = True,
) -> tuple[_Float2D, _Float2D] | tuple[_Float2D | _Complex2D, _Complex2D]: ...
@overload
def expm_frechet(
A: onp.ToComplex2D,
E: onp.ToComplex2D,
method: _Method | None,
compute_expm: Literal[False],
compute_expm: Falsy,
check_finite: bool = True,
) -> tuple[_Float2D, _Float2D] | tuple[_Float2D | _Complex2D, _Complex2D]: ...
@overload
Expand All @@ -33,7 +34,7 @@ def expm_frechet(
E: onp.ToComplex2D,
method: _Method | None = None,
*,
compute_expm: Literal[False],
compute_expm: Falsy,
check_finite: bool = True,
) -> tuple[_Float2D, _Float2D] | tuple[_Float2D | _Complex2D, _Complex2D]: ...

Expand Down
6 changes: 3 additions & 3 deletions scipy-stubs/odr/_odrpack.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from typing import Any, Concatenate, Final, Literal, TypeAlias, TypedDict, overl

import numpy as np
import optype.numpy as onp
from scipy._typing import AnyBool
from scipy._typing import AnyBool, Falsy, Truthy

__all__ = ["ODR", "Data", "Model", "OdrError", "OdrStop", "OdrWarning", "Output", "RealData", "odr", "odr_error", "odr_stop"]

Expand Down Expand Up @@ -300,7 +300,7 @@ def odr(
scld: onp.ToFloatND | None = None,
work: onp.ArrayND[np.float64] | None = None,
iwork: onp.ArrayND[np.int32 | np.int64] | None = None,
full_output: Literal[False, 0] = 0,
full_output: Falsy = 0,
) -> tuple[_Float1D, _Float1D, _Float2D]: ...
@overload
def odr(
Expand Down Expand Up @@ -331,5 +331,5 @@ def odr(
work: onp.ArrayND[np.float64] | None = None,
iwork: onp.ArrayND[np.int32 | np.int64] | None = None,
*,
full_output: Literal[True, 1],
full_output: Truthy,
) -> tuple[_Float1D, _Float1D, _Float2D, _FullOutput]: ...
7 changes: 4 additions & 3 deletions scipy-stubs/optimize/_numdiff.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import Concatenate, Literal, TypeAlias, overload

import numpy as np
import optype.numpy as onp
from scipy._typing import Falsy, Truthy
from scipy.sparse import csr_matrix, sparray, spmatrix
from scipy.sparse.linalg import LinearOperator

Expand Down Expand Up @@ -34,7 +35,7 @@ def approx_derivative(
f0: _ToFloatOr1D | None = None,
bounds: tuple[_ToFloatOr1D, _ToFloatOr1D] = ...,
sparsity: None = None,
as_linear_operator: Literal[False, 0] = False,
as_linear_operator: Falsy = False,
args: tuple[object, ...] = (),
kwargs: Mapping[str, object] | None = None,
) -> _Float1D | onp.Array2D[np.float64]: ...
Expand All @@ -49,7 +50,7 @@ def approx_derivative(
bounds: tuple[_ToFloatOr1D, _ToFloatOr1D] = ...,
*,
sparsity: _Sparsity,
as_linear_operator: Literal[False, 0] = False,
as_linear_operator: Falsy = False,
args: tuple[object, ...] = (),
kwargs: Mapping[str, object] | None = None,
) -> csr_matrix: ...
Expand All @@ -64,7 +65,7 @@ def approx_derivative(
bounds: tuple[_ToFloatOr1D, _ToFloatOr1D] = ...,
sparsity: _Sparsity | None = None,
*,
as_linear_operator: Literal[True, 1],
as_linear_operator: Truthy,
args: tuple[object, ...] = (),
kwargs: Mapping[str, object] | None = None,
) -> LinearOperator: ...
Expand Down
Loading

0 comments on commit 5ea0d1f

Please sign in to comment.