10000 Merge pull request #26897 from jorenham/polynomial-typing · numpy/numpy@8be0e74 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8be0e74

Browse files
authored
Merge pull request #26897 from jorenham/polynomial-typing
TYP: type hint ``numpy.polynomial``
2 parents 97bbe73 + ef3278a commit 8be0e74

13 files changed

+2919
-356
lines changed

numpy/polynomial/__init__.pyi

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
from numpy._pytesttester import PytestTester
1+
from typing import Final, Literal
22

3-
from numpy.polynomial import (
4-
chebyshev as chebyshev,
5-
hermite as hermite,
6-
hermite_e as hermite_e,
7-
laguerre as laguerre,
8-
legendre as legendre,
9-
polynomial as polynomial,
10-
)
11-
from numpy.polynomial.chebyshev import Chebyshev as Chebyshev
12-
from numpy.polynomial.hermite import Hermite as Hermite
13-
from numpy.polynomial.hermite_e import HermiteE as HermiteE
14-
from numpy.polynomial.laguerre import Laguerre as Laguerre
15-
from numpy.polynomial.legendre import Legendre as Legendre
16-
from numpy.polynomial.polynomial import Polynomial as Polynomial
3+
from .polynomial import Polynomial
4+
from .chebyshev import Chebyshev
5+
from .legendre import Legendre
6+
from .hermite import Hermite
7+
from .hermite_e import HermiteE
8+
from .laguerre import Laguerre
179

18-
__all__: list[str]
19-
test: PytestTester
10+
__all__ = [
11+
"set_default_printstyle",
12+
"polynomial", "Polynomial",
13+
"chebyshev", "Chebyshev",
14+
"legendre", "Legendre",
15+
"hermite", "Hermite",
16+
"hermite_e", "HermiteE",
17+
"laguerre", "Laguerre",
18+
]
2019

21-
def set_default_printstyle(style): ...
20+
def set_default_printstyle(style: Literal["ascii", "unicode"]) -> None: ...
21+
22+
from numpy._pytesttester import PytestTester as _PytestTester
23+
test: Final[_PytestTester]

numpy/polynomial/_polybase.pyi

Lines changed: 286 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,297 @@
11
import abc
2-
from typing import Any, ClassVar
2+
import decimal
3+
import numbers
4+
import sys
5+
from collections.abc import Iterator, Mapping, Sequence
6+
from typing import (
7+
TYPE_CHECKING,
8+
Any,
9+
ClassVar,
10+
Final,
11+
Generic,
12+
Literal,
13+
SupportsIndex,
14+
TypeAlias,
15+
TypeGuard,
16+
TypeVar,
17+
overload,
18+
)
319

