Skip to content

Commit 59d9024

Browse files
committed
TYP: Fix several typing issues in numpy.polynomial
1 parent eb6ac83 commit 59d9024

File tree

2 files changed

+29
-49
lines changed

2 files changed

+29
-49
lines changed

numpy/polynomial/_polybase.pyi

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ __all__: Final[Sequence[str]] = ("ABCPolyBase",)
4949

5050

5151
_NameCo = TypeVar("_NameCo", bound=None | LiteralString, covariant=True)
52-
_Self = TypeVar("_Self", bound="ABCPolyBase")
53-
_Other = TypeVar("_Other", bound="ABCPolyBase")
52+
_Self = TypeVar("_Self")
53+
_Other = TypeVar("_Other", bound=ABCPolyBase[Any])
5454

5555
_AnyOther: TypeAlias = ABCPolyBase | _CoefLike_co | _SeriesLikeCoef_co
5656
_Hundred: TypeAlias = Literal[100]
@@ -118,8 +118,6 @@ class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta):
118118
arg: _ArrayLikeCoefObject_co,
119119
) -> npt.NDArray[np.object_]: ...
120120

121-
def __str__(self, /) -> str: ...
122-
def __repr__(self, /) -> str: ...
123121
def __format__(self, fmt_str: str, /) -> str: ...
124122
def __eq__(self, x: object, /) -> bool: ...
125123
def __ne__(self, x: object, /) -> bool: ...
@@ -181,7 +179,7 @@ class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta):
181179
self: _Self,
182180
/,
183181
domain: None | _SeriesLikeCoef_co = ...,
184-
kind: type[_Self] = ...,
182+
kind: None | type[_Self] = ...,
185183
window: None | _SeriesLikeCoef_co = ...,
186184
) -> _Self: ...
187185

@@ -283,7 +281,7 @@ class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta):
283281
) -> _Self: ...
284282

285283
@classmethod
286-
def _str_term_unicode(cls, i: str, arg_str: str) -> str: ...
284+
def _str_term_unicode(cls, /, i: str, arg_str: str) -> str: ...
287285
@staticmethod
288286
def _str_term_ascii(i: str, arg_str: str) -> str: ...
289287
@staticmethod

