Skip to content

Commit 0d40128

Browse files
authored
Merge pull request #254 from numpy/numpy.polynomial
🏷️ fix stubtest errors in `numpy.polynomial._polybase`
2 parents 55061e6 + f9e15aa commit 0d40128

File tree

9 files changed

+87
-64
lines changed

9 files changed

+87
-64
lines changed

src/numpy-stubs/polynomial/_polybase.pyi

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import abc
22
from collections.abc import Iterator, Mapping, Sequence
3-
from typing import Any, ClassVar, Final, Generic, Literal, SupportsIndex, TypeAlias, overload
4-
from typing_extensions import LiteralString, Self, TypeIs, TypeVar
3+
from typing import Any, ClassVar, Final, Literal, SupportsIndex, TypeAlias, overload
4+
from typing_extensions import Self, TypeIs, TypeVar
55

66
import numpy as np
77
import numpy.typing as npt
@@ -14,31 +14,38 @@ __all__: Final[Sequence[str]] = ("ABCPolyBase",)
1414

1515
###
1616

17-
_NameT_co = TypeVar("_NameT_co", bound=str | None, default=LiteralString | None, covariant=True)
1817
_PolyT = TypeVar("_PolyT", bound=ABCPolyBase)
1918

2019
_AnyOther: TypeAlias = ABCPolyBase | _ToNumeric_0d | CoComplex_1d
2120
_Hundred: TypeAlias = Literal[100]
2221

2322
###
2423

25-
class ABCPolyBase(abc.ABC, Generic[_NameT_co]):
24+
class ABCPolyBase(abc.ABC):
2625
__hash__: ClassVar[None] # type: ignore[assignment] # pyright: ignore[reportIncompatibleMethodOverride]
2726
__array_ufunc__: ClassVar[None]
2827

2928
maxpower: ClassVar[_Hundred]
3029
_superscript_mapping: ClassVar[Mapping[int, str]]
3130
_subscript_mapping: ClassVar[Mapping[int, str]]
3231
_use_unicode: ClassVar[bool]
32+
_symbol: str
3333

34-
basis_name: _NameT_co
3534
coef: _InexactObject_1d
36-
domain: Array_1d[np.inexact]
37-
window: Array_1d[np.inexact]
3835

39-
_symbol: LiteralString
4036
@property
41-
def symbol(self, /) -> LiteralString: ...
37+
def symbol(self, /) -> str: ...
38+
39+
#
40+
@property
41+
@abc.abstractmethod
42+
def basis_name(self, /) -> str | None: ...
43+
@property
44+
@abc.abstractmethod
45+
def domain(self, /) -> Array_1d[np.inexact]: ...
46+
@property
47+
@abc.abstractmethod
48+
def window(self, /) -> Array_1d[np.inexact]: ...
4249

