Skip to content

Commit 6b11b7a

Browse files
guan404mingjorenham
andcommitted
♻️ refactor annotation for logical related class
Co-Authored-By: Joren Hammudoglu <[email protected]>
1 parent dbe2316 commit 6b11b7a

File tree

3 files changed

+62
-56
lines changed

3 files changed

+62
-56
lines changed

src/numpy-stubs/__init__.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ from ._typing._char_codes import (
310310
from ._typing._ufunc import (
311311
_Call11Bool,
312312
_Call11Isnat,
313-
_Call11LogicalNot,
313+
_Call11Logical,
314314
_Call21Bool,
315315
_Call21Logical,
316316
_gufunc_2_1,
@@ -7113,7 +7113,7 @@ log: Final[_ufunc_1_1] = ...
71137113
log2: Final[_ufunc_1_1] = ...
71147114
log10: Final[_ufunc_1_1] = ...
71157115
log1p: Final[_ufunc_1_1] = ...
7116-
logical_not: Final[_ufunc_1_1[_Call11LogicalNot]] = ...
7116+
logical_not: Final[_ufunc_1_1[_Call11Logical]] = ...
71177117
negative: Final[_ufunc_1_1] = ...
71187118
positive: Final[_ufunc_1_1] = ...
71197119
rad2deg: Final[_ufunc_1_1] = ...

src/numpy-stubs/_typing/_ufunc.pyi

+56-50
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ import numpy as np
1616
from numpy import _CastingKind, _OrderKACF # noqa: ICN003
1717
from numpy._typing import _DTypeLikeBool, _NestedSequence
1818

19-
from ._array_like import ArrayLike, NDArray, _ArrayLike, _ArrayLikeBool_co, _ArrayLikeInt_co, _ArrayLikeNumber_co
20-
from ._dtype_like import DTypeLike, _DTypeLike, _DTypeLikeObject
19+
from ._array_like import (
20+
ArrayLike,
21+
NDArray,
22+
_ArrayLike,
23+
_ArrayLikeBool_co,
24+
_ArrayLikeInt_co,
25+
_ArrayLikeNumber_co,
26+
_ArrayLikeObject_co,
27+
)
28+
from ._dtype_like import DTypeLike, _DTypeLike
2129
from ._scalars import _NumberLike_co, _ScalarLike_co
2230
from ._shape import _ShapeLike
2331

@@ -256,47 +264,47 @@ class _Call11Isnat(Protocol):
256264
) -> NDArray[np.bool]: ...
257265

258266
@type_check_only
259-
class _Call11LogicalNot(Protocol):
267+
class _Call11Logical(Protocol):
260268
@overload
261-
def __call__( # (scalar, dtype: np.object_) -> np.object_
269+
def __call__( # (scalar, dtype: np.object_) -> bool
262270
self,
263-
x: _NumberLike_co,
271+
x: _ScalarLike_co,
264272
/,
265-
dtype: _DTypeLikeObject,
273+
dtype: _DTypeLike[np.object_],
266274
out: None = None,
267275
**kwargs: Unpack[_Kwargs2],
268-
) -> np.object_: ...
276+
) -> bool: ...
269277
@overload
270-
def __call__( # (scalar) -> bool
278+
def __call__( # (scalar) -> np.bool
271279
self,
272280
x: _NumberLike_co,
273281
/,
274282
out: None = None,
275-
dtype: DTypeLike | None = None,
283+
dtype: _DTypeLikeBool | None = None,
276284
**kwargs: Unpack[_Kwargs2],
277285
) -> np.bool: ...
278286
@overload
279287
def __call__( # (array-like, dtype: np.object_) -> np.object_
280288
self,
281-
x: _ArrayLikeNumber_co,
289+
x: _ArrayLikeNumber_co | _ArrayLikeObject_co,
282290
/,
283-
dtype: _DTypeLikeObject,
291+
dtype: _DTypeLike[np.object_],
284292
out: None = None,
285293
**kwargs: Unpack[_Kwargs2],
286-
) -> np.object_: ...
294+
) -> NDArray[np.object_] | bool: ...
287295
@overload
288296
def __call__( # (array-like, out: T) -> T
289297
self,
290298
x: _ArrayLikeNumber_co,
291299
/,
292-
out: _ArrayT | tuple[_ArrayT],
300+
out: _Out1[_ArrayT],
293301
dtype: DTypeLike | None = None,
294302
**kwargs: Unpack[_Kwargs2],
295303
) -> _ArrayT: ...
296304
@overload # (array) -> Array[bool]
297305
def __call__(
298306
self,
299-
x: _AnyArray,
307+
x: NDArray[np.bool | np.number] | _NestedSequence[np.bool | np.number],
300308
/,
301309
out: _Out1[_AnyArray] | None = None,
302310
*,
@@ -309,7 +317,7 @@ class _Call11LogicalNot(Protocol):
309317
x: _ArrayLikeNumber_co,
310318
/,
311319
out: None = None,
312-
dtype: DTypeLike | None = None,
320+
dtype: _DTypeLikeBool | None = None,
313321
**kwargs: Unpack[_Kwargs2],
314322
) -> NDArray[np.bool] | np.bool: ...
315323
@overload
@@ -320,8 +328,7 @@ class _Call11LogicalNot(Protocol):
320328
out: _Out1[_AnyArray] | None = None,
321329
dtype: DTypeLike | None = None,
322330
**kwargs: Unpack[_Kwargs2],
323-
) -> NDArray[np.bool] | np.bool: ...
324-
331+
) -> Any: ...
325332
@type_check_only
326333
class _Call12(Protocol):
327334
@overload
@@ -490,70 +497,81 @@ class _Call21Logical(Protocol):
490497
@overload # (scalar, scalar, dtype: np.object_) -> np.object_
491498
def __call__(
492499
self,
493-
x1: _NumberLike_co,
494-
x2: _NumberLike_co,
500+
x1: _ScalarLike_co,
501+
x2: _ScalarLike_co,
495502
/,
496-
dtype: _DTypeLikeObject,
503+
dtype: _DTypeLike[np.object_],
497504
out: None = None,
498505
**kwds: Unpack[_Kwargs3],
499-
) -> np.object_: ...
506+
) -> bool: ...
500507
@overload # (scalar, scalar) -> bool
501508
def __call__(
502509
self,
503510
x1: _NumberLike_co,
504511
x2: _NumberLike_co,
505512
/,
506513
out: None = None,
507-
dtype: DTypeLike | None = None,
514+
dtype: _DTypeLikeBool | None = None,
508515
**kwds: Unpack[_Kwargs3],
509516
) -> np.bool: ...
510-
@overload # (array-like, array, dtype: np.object_) -> np.object_
517+
@overload # (array-like, array, dtype: object_) -> Array[object_]
511518
def __call__(
512519
self,
513-
x1: _ArrayLikeNumber_co,
520+
x1: _ArrayLikeNumber_co | _ArrayLikeObject_co,
514521
x2: _AnyArray,
515522
/,
516-
dtype: _DTypeLikeObject,
523+
dtype: _DTypeLike[np.object_],
517524
out: None = None,
518525
**kwds: Unpack[_Kwargs3],
519-
) -> np.object_: ...
526+
) -> NDArray[np.object_]: ...
520527
@overload # (array-like, array, dtype: dtype[T]) -> Array[T]
521528
def __call__(
522529
self,
523530
x1: _ArrayLikeNumber_co,
524-
x2: _AnyArray,
531+
x2: NDArray[np.bool | np.number],
525532
/,
526533
out: None = None,
527-
dtype: DTypeLike | None = None,
534+
dtype: _DTypeLikeBool | None = None,
528535
**kwds: Unpack[_Kwargs3],
529536
) -> NDArray[np.bool]: ...
530-
@overload # (array, array-like, dtype: np.object_) -> np.object_
537+
@overload # (array, array-like, dtype: object_) -> Array[object_]
531538
def __call__(
532539
self,
533540
x1: _AnyArray,
534-
x2: _ArrayLikeNumber_co,
541+
x2: _ArrayLikeNumber_co | _ArrayLikeObject_co,
535542
/,
536-
dtype: _DTypeLikeObject,
543+
dtype: _DTypeLike[np.object_],
537544
out: None = None,
538545
**kwds: Unpack[_Kwargs3],
539-
) -> np.object_: ...
546+
) -> NDArray[np.object_]: ...
540547
@overload # (array, array-like, dtype: dtype[T]) -> Array[T]
541548
def __call__(
542549
self,
543-
x1: _AnyArray,
550+
x1: NDArray[np.bool | np.number] | _NestedSequence[np.bool | np.number],
544551
x2: _ArrayLikeNumber_co,
545552
/,
546553
out: None = None,
547554
dtype: DTypeLike | None = None,
548555
**kwds: Unpack[_Kwargs3],
549556
) -> NDArray[np.bool]: ...
557+
@overload # (array-like, array-like, out: T) -> T
558+
def __call__(
559+
self,
560+
x1: _ArrayLikeNumber_co,
561+
x2: _ArrayLikeNumber_co,
562+
/,
563+
out: _Out1[_ArrayT],
564+
*,
565+
dtype: None = None,
566+
**kwds: Unpack[_Kwargs3],
567+
) -> _ArrayT: ...
550568
@overload # (array-like, array) -> Array[?]
551569
def __call__(
552570
self,
553571
x1: _ArrayLikeNumber_co,
554572
x2: _AnyArray,
555573
/,
556-
out: _Out1[_AnyArray] | None = None,
574+
out: _Out1[NDArray[np.bool]] | None = None,
557575
*,
558576
dtype: DTypeLike | None = None,
559577
**kwds: Unpack[_Kwargs3],
@@ -564,34 +582,22 @@ class _Call21Logical(Protocol):
564582
x1: _AnyArray,
565583
x2: _ArrayLikeNumber_co,
566584
/,
567-
out: _Out1[_AnyArray] | None = None,
585+
out: _Out1[NDArray[np.bool]] | None = None,
568586
*,
569587
dtype: DTypeLike | None = None,
570588
**kwds: Unpack[_Kwargs3],
571589
) -> NDArray[np.bool]: ...
572-
@overload # (array-like, array-like, out: T) -> T
573-
def __call__(
574-
self,
575-
x1: _ArrayLikeNumber_co,
576-
x2: ArrayLike,
577-
/,
578-
out: _ArrayT | tuple[_ArrayT],
579-
*,
580-
dtype: None = None,
581-
**kwds: Unpack[_Kwargs3],
582-
) -> _ArrayT: ...
583590
@overload # (array-like, array-like) -> Array[?] | ?
584591
def __call__(
585592
self,
586-
x1: _ArrayLikeNumber_co,
587-
x2: _ArrayLikeNumber_co,
593+
x1: _ArrayLikeNumber_co | _ArrayLikeObject_co,
594+
x2: _ArrayLikeNumber_co | _ArrayLikeObject_co,
588595
/,
589596
out: _Out1[_AnyArray] | None = None,
590597
*,
591598
dtype: DTypeLike | None = None,
592599
**kwds: Unpack[_Kwargs3],
593-
) -> NDArray[np.bool] | np.bool: ...
594-
600+
) -> Any: ...
595601
@type_check_only
596602
class _Call21(Protocol):
597603
@overload # (scalar, scalar, dtype: type[T]) -> T

