hyb
2025-12-23 7e5db3a16b423ec4a43459805e277979bcac7db5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from typing import Protocol, TypeAlias, TypeVar, assert_type
 
import numpy as np
from numpy._typing import _64Bit
 
_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
 
class CanAbs(Protocol[_T_co]):
    def __abs__(self, /) -> _T_co: ...
 
class CanInvert(Protocol[_T_co]):
    def __invert__(self, /) -> _T_co: ...
 
class CanNeg(Protocol[_T_co]):
    def __neg__(self, /) -> _T_co: ...
 
class CanPos(Protocol[_T_co]):
    def __pos__(self, /) -> _T_co: ...
 
def do_abs(x: CanAbs[_T]) -> _T: ...
def do_invert(x: CanInvert[_T]) -> _T: ...
def do_neg(x: CanNeg[_T]) -> _T: ...
def do_pos(x: CanPos[_T]) -> _T: ...
 
_Bool_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.bool]]
_UInt8_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.uint8]]
_Int16_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.int16]]
_LongLong_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.longlong]]
_Float32_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.float32]]
_Float64_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.float64]]
_LongDouble_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.longdouble]]
_Complex64_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.complex64]]
_Complex128_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.complex128]]
_CLongDouble_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[np.clongdouble]]
 
b1_1d: _Bool_1d
u1_1d: _UInt8_1d
i2_1d: _Int16_1d
q_1d: _LongLong_1d
f4_1d: _Float32_1d
f8_1d: _Float64_1d
g_1d: _LongDouble_1d
c8_1d: _Complex64_1d
c16_1d: _Complex128_1d
G_1d: _CLongDouble_1d
 
assert_type(do_abs(b1_1d), _Bool_1d)
assert_type(do_abs(u1_1d), _UInt8_1d)
assert_type(do_abs(i2_1d), _Int16_1d)
assert_type(do_abs(q_1d), _LongLong_1d)
assert_type(do_abs(f4_1d), _Float32_1d)
assert_type(do_abs(f8_1d), _Float64_1d)
assert_type(do_abs(g_1d), _LongDouble_1d)
 
assert_type(do_abs(c8_1d), _Float32_1d)
# NOTE: Unfortunately it's not possible to have this return a `float64` sctype, see
# https://github.com/python/mypy/issues/14070
assert_type(do_abs(c16_1d), np.ndarray[tuple[int], np.dtype[np.floating[_64Bit]]])
assert_type(do_abs(G_1d), _LongDouble_1d)
 
assert_type(do_invert(b1_1d), _Bool_1d)
assert_type(do_invert(u1_1d), _UInt8_1d)
assert_type(do_invert(i2_1d), _Int16_1d)
assert_type(do_invert(q_1d), _LongLong_1d)
 
assert_type(do_neg(u1_1d), _UInt8_1d)
assert_type(do_neg(i2_1d), _Int16_1d)
assert_type(do_neg(q_1d), _LongLong_1d)
assert_type(do_neg(f4_1d), _Float32_1d)
assert_type(do_neg(c16_1d), _Complex128_1d)
 
assert_type(do_pos(u1_1d), _UInt8_1d)
assert_type(do_pos(i2_1d), _Int16_1d)
assert_type(do_pos(q_1d), _LongLong_1d)
assert_type(do_pos(f4_1d), _Float32_1d)
assert_type(do_pos(c16_1d), _Complex128_1d)