4350
#
4451
def __init__(
@@ -81,6 +88,7 @@ class ABCPolyBase(abc.ABC, Generic[_NameT_co]):
8188
def __rmul__(self, x: _AnyOther, /) -> Self: ...
8289
def __rtruediv__(self, x: _AnyOther, /) -> Self: ...
8390
def __rfloordiv__(self, x: _AnyOther, /) -> Self: ...
91+
def __rdiv__(self, x: _AnyOther, /) -> Self: ...
8492
def __rmod__(self, x: _AnyOther, /) -> Self: ...
8593
def __rdivmod__(self, x: _AnyOther, /) -> _Tuple2[Self]: ...
8694
def __len__(self, /) -> int: ...
@@ -97,7 +105,7 @@ class ABCPolyBase(abc.ABC, Generic[_NameT_co]):
97105
#
98106
def copy(self, /) -> Self: ...
99107
def degree(self, /) -> int: ...
100-
def cutdeg(self, /) -> Self: ...
108+
def cutdeg(self, /, deg: int) -> Self: ...
101109
def trim(self, /, tol: _FloatLike_co = 0) -> Self: ...
102110
def truncate(self, /, size: _ToInt) -> Self: ...
103111

@@ -178,12 +186,17 @@ class ABCPolyBase(abc.ABC, Generic[_NameT_co]):
178186
def fromroots(
179187
cls,
180188
roots: _ToNumeric_nd,
181-
domain: CoComplex_1d | None = None,
189+
domain: CoComplex_1d | None = [],
182190
window: CoComplex_1d | None = None,
183191
symbol: str = "x",
184192
) -> Self: ...
185193
@classmethod
186-
def identity(cls, domain: CoComplex_1d | None = None, window: CoComplex_1d | None = None, symbol: str = "x") -> Self: ...
194+
def identity(
195+
cls,
196+
domain: CoComplex_1d | None = None,
197+
window: CoComplex_1d | None = None,
198+
symbol: str = "x",
199+
) -> Self: ...
187200
@classmethod
188201
def basis(
189202
cls,
@@ -193,10 +206,17 @@ class ABCPolyBase(abc.ABC, Generic[_NameT_co]):
193206
symbol: str = "x",
194207
) -> Self: ...
195208
@classmethod
196-
def cast(cls, series: ABCPolyBase, domain: CoComplex_1d | None = None, window: CoComplex_1d | None = None) -> Self: ...
209+
def cast(
210+
cls,
211+
series: ABCPolyBase,
212+
domain: CoComplex_1d | None = None,
213+
window: CoComplex_1d | None = None,
214+
) -> Self: ...
215+
216+
#
197217
@classmethod
198218
def _str_term_unicode(cls, /, i: str, arg_str: str) -> str: ...
199-
@staticmethod
200-
def _str_term_ascii(i: str, arg_str: str) -> str: ...
201-
@staticmethod
202-
def _repr_latex_term(i: str, arg_str: str, needs_parens: bool) -> str: ...
219+
@classmethod
220+
def _str_term_ascii(cls, i: str, arg_str: str) -> str: ...
221+
@classmethod
222+
def _repr_latex_term(cls, i: str, arg_str: str, needs_parens: bool) -> str: ...

src/numpy-stubs/polynomial/chebyshev.pyi

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ __all__ = [
7070
"poly2cheb",
7171
]
7272

73-
_SCT = TypeVar("_SCT", bound=np.number | np.object_)
74-
_RT = TypeVar("_RT", bound=np.number | np.bool | np.object_)
73+
_NumberT = TypeVar("_NumberT", bound=np.number | np.object_)
74+
_CoNumberT = TypeVar("_CoNumberT", bound=np.number | np.bool | np.object_)
7575

7676
###
7777

78-
def _cseries_to_zseries(c: Array[_SCT]) -> Array_1d[_SCT]: ...
79-
def _zseries_to_cseries(zs: Array[_SCT]) -> Array_1d[_SCT]: ...
80-
def _zseries_mul(z1: Array[_SCT], z2: Array[_SCT]) -> Array_1d[_SCT]: ...
81-
def _zseries_div(z1: Array[_SCT], z2: Array[_SCT]) -> Array_1d[_SCT]: ...
82-
def _zseries_der(zs: Array[_SCT]) -> Array_1d[_SCT]: ...
83-
def _zseries_int(zs: Array[_SCT]) -> Array_1d[_SCT]: ...
78+
def _cseries_to_zseries(c: Array[_NumberT]) -> Array_1d[_NumberT]: ...
79+
def _zseries_to_cseries(zs: Array[_NumberT]) -> Array_1d[_NumberT]: ...
80+
def _zseries_mul(z1: Array[_NumberT], z2: Array[_NumberT]) -> Array_1d[_NumberT]: ...
81+
def _zseries_div(z1: Array[_NumberT], z2: Array[_NumberT]) -> Array_1d[_NumberT]: ...
82+
def _zseries_der(zs: Array[_NumberT]) -> Array_1d[_NumberT]: ...
83+
def _zseries_int(zs: Array[_NumberT]) -> Array_1d[_NumberT]: ...
8484

8585
poly2cheb: Final[_FuncPoly2Ortho[L["poly2cheb"]]] = ...
8686
cheb2poly: Final[_FuncUnOp[L["cheb2poly"]]] = ...
@@ -121,15 +121,23 @@ chebpts2: Final[_FuncPts[L["chebpts2"]]] = ...
121121
@overload
122122
def chebinterpolate(func: np.ufunc, deg: _IntLike_co, args: tuple[()] = ...) -> Array[Any]: ...
123123
@overload
124-
def chebinterpolate(func: Callable[[Array[np.float64]], _RT], deg: _IntLike_co, args: tuple[()] = ...) -> Array[_RT]: ...
124+
def chebinterpolate(
125+
func: Callable[[Array[np.float64]], _CoNumberT],
126+
deg: _IntLike_co,
127+
args: tuple[()] = (),
128+
) -> Array[_CoNumberT]: ...
125129
@overload
126130
def chebinterpolate(
127-
func: Callable[Concatenate[Array[np.float64], ...], _RT],
131+
func: Callable[Concatenate[Array[np.float64], ...], _CoNumberT],
128132
deg: _IntLike_co,
129133
args: Iterable[Any],
130-
) -> Array[_RT]: ...
134+
) -> Array[_CoNumberT]: ...
135+
136+
class Chebyshev(ABCPolyBase):
137+
domain: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
138+
window: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
139+
basis_name: L["T"] = "T" # pyright: ignore[reportIncompatibleMethodOverride]
131140

