Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4985e20

Browse files
authored
Merge pull request#28988 from jorenham/typing/add-missing-ndarray-ops-overloads-for-character-types
TYP: add missing ``ndarray.__{add,mul}__`` ``character`` type overloads
2 parentsd840358 +6ebc810 commit4985e20

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

‎numpy/__init__.pyi

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2839,6 +2839,16 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
28392839
@overload
28402840
def__add__(self:NDArray[datetime64],other:_ArrayLikeTD64_co,/)->NDArray[datetime64]: ...
28412841
@overload
2842+
def__add__(self:NDArray[bytes_],other:_ArrayLikeBytes_co,/)->NDArray[bytes_]: ...
2843+
@overload
2844+
def__add__(self:NDArray[str_],other:_ArrayLikeStr_co,/)->NDArray[str_]: ...
2845+
@overload
2846+
def__add__(
2847+
self:ndarray[Any,dtypes.StringDType],
2848+
other:_ArrayLikeStr_co|_ArrayLikeString_co,
2849+
/,
2850+
)->ndarray[tuple[Any, ...],dtypes.StringDType]: ...
2851+
@overload
28422852
def__add__(self:NDArray[object_],other:Any,/)->Any: ...
28432853
@overload
28442854
def__add__(self:NDArray[Any],other:_ArrayLikeObject_co,/)->Any: ...
@@ -2876,6 +2886,16 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
28762886
@overload
28772887
def__radd__(self:NDArray[datetime64],other:_ArrayLikeTD64_co,/)->NDArray[datetime64]: ...
28782888
@overload
2889+
def__radd__(self:NDArray[bytes_],other:_ArrayLikeBytes_co,/)->NDArray[bytes_]: ...
2890+
@overload
2891+
def__radd__(self:NDArray[str_],other:_ArrayLikeStr_co,/)->NDArray[str_]: ...
2892+
@overload
2893+
def__radd__(
2894+
self:ndarray[Any,dtypes.StringDType],
2895+
other:_ArrayLikeStr_co|_ArrayLikeString_co,
2896+
/,
2897+
)->ndarray[tuple[Any, ...],dtypes.StringDType]: ...
2898+
@overload
28792899
def__radd__(self:NDArray[object_],other:Any,/)->Any: ...
28802900
@overload
28812901
def__radd__(self:NDArray[Any],other:_ArrayLikeObject_co,/)->Any: ...
@@ -2985,6 +3005,12 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
29853005
@overload
29863006
def__mul__(self:_ArrayFloat_co,other:_ArrayLike[timedelta64],/)->NDArray[timedelta64]: ...
29873007
@overload
3008+
def__mul__(
3009+
self:ndarray[Any,dtype[character]|dtypes.StringDType],
3010+
other:_ArrayLikeInt,
3011+
/,
3012+
)->ndarray[tuple[Any, ...],_DTypeT_co]: ...
3013+
@overload
29883014
def__mul__(self:NDArray[object_],other:Any,/)->Any: ...
29893015
@overload
29903016
def__mul__(self:NDArray[Any],other:_ArrayLikeObject_co,/)->Any: ...
@@ -3020,6 +3046,12 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
30203046
@overload
30213047
def__rmul__(self:_ArrayFloat_co,other:_ArrayLike[timedelta64],/)->NDArray[timedelta64]: ...
30223048
@overload
3049+
def__rmul__(
3050+
self:ndarray[Any,dtype[character]|dtypes.StringDType],
3051+
other:_ArrayLikeInt,
3052+
/,
3053+
)->ndarray[tuple[Any, ...],_DTypeT_co]: ...
3054+
@overload
30233055
def__rmul__(self:NDArray[object_],other:Any,/)->Any: ...
30243056
@overload
30253057
def__rmul__(self:NDArray[Any],other:_ArrayLikeObject_co,/)->Any: ...
@@ -3341,6 +3373,14 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
33413373
@overload
33423374
def__iadd__(self:NDArray[timedelta64|datetime64],other:_ArrayLikeTD64_co,/)->ndarray[_ShapeT_co,_DTypeT_co]: ...
33433375
@overload
3376+
def__iadd__(self:NDArray[bytes_],other:_ArrayLikeBytes_co,/)->ndarray[_ShapeT_co,_DTypeT_co]: ...
3377+
@overload
3378+
def__iadd__(
3379+
self:ndarray[Any,dtype[str_]|dtypes.StringDType],
3380+
other:_ArrayLikeStr_co|_ArrayLikeString_co,
3381+
/,
3382+
)->ndarray[_ShapeT_co,_DTypeT_co]: ...
3383+
@overload
33443384
def__iadd__(self:NDArray[object_],other:Any,/)->ndarray[_ShapeT_co,_DTypeT_co]: ...
33453385

