Skip to content

Commit c7f7e7f

Browse files
authored
Merge pull request numpy#27220 from jorenham/typing/chararray-typevar-cleanup
TYP: Fixed & improved ``TypeVar`` use for ``numpy.char.chararray``
2 parents f1d11b3 + 984b3c7 commit c7f7e7f

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

numpy/__init__.pyi

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4409,11 +4409,6 @@ class matrix(ndarray[_Shape2DType_co, _DType_co]):
44094409
def getA1(self) -> ndarray[_Shape, _DType_co]: ...
44104410
def getH(self) -> matrix[_Shape2D, _DType_co]: ...
44114411

4412-
_CharType = TypeVar("_CharType", str_, bytes_)
4413-
_CharDType = TypeVar("_CharDType", dtype[str_], dtype[bytes_])
4414-
4415-
# NOTE: Deprecated
4416-
# class MachAr: ...
44174412

44184413
class _SupportsDLPack(Protocol[_T_contra]):
44194414
def __dlpack__(self, *, stream: None | _T_contra = ...) -> _PyCapsule: ...

numpy/_core/defchararray.pyi

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import (
22
Literal as L,
33
overload,
4+
TypeAlias,
45
TypeVar,
56
Any,
67
SupportsIndex,
@@ -17,7 +18,6 @@ from numpy import (
1718
object_,
1819
_OrderKACF,
1920
_ShapeType_co,
20-
_CharDType,
2121
_SupportsBuffer,
2222
)
2323

@@ -33,10 +33,15 @@ from numpy._typing import (
3333

3434
from numpy._core.multiarray import compare_chararrays as compare_chararrays
3535

36-
_SCT = TypeVar("_SCT", str_, bytes_)
37-
_CharArray = chararray[_Shape, dtype[_SCT]]
36+
_SCT = TypeVar("_SCT", bound=str_ | bytes_)
37+
_CharDType_co = TypeVar(
38+
"_CharDType_co",
39+
bound=dtype[str_ | bytes_],
40+
covariant=True,
41+
)
42+
_CharArray: TypeAlias = chararray[tuple[int, ...], dtype[_SCT]]
3843

39-
class chararray(ndarray[_ShapeType_co, _CharDType]):
44+
class chararray(ndarray[_ShapeType_co, _CharDType_co]):
4045
@overload
4146
def __new__(
4247
subtype,
@@ -61,9 +66,9 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
6166
) -> chararray[_Shape, dtype[str_]]: ...
6267

6368
def __array_finalize__(self, obj: object) -> None: ...
64-
def __mul__(self, other: i_co) -> chararray[_Shape, _CharDType]: ...
65-
def __rmul__(self, other: i_co) -> chararray[_Shape, _CharDType]: ...
66-
def __mod__(self, i: Any) -> chararray[_Shape, _CharDType]: ...
69+
def __mul__(self, other: i_co) -> chararray[_Shape, _CharDType_co]: ...
70+
def __rmul__(self, other: i_co) -> chararray[_Shape, _CharDType_co]: ...
71+
def __mod__(self, i: Any) -> chararray[_Shape, _CharDType_co]: ...
6772

6873
@overload
6974
def __eq__(
@@ -211,7 +216,7 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
211216
def expandtabs(
212217
self,
213218
tabsize: i_co = ...,
214-
) -> chararray[_Shape, _CharDType]: ...
219+
) -> chararray[_Shape, _CharDType_co]: ...
215220

216221
@overload
217222
def find(
@@ -436,12 +441,12 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
436441
deletechars: None | S_co = ...,
437442
) -> _CharArray[bytes_]: ...
438443

439-
def zfill(self, width: _ArrayLikeInt_co) -> chararray[_Shape, _CharDType]: ...
440-
def capitalize(self) -> chararray[_ShapeType_co, _CharDType]: ...
441-
def title(self) -> chararray[_ShapeType_co, _CharDType]: ...
442-
def swapcase(self) -> chararray[_ShapeType_co, _CharDType]: ...
443-
def lower(self) -> chararray[_ShapeType_co, _CharDType]: ...
444-
def upper(self) -> chararray[_ShapeType_co, _CharDType]: ...
444+
def zfill(self, width: _ArrayLikeInt_co) -> chararray[_Shape, _CharDType_co]: ...
445+
def capitalize(self) -> chararray[_ShapeType_co, _CharDType_co]: ...
446+
def title(self) -> chararray[_ShapeType_co, _CharDType_co]: ...
447+
def swapcase(self) -> chararray[_ShapeType_co, _CharDType_co]: ...
448+
def lower(self) -> chararray[_ShapeType_co, _CharDType_co]: ...
449+
def upper(self) -> chararray[_ShapeType_co, _CharDType_co]: ...
445450
def isalnum(self) -> ndarray[_ShapeType_co, dtype[np.bool]]: ...
446451
def isalpha(self) -> ndarray[_ShapeType_co, dtype[np.bool]]: ...
447452
def isdigit(self) -> ndarray[_ShapeType_co, dtype[np.bool]]: ...

0 commit comments

Comments
 (0)