132-
class Chebyshev(ABCPolyBase[L["T"]]):
133141
@overload
134142
@classmethod
135143
def interpolate(
@@ -145,8 +153,7 @@ class Chebyshev(ABCPolyBase[L["T"]]):
145153
cls,
146154
func: Callable[Concatenate[Array[np.float64], ...], _ToNumeric_1d],
147155
deg: _IntLike_co,
148-
domain: CoComplex_1d | None = ...,
149-
*,
156+
domain: CoComplex_1d | None,
150157
args: Iterable[Any],
151158
) -> Self: ...
152159
@overload
@@ -155,6 +162,7 @@ class Chebyshev(ABCPolyBase[L["T"]]):
155162
cls,
156163
func: Callable[Concatenate[Array[np.float64], ...], _ToNumeric_1d],
157164
deg: _IntLike_co,
158-
domain: CoComplex_1d | None,
165+
domain: CoComplex_1d | None = ...,
166+
*,
159167
args: Iterable[Any],
160168
) -> Self: ...

src/numpy-stubs/polynomial/hermite.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,7 @@ hermweight: Final[_FuncWeight[L["hermweight"]]] = ...
9999

100100
def _normed_hermite_n(x: Array[np.float64, _ShapeT], n: int | np.intp) -> Array[np.float64, _ShapeT]: ...
101101

102-
class Hermite(ABCPolyBase[L["H"]]): ...
102+
class Hermite(ABCPolyBase):
103+
domain: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
104+
window: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
105+
basis_name: L["H"] = "H" # pyright: ignore[reportIncompatibleMethodOverride]

src/numpy-stubs/polynomial/hermite_e.pyi

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ __all__ = [
6363
"poly2herme",
6464
]
6565

66-
_ND = TypeVar("_ND", bound=tuple[int, ...])
66+
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
6767

6868
###
6969

@@ -99,6 +99,9 @@ hermeroots: Final[_FuncRoots[L["hermeroots"]]] = ...
9999
hermegauss: Final[_FuncGauss[L["hermegauss"]]] = ...
100100
hermeweight: Final[_FuncWeight[L["hermeweight"]]] = ...
101101

102-
def _normed_hermite_e_n(x: Array[np.float64, _ND], n: int | np.intp) -> Array[np.float64, _ND]: ...
102+
def _normed_hermite_e_n(x: Array[np.float64, _ShapeT], n: int | np.intp) -> Array[np.float64, _ShapeT]: ...
103103