33463386
#
@@ -3371,7 +3411,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
33713411
/,
33723412
)->ndarray[_ShapeT_co,_DTypeT_co]: ...
33733413
@overload
3374-
def__imul__(self:NDArray[signedinteger],other:_ArrayLikeInt_co,/)->ndarray[_ShapeT_co,_DTypeT_co]: ...
3414+
def__imul__(
3415+
self:ndarray[Any,dtype[signedinteger|character]|dtypes.StringDType],
3416+
other:_ArrayLikeInt_co,
3417+
/,
3418+
)->ndarray[_ShapeT_co,_DTypeT_co]: ...
33753419
@overload
33763420
def__imul__(self:NDArray[floating|timedelta64],other:_ArrayLikeFloat_co,/)->ndarray[_ShapeT_co,_DTypeT_co]: ...
33773421
@overload

‎numpy/typing/tests/data/reveal/arithmetic.pyi

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ AR_c: npt.NDArray[np.complex128]
4848
AR_m:npt.NDArray[np.timedelta64]
4949
AR_M:npt.NDArray[np.datetime64]
5050
AR_O:npt.NDArray[np.object_]
51+
AR_S:npt.NDArray[np.bytes_]
52+
AR_U:npt.NDArray[np.str_]
53+
AR_T:np.ndarray[tuple[Any, ...],np.dtypes.StringDType]
5154
AR_floating:npt.NDArray[np.floating]
5255
AR_number:npt.NDArray[np.number]
5356
AR_Any:npt.NDArray[Any]
@@ -673,3 +676,45 @@ assert_type(f / AR_floating, npt.NDArray[np.floating])
673676
assert_type(f//AR_floating,npt.NDArray[np.floating])
674677
assert_type(f%AR_floating,npt.NDArray[np.floating])
675678
assert_type(divmod(f,AR_floating),tuple[npt.NDArray[np.floating],npt.NDArray[np.floating]])
679+
680+
# character-like
681+
682+
assert_type(AR_S+b"",npt.NDArray[np.bytes_])
683+
assert_type(AR_S+ [b""],npt.NDArray[np.bytes_])
684+
assert_type([b""]+AR_S,npt.NDArray[np.bytes_])
685+
assert_type(AR_S+AR_S,npt.NDArray[np.bytes_])
686+
687+
assert_type(AR_U+"",npt.NDArray[np.str_])
688+
assert_type(AR_U+ [""],npt.NDArray[np.str_])
689+
assert_type(""+AR_U,npt.NDArray[np.str_])
690+
assert_type([""]+AR_U,npt.NDArray[np.str_])
691+
assert_type(AR_U+AR_U,npt.NDArray[np.str_])
692+
693+
assert_type(AR_T+"",np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
694+
assert_type(AR_T+ [""],np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
695+
assert_type(""+AR_T,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
696+
assert_type([""]+AR_T,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
697+
assert_type(AR_T+AR_T,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
698+
assert_type(AR_T+AR_U,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
699+
assert_type(AR_U+AR_T,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
700+
701+
assert_type(AR_S*i,np.ndarray[tuple[Any, ...],np.dtype[np.bytes_]])
702+
assert_type(AR_S*AR_LIKE_i,np.ndarray[tuple[Any, ...],np.dtype[np.bytes_]])
703+
assert_type(AR_S*AR_i,np.ndarray[tuple[Any, ...],np.dtype[np.bytes_]])
704+
assert_type(i*AR_S,np.ndarray[tuple[Any, ...],np.dtype[np.bytes_]])
705+
# mypy incorrectly infers `AR_LIKE_i * AR_S` as `list[int]`
706+
assert_type(AR_i*AR_S,np.ndarray[tuple[Any, ...],np.dtype[np.bytes_]])
707+
708+
assert_type(AR_U*i,np.ndarray[tuple[Any, ...],np.dtype[np.str_]])
709+
assert_type(AR_U*AR_LIKE_i,np.ndarray[tuple[Any, ...],np.dtype[np.str_]])
710+
assert_type(AR_U*AR_i,np.ndarray[tuple[Any, ...],np.dtype[np.str_]])
711+
assert_type(i*AR_U,np.ndarray[tuple[Any, ...],np.dtype[np.str_]])
712+
# mypy incorrectly infers `AR_LIKE_i * AR_U` as `list[int]`
713+
assert_type(AR_i*AR_U,np.ndarray[tuple[Any, ...],np.dtype[np.str_]])
714+
715+
assert_type(AR_T*i,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
716+
assert_type(AR_T*AR_LIKE_i,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
717+
assert_type(AR_T*AR_i,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
718+
assert_type(i*AR_T,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])
719+
# mypy incorrectly infers `AR_LIKE_i * AR_T` as `list[int]`
720+
assert_type(AR_i*AR_T,np.ndarray[tuple[Any, ...],np.dtypes.StringDType])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp