-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
typing.py
167 lines (131 loc) · 4.1 KB
/
typing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""Typing utilities meant for internal usage.""" # noqa: A005
# pyright: reportPrivateUsage=false
# ruff: noqa: PLC2701
from __future__ import annotations
from collections.abc import Callable, Sequence
from typing import (
Any,
Literal,
ParamSpec,
Protocol,
TypeAlias,
TypeVar,
TypedDict,
overload,
)
import numpy as np
import optype.numpy as onp
from scipy.stats._distn_infrastructure import (
_ShapeInfo as ShapeInfo,
rv_continuous,
rv_frozen,
rv_generic,
)
__all__ = [
"Callable2",
"Floating",
"Integer",
"LComomentOptions",
"LMomentOptions",
"OrderReshape",
"QuadOptions",
"ShapeInfo",
"SortKind",
"ToAWeights",
"ToFWeights",
"ToIntTrim",
"ToOrder0D",
"ToOrder1D",
"ToOrderND",
"ToTrim",
"rv_continuous",
"rv_frozen",
"rv_generic",
]
def __dir__() -> list[str]:
return __all__
Integer: TypeAlias = np.integer[Any]
Floating: TypeAlias = np.floating[Any]
OrderReshape: TypeAlias = Literal["C", "F", "A"]
"""Type of the `order` parameter of e.g. [`np.reshape`][numpy.array]."""
# matches `_SortKind` in `numpy/__init__.pyi` and `numpy >= 2.1`
SortKind: TypeAlias = Literal[
"Q", "quick", "quicksort",
"M", "merge", "mergesort",
"H", "heap", "heapsort",
"S", "stable", "stablesort",
] # fmt: skip
"""
Type of the `kind` parameter of e.g. [`np.sort`][numpy.sort], as
allowed by numpy's own stubs.
Note that the actual implementation just looks at `kind[0].lower() == 'q'`.
This means that it's possible to select stable-sort by passing
`kind='SnailSort'` instead of `kind='stable'` (although your typechecker might
ruin the fun).
"""
RNG: TypeAlias = np.random.SeedSequence | np.random.BitGenerator | np.random.Generator
Seed: TypeAlias = int | Integer | onp.ArrayND[Integer] | RNG
"""The accepted type of [`numpy.random.default_rng`][numpy.random.default_rng]."""
ToIntTrim: TypeAlias = int | tuple[int, int]
ToTrim: TypeAlias = float | tuple[float, float]
ToOrder0D: TypeAlias = int | Integer
ToOrder1D: TypeAlias = onp.CanArrayND[Integer] | Sequence[ToOrder0D]
ToOrderND: TypeAlias = (
onp.CanArrayND[Integer]
| onp.SequenceND[onp.CanArrayND[Integer]]
| onp.SequenceND[ToOrder0D]
)
ToOrder: TypeAlias = ToOrder0D | ToOrderND
ToFWeights: TypeAlias = onp.ArrayND[Integer]
ToAWeights: TypeAlias = onp.ArrayND[Floating]
class UnivariateOptions(TypedDict, total=False):
"""Use as e.g. `**kwds: Unpack[UnivariateOptions]`."""
sort: bool | SortKind
fweights: ToFWeights
aweights: ToAWeights
class LMomentOptions(UnivariateOptions, TypedDict, total=False):
"""Use as e.g. `**kwds: Unpack[LMomentOptions]`."""
cache: bool | None
class LComomentOptions(TypedDict, total=False):
"""Use as e.g. `**kwds: Unpack[LComomentOptions]`."""
sort: SortKind
cache: bool | None
rowvar: bool | None
_Tss = ParamSpec("_Tss")
_T = TypeVar("_T")
_F1_co = TypeVar("_F1_co", bound=Callable[..., object], covariant=True)
_F2_co = TypeVar("_F2_co", bound=Callable[..., object], covariant=True)
class Callable2(Protocol[_F1_co, _F2_co]):
"""
The intersection of two callable types, i.e. a callable with two overloads, one
for each of the callable type params.
"""
@overload
def __call__(
self: Callable2[Callable[_Tss, _T], _F2_co],
/,
*args: _Tss.args,
**kwds: _Tss.kwargs,
) -> _T: ...
@overload
def __call__(
self: Callable2[_F1_co, Callable[_Tss, _T]],
/,
*args: _Tss.args,
**kwds: _Tss.kwargs,
) -> _T: ...
# scipy stuff
_IntLike: TypeAlias = int | Integer
_FloatLike: TypeAlias = float | Floating
class QuadOptions(TypedDict, total=False):
"""
Optional quadrature options to be passed to the integration routine, e.g.
[`scipy.integrate.quad`][scipy.integrate.quad].
"""
epsabs: _FloatLike
epsrel: _FloatLike
limit: _IntLike
points: onp.ToFloat1D
weight: Literal["cos", "sin", "alg", "alg-loga", "alg-logb", "alg-log", "cauchy"]
wvar: _FloatLike | tuple[_FloatLike, _FloatLike]
wopts: tuple[_IntLike, onp.ArrayND[np.float32 | np.float64]]