104-
class HermiteE(ABCPolyBase[L["He"]]): ...
104+
class HermiteE(ABCPolyBase):
105+
domain: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
106+
window: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
107+
basis_name: L["He"] = "He" # pyright: ignore[reportIncompatibleMethodOverride]

src/numpy-stubs/polynomial/laguerre.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,7 @@ lagroots: Final[_FuncRoots[L["lagroots"]]] = ...
9494
laggauss: Final[_FuncGauss[L["laggauss"]]] = ...
9595
lagweight: Final[_FuncWeight[L["lagweight"]]] = ...
9696

97-
class Laguerre(ABCPolyBase[L["L"]]): ...
97+
class Laguerre(ABCPolyBase):
98+
domain: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
99+
window: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
100+
basis_name: L["L"] = "L" # pyright: ignore[reportIncompatibleMethodOverride]

src/numpy-stubs/polynomial/legendre.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,7 @@ legroots: Final[_FuncRoots[L["legroots"]]] = ...
9494
leggauss: Final[_FuncGauss[L["leggauss"]]] = ...
9595
legweight: Final[_FuncWeight[L["legweight"]]] = ...
9696

97-
class Legendre(ABCPolyBase[L["P"]]): ...
97+
class Legendre(ABCPolyBase):
98+
domain: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
99+
window: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
100+
basis_name: L["P"] = "P" # pyright: ignore[reportIncompatibleMethodOverride]

src/numpy-stubs/polynomial/polynomial.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,7 @@ polyfit: Final[_FuncFit[L["polyfit"]]] = ...
8484
polycompanion: Final[_FuncCompanion[L["polycompanion"]]] = ...
8585
polyroots: Final[_FuncRoots[L["polyroots"]]] = ...
8686

87-
class Polynomial(ABCPolyBase[None]): ...
87+
class Polynomial(ABCPolyBase):
88+
domain: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
89+
window: Array_1d[np.float64] = ... # pyright: ignore[reportIncompatibleMethodOverride]
90+
basis_name: None = None # pyright: ignore[reportIncompatibleMethodOverride]

test/static/accept/polynomial_polybase.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from collections.abc import Sequence
22
from decimal import Decimal
33
from fractions import Fraction
44
from typing import Any, Literal as L, TypeAlias
5-
from typing_extensions import LiteralString, TypeVar, assert_type
5+
from typing_extensions import TypeVar, assert_type
66

77
import numpy as np
88
import numpy.polynomial as npp
@@ -82,9 +82,9 @@ assert_type(type(PS_herme).cast(PS_leg), npp.HermiteE)
8282
# attributes / properties
8383

8484
assert_type(PS_all.coef, _Array1D[np.inexact | np.object_])
85-
assert_type(PS_all.domain, _Array1D[np.inexact])
86-
assert_type(PS_all.window, _Array1D[np.inexact])
87-
assert_type(PS_all.symbol, LiteralString)
85+
assert_type(PS_all.domain, _Array1D[np.float64])
86+
assert_type(PS_all.window, _Array1D[np.float64])
87+
assert_type(PS_all.symbol, str)
8888

8989
# instance methods
9090

@@ -104,7 +104,7 @@ assert_type(PS_herme.copy(), npp.HermiteE)
104104
assert_type(PS_lag.copy(), npp.Laguerre)
105105
assert_type(PS_leg.copy(), npp.Legendre)
106106

107-
assert_type(PS_leg.cutdeg(), npp.Legendre)
107+
assert_type(PS_leg.cutdeg(1), npp.Legendre)
108108
assert_type(PS_leg.trim(), npp.Legendre)
109109
assert_type(PS_leg.trim(tol=SC_f_co), npp.Legendre)
110110
assert_type(PS_leg.truncate(SC_i_co), npp.Legendre)