test/static/accept/ufuncs.pyi

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,25 @@ assert_type(np.isfinite(AR_f8, out=AR_bool), npt.NDArray[np.bool_])
6666
assert_type(np.logical_not(True), np.bool_)
6767
assert_type(np.logical_not(AR_bool), npt.NDArray[np.bool_])
6868
assert_type(np.logical_not(AR_bool, out=AR_bool), npt.NDArray[np.bool_])
69-
assert_type(np.logical_not(AR_bool, dtype=np.object_), np.object_)
69+
assert_type(np.logical_not(AR_bool, dtype=np.object_), npt.NDArray[np.object_] | bool)
7070

7171
assert_type(np.logical_and(True, True), np.bool_)
7272
assert_type(np.logical_and(AR_bool, AR_bool), npt.NDArray[np.bool_])
7373
assert_type(np.logical_and(AR_bool, AR_bool, out=AR_bool), npt.NDArray[np.bool_])
7474
assert_type(np.logical_and(AR_i8, AR_bool, out=AR_bool), npt.NDArray[np.bool_])
7575
assert_type(np.logical_and(AR_bool, AR_i8), npt.NDArray[np.bool_])
76-
assert_type(np.logical_and(AR_bool, AR_bool, dtype=np.object_), np.object_)
76+
assert_type(np.logical_and(AR_bool, AR_bool, dtype=np.object_), npt.NDArray[np.object_])
7777