4-
__all__: list[str]
20+
import numpy as np
21+
import numpy.typing as npt
22+
from numpy._typing import (
23+
_FloatLike_co,
24+
_NumberLike_co,
525

6-
class ABCPolyBase(abc.ABC):
26+
_ArrayLikeFloat_co,
27+
_ArrayLikeComplex_co,
28+
)
29+
30+
from ._polytypes import (
31+
_AnyInt,
32+
_CoefLike_co,
33+
34+
_Array2,
35+
_Tuple2,
36+
37+
_Series,
38+
_CoefSeries,
39+
40+
_SeriesLikeInt_co,
41+
_SeriesLikeCoef_co,
42+
43+
_ArrayLikeCoefObject_co,
44+
_ArrayLikeCoef_co,
45+
)
46+
47+
if sys.version_info >= (3, 11):
48+
from typing import LiteralString
49+
elif TYPE_CHECKING:
50+
from typing_extensions import LiteralString
51+
else:
52+
LiteralString: TypeAlias = str
53+
54+
55+
__all__: Final[Sequence[str]] = ("ABCPolyBase",)
56+
57+
58+
_NameCo = TypeVar("_NameCo", bound=None | LiteralString, covariant=True)
59+
_Self = TypeVar("_Self", bound="ABCPolyBase")
60+
_Other = TypeVar("_Other", bound="ABCPolyBase")
61+
62+
_AnyOther: TypeAlias = ABCPolyBase | _CoefLike_co | _SeriesLikeCoef_co
63+
_Hundred: TypeAlias = Literal[100]
64+
65+
66+
class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta):
767
__hash__: ClassVar[None] # type: ignore[assignment]
868
__array_ufunc__: ClassVar[None]
9-
maxpower: ClassVar[int]
10-
coef: Any
11-
@property
12-
def symbol(self) -> str: ...
13-
@property
14-
@abc.abstractmethod
15-
def domain(self): ...
16-
@property
17-
@abc.abstractmethod
18-
def window(self): ...
69+
70+
maxpower: ClassVar[_Hundred]
71+
_superscript_mapping: ClassVar[Mapping[int, str]]
72+
_subscript_mapping: ClassVar[Mapping[int, str]]
73+
_use_unicode: ClassVar[bool]
74+
75+
basis_name: _NameCo
76+
coef: _CoefSeries
77+
domain: _Array2[np.inexact[Any] | np.object_]
78+
window: _Array2[np.inexact[Any] | np.object_]
79+
80+
_symbol: LiteralString
1981
@property
20-
@abc.abstractmethod
21-
def basis_name(self): ...
22-
def has_samecoef(self, other): ...
23-
def has_samedomain(self, other): ...
24-
def has_samewindow(self, other): ...
25-
def has_sametype(self, other): ...
26-
def __init__(self, coef, domain=..., window=..., symbol: str = ...) -> None: ...
27-
def __format__(self, fmt_str): ...
28-
def __call__(self, arg): ...
29-
def __iter__(self): ...
30-
def __len__(self): ...
31-
def __neg__(self): ...
32-
def __pos__(self): ...
33-
def __add__(self, other): ...
34-
def __sub__(self, other): ...
35-
def __mul__(self, other): ...
36-
def __truediv__(self, other): ...
37-
def __floordiv__(self, other): ...
38-
def __mod__(self, other): ...
39-
def __divmod__(self, other): ...
40-
def __pow__(self, other): ...
41-
def __radd__(self, other): ...
42-
def __rsub__(self, other): ...
43-
def __rmul__(self, other): ...
44-
def __rdiv__(self, other): ...
45-
def __rtruediv__(self, other): ...
46-
def __rfloordiv__(self, other): ...
47-
def __rmod__(self, other): ...
48-
def __rdivmod__(self, other): ...
49-
def __eq__(self, other): ...
50-
def __ne__(self, other): ...
51-
def copy(self): ...
52-
def degree(self): ...
53-
def cutdeg(self, deg): ...
54-
def trim(self, tol=...): ...
55-
def truncate(self, size): ...
56-
def convert(self, domain=..., kind=..., window=...): ...
57-
def mapparms(self): ...
58-
def integ(self, m=..., k = ..., lbnd=...): ...
59-
def deriv(self, m=...): ...
60-
def roots(self): ...
61-
def linspace(self, n=..., domain=...): ...
82+
def symbol(self, /) -> LiteralString: ...
83+
84+
def __ini B41A t__(
85+
self,
86+
/,
87+
coef: _SeriesLikeCoef_co,
88+
domain: None | _SeriesLikeCoef_co = ...,
89+
window: None | _SeriesLikeCoef_co = ...,
90+
symbol: str = ...,
91+
) -> None: ...
92+
93+
@overload
94+
def __call__(self, /, arg: _Other) -> _Other: ...
95+
# TODO: Once `_ShapeType@ndarray` is covariant and bounded (see #26081),
96+
# additionally include 0-d arrays as input types with scalar return type.
97+
@overload
98+
def __call__(
99+
self,
100+
/,
101+
arg: _FloatLike_co | decimal.Decimal | numbers.Real | np.object_,
102+
) -> np.float64 | np.complex128: ...
103+
@overload
104+
def __call__(
105+
self,
106+
/,
107+
arg: _NumberLike_co | numbers.Complex,
108+
) -> np.complex128: ...
109+
@overload
110+
def __call__(self, /, arg: _ArrayLikeFloat_co) -> (
111+
npt.NDArray[np.float64]
112+
| npt.NDArray[np.complex128]
113+
| npt.NDArray[np.object_]
114+
): ...
115+
@overload< F438 /span>
116+
def __call__(
117+
self,
118+
/,
119+
arg: _ArrayLikeComplex_co,
120+
) -> npt.NDArray[np.complex128] | npt.NDArray[np.object_]: ...
121+
@overload
122+
def __call__(
123+
self,
124+
/,
125+
arg: _ArrayLikeCoefObject_co,
126+
) -> npt.NDArray[np.object_]: ...
127+
128+
def __str__(self, /) -> str: ...
129+
def __repr__(self, /) -> str: ...
130+
def __format__(self, fmt_str: str, /) -> str: ...
131+
def __eq__(self, x: object, /) -> bool: ...
132+
def __ne__(self, x: object, /) -> bool: ...
133+
def __neg__(self: _Self, /) -> _Self: ...
134+
def __pos__(self: _Self, /) -> _Self: ...
135+
def __add__(self: _Self, x: _AnyOther, /) -> _Self: ...
136+
def __sub__(self: _Self, x: _AnyOther, /) -> _Self: ...
137+
def __mul__(self: _Self, x: _AnyOther, /) -> _Self: ...
138+
def __truediv__(self: _Self, x: _AnyOther, /) -> _Self: ...
139+
def __floordiv__(self: _Self, x: _AnyOther, /) -> _Self: ...
140+
def __mod__(self: _Self, x: _AnyOther, /) -> _Self: ...
141+
def __divmod__(self: _Self, x: _AnyOther, /) -> _Tuple2[_Self]: ...
142+
def __pow__(self: _Self, x: _AnyOther, /) -> _Self: ...
143+
def __radd__(self: _Self, x: _AnyOther, /) -> _Self: ...
144+
def __rsub__(self: _Self, x: _AnyOther, /) -> _Self: ...
145+
def __rmul__(self: _Self, x: _AnyOther, /) -> _Self: ...
146+
def __rtruediv__(self: _Self, x: _AnyOther, /) -> _Self: ...
147+
def __rfloordiv__(self: _Self, x: _AnyOther, /) -> _Self: ...
148+
def __rmod__(self: _Self, x: _AnyOther, /) -> _Self: ...
149+
def __rdivmod__(self: _Self, x: _AnyOther, /) -> _Tuple2[_Self]: ...
150+
def __len__(self, /) -> int: ...
151+
def __iter__(self, /) -> Iterator[np.inexact[Any] | object]: ...
152+
def __getstate__(self, /) -> dict[str, Any]: ...
153+
def __setstate__(self, dict: dict[str, Any], /) -> None: ...
154+
155+
def has_samecoef(self, /, other: ABCPolyBase) -> bool: ...
156+
def has_samedomain(self, /, other: ABCPolyBase) -> bool: ...
157+
def has_samewindow(self, /, other: ABCPolyBase) -> bool: ...
158+
@overload
159+
def has_sametype(self: _Self, /, other: ABCPolyBase) -> TypeGuard[_Self]: ...
160+
@overload
161+
def has_sametype(self, /, other: object) -> Literal[False]: ...
162+
163+
def copy(self: _Self, /) -> _Self: ...
164+
def degree(self, /) -> int: ...
165+
def cutdeg(self: _Self, /) -> _Self: ...
166+
def trim(self: _Self, /, tol: _FloatLike_co = ...) -> _Self: ...
167+
def truncate(self: _Self, /, size: _AnyInt) -> _Self: ...
168+
169+
@overload
170+
def convert(
171+
self,
172+
domain: None | _SeriesLikeCoef_co,
173+
kind: type[_Other],
174+
/,
175+
window: None | _SeriesLikeCoef_co = ...,
176+
) -> _Other: ...
177+
@overload
178+
def convert(
179+
self,
180+
/,
181+
domain: None | _SeriesLikeCoef_co = ...,
182+
*,
183+
kind: type[_Other],
184+
window: None | _SeriesLikeCoef_co = ...,
185+
) -> _Other: ...
186+
@overload
187+
def convert(
188+
self: _Self,
189+
/,
190+
domain: None | _SeriesLikeCoef_co = ...,
191+
kind: type[_Self] = ...,
192+
window: None | _SeriesLikeCoef_co = ...,
193+
) -> _Self: ...
194+
195+
def mapparms(self, /) -> _Tuple2[Any]: ...
196+
197+
def integ(
198+
self: _Self, /,
199+
m: SupportsIndex = ...,
200+
k: _CoefLike_co | _SeriesLikeCoef_co = ...,
201+
lbnd: None | _CoefLike_co = ...,
202+
) -> _Self: ...
203+
204+
def deriv(self: _Self, /, m: SupportsIndex = ...) -> _Self: ...
205+
206+
def roots(self, /) -> _CoefSeries: ...
207+
208+
def linspace(
209+
self, /,
210+
n: SupportsIndex = ...,
211+
domain: None | _SeriesLikeCoef_co = ...,
212+
) -> _Tuple2[_Series[np.float64 | np.complex128]]: ...
213+
214+
@overload
62215
@classmethod
63-
def fit(cls, x, y, deg, domain=..., rcond=..., full=..., w=..., window=...): ...
216+
def fit(
217+
cls: type[_Self], /,
218+
x: _SeriesLikeCoef_co,
219+
y: _SeriesLikeCoef_co,
220+
deg: int | _SeriesLikeInt_co,
221+
domain: None | _SeriesLikeCoef_co = ...,
222+
rcond: _FloatLike_co = ...,
223+
full: Literal[False] = ...,
224+
w: None | _SeriesLikeCoef_co = ...,
225+
window: None | _SeriesLikeCoef_co = ...,
226+
symbol: str = ...,
227+
) -> _Self: ...
228+
@overload
64229
@classmethod
65-
def fromroots(cls, roots, domain = ..., window=...): ...
230+
def fit(
231+
cls: type[_Self], /,
232+
x: _SeriesLikeCoef_co,
233+
y: _SeriesLikeCoef_co,
234+
deg: int | _SeriesLikeInt_co,
235+
domain: None | _SeriesLikeCoef_co = ...,
236+
rcond: _FloatLike_co = ...,
237+
*,
238+
full: Literal[True],
239+
w: None | _SeriesLikeCoef_co = ...,
240+
window: None | _SeriesLikeCoef_co = ...,
241+
symbol: str = ...,
242+
) -> tuple[_Self, Sequence[np.inexact[Any] | np.int32]]: ...
243+
@overload
66244
@classmethod
67-
def identity(cls, domain=..., window=...): ...
245+
def fit(
246+
cls: type[_Self],
247+
x: _SeriesLikeCoef_co,
248+
y: _SeriesLikeCoef_co,
249+
deg: int | _SeriesLikeInt_co,
250+
domain: None | _SeriesLikeCoef_co,
251+
rcond: _FloatLike_co,
252+
full: Literal[True], /,
253+
w: None | _SeriesLikeCoef_co = ...,
254+
window: None | _SeriesLikeCoef_co = ...,
255+
symbol: str = ...,
256+
) -> tuple[_Self, Sequence[np.inexact[Any] | np.int32]]: ...
257+
68258
@classmethod
69-
def basis(cls, deg, domain=..., window=...): ...
259+
def fromroots(
260+
cls: type[_Self], /,
261+
roots: _ArrayLikeCoef_co,
262+
domain: None | _SeriesLikeCoef_co = ...,
263+
window: None | _SeriesLikeCoef_co = ...,
264+
symbol: str = ...,
265+
) -> _Self: ...
266+
267+
@classmethod
268+
def identity(
269+
cls: type[_Self], /,
270+
domain: None | _SeriesLikeCoef_co = ...,
271+
window: None | _SeriesLikeCoef_co = ...,
272+
symbol: str = ...,
273+
) -> _Self: ...
274+
275+
@classmethod
276+
def basis(
277+
cls: type[_Self], /,
278+
deg: _AnyInt,
279+
domain: None | _SeriesLikeCoef_co = ...,
280+
window: None | _SeriesLikeCoef_co = ...,
281+
symbol: str = ...,
282+
) -> _Self: ...
283+
284+
@classmethod
285+
def cast(
286+
cls: type[_Self], /,
287+
series: ABCPolyBase,
288+
domain: None | _SeriesLikeCoef_co = ...,
289+
window: None | _SeriesLikeCoef_co = ...,
290+
) -> _Self: ...
291+
70292
@classmethod
71-
def cast(cls, series, domain=..., window=...): ...
293+
def _str_term_unicode(cls, i: str, arg_str: str) -> str: ...
294+
@staticmethod
295+
def _str_term_ascii(i: str, arg_str: str) -> str: ...
296+
@staticmethod
297+
def _repr_latex_term(i: str, arg_str: str, needs_parens: bool) -> str: ...

0 commit comments

Comments
 (0)
0