numpy/polynomial/_polytypes.pyi

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from numpy._typing import (
2121
_ArrayLikeNumber_co,
2222
_ArrayLikeObject_co,
2323
_NestedSequence,
24+
_SupportsArray,
2425

2526
# scalar-likes
2627
_IntLike_co,
@@ -31,30 +32,14 @@ from numpy._typing import (
3132

3233
from typing_extensions import LiteralString
3334

35+
3436
_T = TypeVar("_T")
3537
_T_contra = TypeVar("_T_contra", contravariant=True)
36-
37-
_Tuple2: TypeAlias = tuple[_T, _T]
38-
39-
_V = TypeVar("_V")
40-
_V_co = TypeVar("_V_co", covariant=True)
41-
_Self = TypeVar("_Self", bound=object)
42-
38+
_Self = TypeVar("_Self")
4339
_SCT = TypeVar("_SCT", bound=np.number[Any] | np.bool | np.object_)
44-
_SCT_co = TypeVar(
45-
"_SCT_co",
46-
bound=np.number[Any] | np.bool | np.object_,
47-
covariant=True,
48-
)
49-
50-
@final
51-
class _SupportsArray(Protocol[_SCT_co]):
52-
def __array__(self ,) -> npt.NDArray[_SCT_co]: ...
5340

54-
@final
41+
# compatible with e.g. int, float, complex, Decimal, Fraction, and ABCPolyBase
5542
class _SupportsCoefOps(Protocol[_T_contra]):
56-
# compatible with e.g. `int`, `float`, `complex`, `Decimal`, `Fraction`,
57-
# and `ABCPolyBase`
5843
def __eq__(self, x: object, /) -> bool: ...
5944
def __ne__(self, x: object, /) -> bool: ...
6045

@@ -64,19 +49,16 @@ class _SupportsCoefOps(Protocol[_T_contra]):
6449
def __add__(self: _Self, x: _T_contra, /) -> _Self: ...
6550
def __sub__(self: _Self, x: _T_contra, /) -> _Self: ...
6651
def __mul__(self: _Self, x: _T_contra, /) -> _Self: ...
67-
def __truediv__(self: _Self, x: _T_contra, /) -> _Self | float: ...
6852
def __pow__(self: _Self, x: _T_contra, /) -> _Self | float: ...
6953

7054
def __radd__(self: _Self, x: _T_contra, /) -> _Self: ...
7155
def __rsub__(self: _Self, x: _T_contra, /) -> _Self: ...
7256
def __rmul__(self: _Self, x: _T_contra, /) -> _Self: ...
73-
def __rtruediv__(self: _Self, x: _T_contra, /) -> _Self | float: ...
7457

7558
_Series: TypeAlias = np.ndarray[tuple[int], np.dtype[_SCT]]
7659

7760
_FloatSeries: TypeAlias = _Series[np.floating[Any]]
7861
_ComplexSeries: TypeAlias = _Series[np.complexfloating[Any, Any]]
79-
_NumberSeries: TypeAlias = _Series[np.number[Any]]
8062
_ObjectSeries: TypeAlias = _Series[np.object_]
8163
_CoefSeries: TypeAlias = _Series[np.inexact[Any] | np.object_]
8264

@@ -85,38 +67,38 @@ _ComplexArray: TypeAlias = npt.NDArray[np.complexfloating[Any, Any]]
8567
_ObjectArray: TypeAlias = npt.NDArray[np.object_]
8668
_CoefArray: TypeAlias = npt.NDArray[np.inexact[Any] | np.object_]
8769

70+
_Tuple2: TypeAlias = tuple[_T, _T]
8871
_Array1: TypeAlias = np.ndarray[tuple[Literal[1]], np.dtype[_SCT]]
8972
_Array2: TypeAlias = np.ndarray[tuple[Literal[2]], np.dtype[_SCT]]
9073

9174
_AnyInt: TypeAlias = SupportsInt | SupportsIndex
9275

93-
_CoefObjectLike_co: TypeAlias = np.object_ | _SupportsCoefOps
76+
_CoefObjectLike_co: TypeAlias = np.object_ | _SupportsCoefOps[Any]
9477
_CoefLike_co: TypeAlias = _NumberLike_co | _CoefObjectLike_co
9578

9679
# The term "series" is used here to refer to 1-d arrays of numeric scalars.
9780
_SeriesLikeBool_co: TypeAlias = (
98-
_SupportsArray[np.bool]
81+
_SupportsArray[np.dtype[np.bool]]
9982
| Sequence[bool | np.bool]
10083
)
10184
_SeriesLikeInt_co: TypeAlias = (
102-
_SupportsArray[np.integer[Any] | np.bool]
85+
_SupportsArray[np.dtype[np.integer[Any] | np.bool]]
10386
| Sequence[_IntLike_co]
10487
)
10588
_SeriesLikeFloat_co: TypeAlias = (
106-
_SupportsArray[np.floating[Any] | np.integer[Any] | np.bool]
89+
_SupportsArray[np.dtype[np.floating[Any] | np.integer[Any] | np.bool]]
10790
| Sequence[_FloatLike_co]
10891
)
10992
_SeriesLikeComplex_co: TypeAlias = (
110-
_SupportsArray[np.integer[Any] | np.inexact[Any] | np.bool]
93+
_SupportsArray[np.dtype[np.inexact[Any] | np.integer[Any] | np.bool]]
11194
| Sequence[_ComplexLike_co]
11295
)
11396
_SeriesLikeObject_co: TypeAlias = (
114-
_SupportsArray[np.object_]
97+
_SupportsArray[np.dtype[np.object_]]
11598
| Sequence[_CoefObjectLike_co]
11699
)
117100
_SeriesLikeCoef_co: TypeAlias = (
118-
# npt.NDArray[np.number[Any] | np.bool | np.object_]
119-
_SupportsArray[np.number[Any] | np.bool | np.object_]
101+
_SupportsArray[np.dtype[np.number[Any] | np.bool | np.object_]]
120102
| Sequence[_CoefLike_co]
121103
)
122104

@@ -158,8 +140,8 @@ class _FuncLine(_Named[_Name_co], Protocol[_Name_co]):
158140
def __call__(
159141
self,
160142
/,
161-
off: _SupportsCoefOps,
162-
scl: _SupportsCoefOps,
143+
off: _SupportsCoefOps[Any],
144+
scl: _SupportsCoefOps[Any],
163145
) -> _Line[np.object_]: ...
164146

165147
@final
@@ -307,7 +289,7 @@ class _FuncInteg(_Named[_Name_co], Protocol[_Name_co]):
307289
/,
308290
c: _ArrayLikeCoef_co,
309291
m: SupportsIndex = ...,
310-
k: _SeriesLikeCoef_co | _SeriesLikeCoef_co = ...,
292+
k: _CoefLike_co | _SeriesLikeCoef_co = ...,
311293
lbnd: _CoefLike_co = ...,
312294
scl: _CoefLike_co = ...,
313295
axis: SupportsIndex = ...,
@@ -362,7 +344,7 @@ class _FuncValFromRoots(_Named[_Name_co], Protocol[_Name_co]):
362344
x: _CoefLike_co,
363345
r: _CoefLike_co,
364346
tensor: bool = ...,
365-
) -> _SupportsCoefOps: ...
347+
) -> _SupportsCoefOps[Any]: ...
366348