tool/.mypyignore-todo

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,6 @@ numpy.ma.core.mask_rowcols
5656
numpy.ma.extras.MAxisConcatenator.concatenate
5757
numpy.ma.mrecords.fromtextfile
5858

59-
numpy.polynomial.Chebyshev.basis_name
60-
numpy.polynomial.Hermite.basis_name
61-
numpy.polynomial.HermiteE.basis_name
62-
numpy.polynomial.Laguerre.basis_name
63-
numpy.polynomial.Legendre.basis_name
64-
numpy.polynomial.Polynomial.basis_name
65-
numpy.polynomial._polybase.ABCPolyBase.__rdiv__
66-
numpy.polynomial._polybase.ABCPolyBase._repr_latex_term
67-
numpy.polynomial._polybase.ABCPolyBase._str_term_ascii
68-
numpy.polynomial._polybase.ABCPolyBase.basis_name
69-
numpy.polynomial._polybase.ABCPolyBase.cutdeg
70-
numpy.polynomial._polybase.ABCPolyBase.domain
71-
numpy.polynomial._polybase.ABCPolyBase.fromroots
72-
numpy.polynomial._polybase.ABCPolyBase.window
73-
numpy.polynomial.chebyshev.Chebyshev.basis_name
7459
numpy.polynomial.chebyshev.cheb2poly
7560
numpy.polynomial.chebyshev.chebadd
7661
numpy.polynomial.chebyshev.chebcompanion
@@ -99,7 +84,6 @@ numpy.polynomial.chebyshev.chebvander2d
9984
numpy.polynomial.chebyshev.chebvander3d
10085
numpy.polynomial.chebyshev.chebweight
10186
numpy.polynomial.chebyshev.poly2cheb
102-
numpy.polynomial.hermite.Hermite.basis_name
10387
numpy.polynomial.hermite.herm2poly
10488
numpy.polynomial.hermite.hermadd
10589
numpy.polynomial.hermite.hermcompanion
@@ -126,7 +110,6 @@ numpy.polynomial.hermite.hermvander2d
126110
numpy.polynomial.hermite.hermvander3d
127111
numpy.polynomial.hermite.hermweight
128112
numpy.polynomial.hermite.poly2herm
129-
numpy.polynomial.hermite_e.HermiteE.basis_name
130113
numpy.polynomial.hermite_e.herme2poly
131114
numpy.polynomial.hermite_e.hermeadd
132115
numpy.polynomial.hermite_e.hermecompanion
@@ -153,7 +136,6 @@ numpy.polynomial.hermite_e.hermevander2d
153136
numpy.polynomial.hermite_e.hermevander3d
154137
numpy.polynomial.hermite_e.hermeweight
155138
numpy.polynomial.hermite_e.poly2herme
156-
numpy.polynomial.laguerre.Laguerre.basis_name
157139
numpy.polynomial.laguerre.lag2poly
158140
numpy.polynomial.laguerre.lagadd
159141
numpy.polynomial.laguerre.lagcompanion
@@ -180,7 +162,6 @@ numpy.polynomial.laguerre.lagvander2d
180162
numpy.polynomial.laguerre.lagvander3d
181163
numpy.polynomial.laguerre.lagweight
182164
numpy.polynomial.laguerre.poly2lag
183-
numpy.polynomial.legendre.Legendre.basis_name
184165
numpy.polynomial.legendre.leg2poly
185166
numpy.polynomial.legendre.legadd
186167
numpy.polynomial.legendre.legcompanion
@@ -207,7 +188,6 @@ numpy.polynomial.legendre.legvander2d
207188
numpy.polynomial.legendre.legvander3d
208189
numpy.polynomial.legendre.legweight
209190
numpy.polynomial.legendre.poly2leg
210-
numpy.polynomial.polynomial.Polynomial.basis_name
211191
numpy.polynomial.polynomial.polyadd
212192
numpy.polynomial.polynomial.polycompanion
213193
numpy.polynomial.polynomial.polyder

0 commit comments

Comments
 (0)