7878
assert_type(np.logical_or(True, True), np.bool_)
7979
assert_type(np.logical_or(AR_bool, AR_bool), npt.NDArray[np.bool_])
8080
assert_type(np.logical_or(AR_bool, AR_bool, out=AR_bool), npt.NDArray[np.bool_])
8181
assert_type(np.logical_or(AR_i8, AR_bool, out=AR_bool), npt.NDArray[np.bool_])
8282
assert_type(np.logical_or(AR_bool, AR_i8), npt.NDArray[np.bool_])
83-
assert_type(np.logical_or(AR_bool, AR_bool, dtype=np.object_), np.object_)
83+
assert_type(np.logical_or(AR_bool, AR_bool, dtype=np.object_), npt.NDArray[np.object_])
8484

8585
assert_type(np.logical_xor(True, True), np.bool_)
8686
assert_type(np.logical_xor(AR_bool, AR_bool), npt.NDArray[np.bool_])
8787
assert_type(np.logical_xor(AR_bool, AR_bool, out=AR_bool), npt.NDArray[np.bool_])
8888
assert_type(np.logical_xor(AR_i8, AR_bool, out=AR_bool), npt.NDArray[np.bool_])
8989
assert_type(np.logical_xor(AR_bool, AR_i8), npt.NDArray[np.bool_])
90-
assert_type(np.logical_xor(AR_bool, AR_bool, dtype=np.object_), np.object_)
90+
assert_type(np.logical_xor(AR_bool, AR_bool, dtype=np.object_), npt.NDArray[np.object_])

0 commit comments

Comments
 (0)