Skip to content

Commit 387d171

Browse files
committed
♻️ remove references to the _polytypeshelper submodule in numpy.polynomial
1 parent 2b140c6 commit 387d171

File tree

5 files changed

+86
-42
lines changed

5 files changed

+86
-42
lines changed

src/numpy-stubs/polynomial/_polybase.pyi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import numpy.typing as npt
88
from _numtype import Array_1d, CoComplex_0d, CoComplex_1d, CoComplex_1nd, CoInteger_0d, CoInteger_1d, ToObject_0d, ToObject_1nd
99
from numpy._typing import _FloatLike_co
1010

11-
from ._polytypes import _InexactObject_1d, _ToInt, _ToNumeric_0d, _ToNumeric_nd, _Tuple2
11+
from .polynomial import _ToNumeric_0d, _ToNumeric_nd
1212

1313
__all__: Final[Sequence[str]] = ("ABCPolyBase",)
1414

@@ -31,7 +31,7 @@ class ABCPolyBase(abc.ABC):
3131
_use_unicode: ClassVar[bool]
3232
_symbol: str
3333

34-
coef: _InexactObject_1d
34+
coef: Array_1d[np.inexact | np.object_]
3535

3636
@property
3737
def symbol(self, /) -> str: ...
@@ -81,7 +81,7 @@ class ABCPolyBase(abc.ABC):
8181
def __truediv__(self, x: _AnyOther, /) -> Self: ...
8282
def __floordiv__(self, x: _AnyOther, /) -> Self: ...
8383
def __mod__(self, x: _AnyOther, /) -> Self: ...
84-
def __divmod__(self, x: _AnyOther, /) -> _Tuple2[Self]: ...
84+
def __divmod__(self, x: _AnyOther, /) -> tuple[Self, Self]: ...
8585
def __pow__(self, x: _AnyOther, /) -> Self: ...
8686
def __radd__(self, x: _AnyOther, /) -> Self: ...
8787
def __rsub__(self, x: _AnyOther, /) -> Self: ...
@@ -90,7 +90,7 @@ class ABCPolyBase(abc.ABC):
9090
def __rfloordiv__(self, x: _AnyOther, /) -> Self: ...
9191
def __rdiv__(self, x: _AnyOther, /) -> Self: ...
9292
def __rmod__(self, x: _AnyOther, /) -> Self: ...
93-
def __rdivmod__(self, x: _AnyOther, /) -> _Tuple2[Self]: ...
93+
def __rdivmod__(self, x: _AnyOther, /) -> tuple[Self, Self]: ...
9494
def __len__(self, /) -> int: ...
9595
def __iter__(self, /) -> Iterator[np.inexact | object]: ...
9696
def __getstate__(self, /) -> dict[str, Any]: ...
@@ -107,7 +107,7 @@ class ABCPolyBase(abc.ABC):
107107
def degree(self, /) -> int: ...
108108
def cutdeg(self, /, deg: int) -> Self: ...
109109
def trim(self, /, tol: _FloatLike_co = 0) -> Self: ...
110-
def truncate(self, /, size: _ToInt) -> Self: ...
110+
def truncate(self, /, size: SupportsIndex) -> Self: ...
111111

112112
#
113113
@overload
@@ -125,16 +125,16 @@ class ABCPolyBase(abc.ABC):
125125
) -> _PolyT: ...
126126

127127
#
128-
def mapparms(self, /) -> _Tuple2[Any]: ...
128+
def mapparms(self, /) -> tuple[Any, Any]: ...
129129
def integ(self, /, m: SupportsIndex = 1, k: _ToNumeric_0d | CoComplex_1d = [], lbnd: _ToNumeric_0d | None = None) -> Self: ...
130130
def deriv(self, /, m: SupportsIndex = 1) -> Self: ...
131-
def roots(self, /) -> _InexactObject_1d: ...
131+
def roots(self, /) -> Array_1d[np.float64] | Array_1d[np.complex128]: ...
132132
def linspace(
133133
self,
134134
/,
135135
n: SupportsIndex = 100,
136136
domain: CoComplex_1d | None = None,
137-
) -> _Tuple2[Array_1d[np.float64 | np.complex128]]: ...
137+
) -> tuple[Array_1d[np.float64 | np.complex128], Array_1d[np.float64 | np.complex128]]: ...
138138