367349
@final
368350
class _FuncVal(_Named[_Name_co], Protocol[_Name_co]):
@@ -413,7 +395,7 @@ class _FuncVal(_Named[_Name_co], Protocol[_Name_co]):
413395
x: _CoefLike_co,
414396
c: _SeriesLikeObject_co,
415397
tensor: bool = ...,
416-
) -> _SupportsCoefOps: ...
398+
) -> _SupportsCoefOps[Any]: ...
417399

418400
@final
419401
class _FuncVal2D(_Named[_Name_co], Protocol[_Name_co]):
@@ -464,7 +446,7 @@ class _FuncVal2D(_Named[_Name_co], Protocol[_Name_co]):
464446
x: _CoefLike_co,
465447
y: _CoefLike_co,
466448
c: _SeriesLikeCoef_co,
467-
) -> _SupportsCoefOps: ...
449+
) -> _SupportsCoefOps[Any]: ...
468450

469451
@final
470452
class _FuncVal3D(_Named[_Name_co], Protocol[_Name_co]):
@@ -521,7 +503,7 @@ class _FuncVal3D(_Named[_Name_co], Protocol[_Name_co]):
521503
y: _CoefLike_co,
522504
z: _CoefLike_co,
523505
c: _SeriesLikeCoef_co,
524-
) -> _SupportsCoefOps: ...
506+
) -> _SupportsCoefOps[Any]: ...
525507

526508
_AnyValF: TypeAlias = Callable[
527509
[npt.ArrayLike, npt.ArrayLike, bool],
@@ -566,18 +548,18 @@ class _FuncValND(_Named[_Name_co], Protocol[_Name_co]):
566548
def __call__(
567549
self,
568550
val_f: _AnyValF,
569-
c: _ArrayLikeCoef_co,
551+
c: _SeriesLikeObject_co,
570552
/,
571-
*args: _ArrayLikeCoef_co,
572-
) -> _ObjectArray: ...
553+
*args: _CoefObjectLike_co,
554+
) -> _SupportsCoefOps[Any]: ...
573555
@overload
574556
def __call__(
575557
self,
576558
val_f: _AnyValF,
577-
c: _SeriesLikeObject_co,
559+
c: _ArrayLikeCoef_co,
578560
/,
579-
*args: _CoefObjectLike_co,
580-
) -> _SupportsCoefOps: ...
561+
*args: _ArrayLikeCoef_co,
562+
) -> _ObjectArray: ...
581563

582564
@final
583565
class _FuncVander(_Named[_Name_co], Protocol[_Name_co]):

0 commit comments

Comments
 (0)