Skip to content

Commit 8c15182

Browse files
committed
✨ embed NEP50 in integer & hugely simplify __[r]add__
1 parent 97fb1da commit 8c15182

File tree

4 files changed

+290
-402
lines changed

4 files changed

+290
-402
lines changed

src/_numtype/@test/test_nep50.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ i8_to_i2: CanCast0D[Any, np.int64] = i2 # type: ignore[assignment] # pyright:
285285
i8_to_u2: CanCast0D[Any, np.int64] = u2 # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
286286
i8_to_i4: CanCast0D[Any, np.int64] = i4 # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
287287
i8_to_u4: CanCast0D[Any, np.int64] = u4 # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
288-
i8_to_i8: CanCast0D[Any, np.int64] = i8 # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
288+
i8_to_i8: CanCast0D[Any, np.int64] = i8 # NOTE: This accepts `signedinteger` for convenience
289289
i8_to_u8: CanCast0D[Any, np.int64] = u8 # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
290290
i8_to_f2: CanCast0D[Any, np.int64] = f2 # type: ignore[assignment] # pyright: ignore[reportAssignmentType]
291291
i8_to_f4: CanCast0D[Any, np.int64] = f4 # type: ignore[assignment] # pyright: ignore[reportAssignmentType]

src/_numtype/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ from ._nep50 import (
5353
CanNEP50 as CanNEP50,
5454
MatchND as MatchND,
5555
PromoteWith as PromoteWith,
56+
PromoteWith0D as PromoteWith0D,
5657
)
5758
from ._scalar import (
5859
inexact32 as inexact32,

src/_numtype/_nep50.pyi

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ from typing_extensions import TypeAliasType, TypeVar
77

88
import numpy as np
99

10-
__all__ = ["CanCast0D", "CanCastND", "CanNEP50", "MatchND", "PromoteWith"]
10+
__all__ = ["CanCast0D", "CanCastND", "CanNEP50", "MatchND", "PromoteWith", "PromoteWith0D"]
1111

1212
_T_co = TypeVar("_T_co", covariant=True)
1313
_BelowT_contra = TypeVar("_BelowT_contra", bound=np.generic, contravariant=True)
@@ -21,23 +21,45 @@ class CanNEP50(Protocol[_BelowT_contra, _AboveT_contra, _MatchT_co]):
2121
def __nep50__(self, below: _BelowT_contra, above: _AboveT_contra, /) -> _MatchT_co: ...
2222

2323
@type_check_only
24-
class _CanNEP50Int(Protocol[_OtherT_contra, _T_co]):
25-
def __nep50_int__(self, other: _OtherT_contra, /) -> _T_co: ...
24+
class _CanNEP50Rule0(Protocol[_OtherT_contra, _T_co]):
25+
def __nep50_rule0__(self, other: _OtherT_contra, /) -> _T_co: ...
2626

2727
@type_check_only
28-
class _CanNEP50Float(Protocol[_OtherT_contra, _T_co]):
29-
def __nep50_float__(self, other: _OtherT_contra, /) -> _T_co: ...
28+
class _CanNEP50Rule1(Protocol[_OtherT_contra, _T_co]):
29+
def __nep50_rule1__(self, other: _OtherT_contra, /) -> _T_co: ...
3030

3131
@type_check_only
32-
class _CanNEP50Complex(Protocol[_OtherT_contra, _T_co]):
33-
def __nep50_complex__(self, other: _OtherT_contra, /) -> _T_co: ...
32+
class _CanNEP50Rule2(Protocol[_OtherT_contra, _T_co]):
33+
def __nep50_rule2__(self, other: _OtherT_contra, /) -> _T_co: ...
34+
35+
@type_check_only
36+
class _CanNEP50Rule3(Protocol[_OtherT_contra, _T_co]):
37+
def __nep50_rule3__(self, other: _OtherT_contra, /) -> _T_co: ...
38+
39+
@type_check_only
40+
class _CanNEP50Rule4(Protocol[_OtherT_contra, _T_co]):
41+
def __nep50_rule4__(self, other: _OtherT_contra, /) -> _T_co: ...
42+
43+
@type_check_only
44+
class _CanNEP50Rule5(Protocol[_OtherT_contra, _T_co]):
45+
def __nep50_rule5__(self, other: _OtherT_contra, /) -> _T_co: ...
3446

3547
_WithT = TypeVar("_WithT")
3648
_OutT = TypeVar("_OutT", bound=np.generic)
3749

3850
PromoteWith = TypeAliasType(
3951
"PromoteWith",
40-
_CanNEP50Int[_WithT, _OutT] | _CanNEP50Float[_WithT, _OutT] | _CanNEP50Complex[_WithT, _OutT],
52+
_CanNEP50Rule0[_WithT, _OutT]
53+
| _CanNEP50Rule1[_WithT, _OutT]
54+
| _CanNEP50Rule2[_WithT, _OutT]
55+
| _CanNEP50Rule3[_WithT, _OutT]
56+
| _CanNEP50Rule4[_WithT, _OutT]
57+
| _CanNEP50Rule5[_WithT, _OutT],
58+
type_params=(_WithT, _OutT),
59+
)
60+
PromoteWith0D = TypeAliasType(
61+
"PromoteWith0D",
62+
MatchND[tuple[()], PromoteWith[_WithT, _OutT]],
4163
type_params=(_WithT, _OutT),
4264
)
4365

0 commit comments

Comments
 (0)