139139
#
140140
@overload
@@ -200,7 +200,7 @@ class ABCPolyBase(abc.ABC):
200200
@classmethod
201201
def basis(
202202
cls,
203-
deg: _ToInt,
203+
deg: SupportsIndex,
204204
domain: CoComplex_1d | None = None,
205205
window: CoComplex_1d | None = None,
206206
symbol: str = "x",

src/numpy-stubs/polynomial/polynomial.pyi

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from typing import Any, Final, Literal as L, SupportsIndex as CanIndex, overload
1+
from collections.abc import Sequence
2+
from typing import Any, Final, Literal as L, Protocol, SupportsIndex, TypeAlias, overload, type_check_only
3+
from typing_extensions import Self, TypeVar
24

35
import numpy as np
46
from _numtype import (
@@ -43,10 +45,11 @@ from _numtype import (
4345
ToObject_1nd,
4446
ToObject_nd,
4547
ToReal_1d,
48+
_ToArray_1d,
49+
_ToArray_nd,
4650
)
4751

4852
from ._polybase import ABCPolyBase
49-
from ._polytypes import _ArrayAndFitResult, _Indices, _ToNumeric_0d, _ToNumeric_1d, _ToNumeric_nd
5053
from .polyutils import trimcoef as polytrim
5154

5255
__all__ = [
@@ -82,6 +85,32 @@ __all__ = [
8285

8386
###
8487

88+
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
89+
90+
_Indices: TypeAlias = Sequence[SupportsIndex]
91+
_ArrayAndFitResult: TypeAlias = tuple[Array[_ScalarT], Sequence[np.inexact | np.int32]]
92+
93+
_ToNumeric_0d: TypeAlias = ToComplex_0d | ToObject_0d | _SupportsCoefOps
94+
_ToNumeric_1d: TypeAlias = _ToArray_1d[np.number | np.bool | np.object_, complex | _SupportsCoefOps]
95+
_ToNumeric_nd: TypeAlias = _ToArray_nd[np.number | np.bool | np.object_, complex | _SupportsCoefOps]
96+
97+
# compatible with e.g. int, float, complex, Decimal, Fraction, and ABCPolyBase
98+
@type_check_only
99+
class _SupportsCoefOps(Protocol):
100+
def __eq__(self, x: object, /) -> bool: ...
101+
def __ne__(self, x: object, /) -> bool: ...
102+
def __neg__(self, /) -> Self: ...
103+
def __pos__(self, /) -> Self: ...
104+
def __add__(self, x: Any, /) -> Self: ...
105+
def __sub__(self, x: Any, /) -> Self: ...
106+
def __mul__(self, x: Any, /) -> Self: ...
107+
def __pow__(self, x: Any, /) -> Self | float: ...
108+
def __radd__(self, x: Any, /) -> Self: ...
109+
def __rsub__(self, x: Any, /) -> Self: ...
110+
def __rmul__(self, x: Any, /) -> Self: ...
111+
112+
###
113+
85114
polydomain: Final[Array_1d[np.float64]] = ...
86115
polyzero: Final[Array_1d[np.intp]] = ...
87116
polyone: Final[Array_1d[np.intp]] = ...
@@ -264,73 +293,83 @@ def polypow(c: ToObject_1d, pow: CoInteger_0d, maxpower: CoInteger_0d | None = N
264293

265294
#
266295
@overload
267-
def polyder(c: ToFloat64_nd | CoInteger_nd, m: CanIndex = 1, scl: CoFloating_0d = 1, axis: CanIndex = 0) -> Array[np.float64]: ...
296+
def polyder(
297+
c: ToFloat64_nd | CoInteger_nd,
298+
m: SupportsIndex = 1,
299+
scl: CoFloating_0d = 1,
300+
axis: SupportsIndex = 0,
301+
) -> Array[np.float64]: ...
268302
@overload
269-
def polyder(c: ToFloating_nd, m: CanIndex = 1, scl: CoFloating_0d = 1, axis: CanIndex = 0) -> Array[np.floating]: ...
303+
def polyder(c: ToFloating_nd, m: SupportsIndex = 1, scl: CoFloating_0d = 1, axis: SupportsIndex = 0) -> Array[np.floating]: ...
270304
@overload
271-
def polyder(c: ToComplex128_nd, m: CanIndex = 1, scl: CoComplex_0d = 1, axis: CanIndex = 0) -> Array[np.complex128]: ...
305+
def polyder(c: ToComplex128_nd, m: SupportsIndex = 1, scl: CoComplex_0d = 1, axis: SupportsIndex = 0) -> Array[np.complex128]: ...
272306
@overload
273-
def polyder(c: ToComplex_nd, m: CanIndex = 1, scl: CoComplex_0d = 1, axis: CanIndex = 0) -> Array[np.complexfloating]: ...
307+
def polyder(
308+
c: ToComplex_nd,
309+
m: SupportsIndex = 1,
310+
scl: CoComplex_0d = 1,
311+
axis: SupportsIndex = 0,
312+
) -> Array[np.complexfloating]: ...
274313
@overload
275-
def polyder(c: CoComplex_nd, m: CanIndex = 1, scl: CoComplex_0d = 1, axis: CanIndex = 0) -> Array[np.inexact]: ...
314+
def polyder(c: CoComplex_nd, m: SupportsIndex = 1, scl: CoComplex_0d = 1, axis: SupportsIndex = 0) -> Array[np.inexact]: ...
276315
@overload
277-
def polyder(c: ToObject_nd, m: CanIndex = 1, scl: _ToNumeric_0d = 1, axis: CanIndex = 0) -> Array[np.object_]: ...
316+
def polyder(c: ToObject_nd, m: SupportsIndex = 1, scl: _ToNumeric_0d = 1, axis: SupportsIndex = 0) -> Array[np.object_]: ...
278317

279318
#
280319
@overload
281320
def polyint(
282321
c: ToFloat64_nd | CoInteger_nd,
283-
m: CanIndex = 1,
322+
m: SupportsIndex = 1,
284323
k: CoFloat64_0d | CoFloat64_1d = [],
285324
lbnd: CoFloating_0d = 0,
286325
scl: CoFloating_0d = 1,
287-
axis: CanIndex = 0,
326+
axis: SupportsIndex = 0,
288327
) -> Array[np.float64]: ...
289328
@overload
290329
def polyint(
291330
c: CoFloating_nd,
292-
m: CanIndex = 1,
331+
m: SupportsIndex = 1,
293332
k: CoFloating_0d | CoFloating_1d = [],
294333
lbnd: CoFloating_0d = 0,
295334
scl: CoFloating_0d = 1,
296-
axis: CanIndex = 0,
335+
axis: SupportsIndex = 0,
297336
) -> Array[np.floating]: ...
298337
@overload
299338
def polyint(
300339
c: ToComplex_nd,
301-
m: CanIndex = 1,
340+
m: SupportsIndex = 1,
302341
k: CoComplex_0d | CoComplex_1d = [],
303342
lbnd: CoComplex_0d = 0,
304343
scl: CoComplex_0d = 1,
305-
axis: CanIndex = 0,
344+
axis: SupportsIndex = 0,
306345
) -> Array[np.complexfloating]: ...
307346
@overload
308347
def polyint(
309348
c: CoComplex_nd,
310-
m: CanIndex,
349+
m: SupportsIndex,
311350
k: ToComplex_0d | ToComplex_1d,
312351
lbnd: CoComplex_0d = 0,
313352
scl: CoComplex_0d = 1,
314-
axis: CanIndex = 0,
353+
axis: SupportsIndex = 0,
315354
) -> Array[np.complexfloating]: ...
316355
@overload
317356
def polyint(
318357
c: CoComplex_nd,
319-
m: CanIndex = 1,
358+
m: SupportsIndex = 1,
320359
*,
321360
k: ToComplex_0d | ToComplex_1d,
322361
lbnd: CoComplex_0d = 0,
323362
scl: CoComplex_0d = 1,
324-
axis: CanIndex = 0,
363+
axis: SupportsIndex = 0,
325364
) -> Array[np.complexfloating]: ...
326365
@overload
327366
def polyint(
328367
c: ToObject_nd,
329-
m: CanIndex = 1,
368+
m: SupportsIndex = 1,
330369
k: _ToNumeric_0d | _ToNumeric_1d = [],
331370
lbnd: _ToNumeric_0d = 0,
332371
scl: _ToNumeric_0d = 1,
333-
axis: CanIndex = 0,
372+
axis: SupportsIndex = 0,
334373
) -> Array[np.object_]: ...
335374

336375
#
@@ -563,19 +602,19 @@ def polygrid3d(x: _ToNumeric_nd, y: _ToNumeric_nd, z: _ToNumeric_nd, c: ToObject
563602

564603
#
565604
@overload
566-
def polyvander(x: ToFloat64_nd | CoInteger_nd, deg: CanIndex) -> Array[np.float64]: ...
605+
def polyvander(x: ToFloat64_nd | CoInteger_nd, deg: SupportsIndex) -> Array[np.float64]: ...
567606
@overload
568-
def polyvander(x: CoFloating_nd, deg: CanIndex) -> Array[np.floating]: ...
607+
def polyvander(x: CoFloating_nd, deg: SupportsIndex) -> Array[np.floating]: ...
569608
@overload
570-
def polyvander(x: ToComplex128_nd, deg: CanIndex) -> Array[np.complex128]: ...
609+
def polyvander(x: ToComplex128_nd, deg: SupportsIndex) -> Array[np.complex128]: ...
571610
@overload
572-
def polyvander(x: ToComplex_nd, deg: CanIndex) -> Array[np.complexfloating]: ...
611+
def polyvander(x: ToComplex_nd, deg: SupportsIndex) -> Array[np.complexfloating]: ...
573612
@overload
574-
def polyvander(x: CoComplex_nd, deg: CanIndex) -> Array[np.inexact]: ...
613+
def polyvander(x: CoComplex_nd, deg: SupportsIndex) -> Array[np.inexact]: ...
575614
@overload
576-
def polyvander(x: ToObject_nd, deg: CanIndex) -> Array[np.object_]: ...
615+
def polyvander(x: ToObject_nd, deg: SupportsIndex) -> Array[np.object_]: ...
577616
@overload
578-
def polyvander(x: _ToNumeric_nd, deg: CanIndex) -> Array[Any]: ...
617+
def polyvander(x: _ToNumeric_nd, deg: SupportsIndex) -> Array[Any]: ...
579618

580619
#
581620
@overload

src/numpy-stubs/polynomial/polyutils.pyi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from _typeshed import SupportsLenAndGetItem
12
from collections.abc import Iterable, Sequence
2-
from typing import Any, overload
3+
from typing import Any, TypeAlias, overload
34
from typing_extensions import TypeVar
45

56
import numpy as np
@@ -18,13 +19,14 @@ from _numtype import (
1819
)
1920
from numpy._typing import _FloatLike_co
2021

21-
from ._polytypes import _InexactObject_nd, _ToNumeric_0d, _Tuple2
22-
2322
__all__ = ["as_series", "format_float", "getdomain", "mapdomain", "mapparms", "trimcoef", "trimseq"]
2423

2524
###
2625

27-
_T_seq = TypeVar("_T_seq", bound=_InexactObject_nd | Sequence[_ToNumeric_0d])
26+
_T = TypeVar("_T")
27+
_T_seq = TypeVar("_T_seq", bound=Array[np.number | np.bool | np.object_] | SupportsLenAndGetItem[CoComplex_0d | ToObject_0d])
28+
29+
_Tuple2: TypeAlias = tuple[_T, _T]
2830

2931
###
3032

test/static/accept/polynomial_polybase.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ assert_type(PS_lag.integ(SC_i_co, SC_f_co), npp.Laguerre)
122122
assert_type(PS_poly.deriv(), npp.Polynomial)
123123
assert_type(PS_herm.deriv(SC_i_co), npp.Hermite)
124124

125-
assert_type(PS_poly.roots(), _Array1D[np.inexact | np.object_])
125+
assert_type(PS_poly.roots(), _Array1D[np.float64] | _Array1D[np.complex128])
126126

127127
assert_type(
128128
PS_poly.linspace(),

test/static/accept/polynomial_polyutils.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ from typing_extensions import TypeVar, assert_type
77
import numpy as np
88
import numpy.polynomial.polyutils as pu
99
import numpy.typing as npt
10-
from numpy.polynomial._polytypes import _Tuple2
1110

1211
###
1312

13+
_T = TypeVar("_T")
1414
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
15+
1516
_Array1D: TypeAlias = np.ndarray[tuple[int], np.dtype[_ScalarT]]
1617

1718
_Floating1D: TypeAlias = _Array1D[np.floating]
1819
_Complex1D: TypeAlias = _Array1D[np.complexfloating]
1920
_Object1D: TypeAlias = _Array1D[np.object_]
2021

22+
_Tuple2: TypeAlias = tuple[_T, _T]
23+
2124
###
2225

2326
num_int: int

0 commit comments

Comments
 (0)