2
2
3
3
from typing import TYPE_CHECKING , Protocol , TypeVar
4
4
5
- from ._types import (
6
- dtype as Dtype ,
7
- device as Device ,
8
- Any ,
9
- PyCapsule ,
10
- Enum ,
11
- ellipsis ,
12
- )
5
+ if TYPE_CHECKING :
6
+ from ._types import (
7
+ device as Device ,
8
+ Any ,
9
+ PyCapsule ,
10
+ Enum ,
11
+ ellipsis ,
12
+ )
13
+ from .data_types import DType
13
14
14
- Self = TypeVar ("Self " , bound = "Array" )
15
- # NOTE: when working with py3.11+ this can be ``typing.Self ``.
15
+ array = TypeVar ("array " , bound = "Array" )
16
+ # NOTE: when working with py3.11+ this can be ``typing.array ``.
16
17
17
18
18
19
class Array (Protocol ):
@@ -21,7 +22,7 @@ def __init__(self) -> None:
21
22
...
22
23
23
24
@property
24
- def dtype (self ) -> Dtype :
25
+ def dtype (self ) -> DType :
25
26
"""
26
27
Data type of the array elements.
27
28
@@ -45,7 +46,7 @@ def device(self) -> Device:
45
46
...
46
47
47
48
@property
48
- def mT (self : Self ) -> Self :
49
+ def mT (self : array ) -> array :
49
50
"""
50
51
Transpose of a matrix (or a stack of matrices).
51
52
@@ -109,7 +110,7 @@ def size(self) -> int | None:
109
110
...
110
111
111
112
@property
112
- def T (self : Self ) -> Self :
113
+ def T (self : array ) -> array :
113
114
"""
114
115
Transpose of the array.
115
116
@@ -126,7 +127,7 @@ def T(self: Self) -> Self:
126
127
"""
127
128
...
128
129
129
- def __abs__ (self : Self , / ) -> Self :
130
+ def __abs__ (self : array , / ) -> array :
130
131
"""
131
132
Calculates the absolute value for each element of an array instance.
132
133
@@ -156,7 +157,7 @@ def __abs__(self: Self, /) -> Self:
156
157
"""
157
158
...
158
159
159
- def __add__ (self : Self , other : int | float | Self , / ) -> Self :
160
+ def __add__ (self : array , other : int | float | array , / ) -> array :
160
161
"""
161
162
Calculates the sum for each element of an array instance with the respective element of the array ``other``.
162
163
@@ -183,7 +184,7 @@ def __add__(self: Self, other: int | float | Self, /) -> Self:
183
184
"""
184
185
...
185
186
186
- def __and__ (self : Self , other : int | bool | Self , / ) -> Self :
187
+ def __and__ (self : array , other : int | bool | array , / ) -> array :
187
188
"""
188
189
Evaluates ``self_i & other_i`` for each element of an array instance with the respective element of the array ``other``.
189
190
@@ -394,7 +395,7 @@ def __dlpack_device__(self, /) -> tuple[Enum, int]:
394
395
# Note that __eq__ returns an array while `object.__eq__` returns a bool.
395
396
# Hence Mypy will complain that this violates the Liskov substitution
396
397
# principle - ignore that.
397
- def __eq__ (self : Self , other : int | float | bool | Self , / ) -> Self : # xtype : ignore
398
+ def __eq__ (self : array , other : int | float | bool | array , / ) -> array : # type : ignore[override]
398
399
r"""
399
400
Computes the truth value of ``self_i == other_i`` for each element of an array instance with the respective element of the array ``other``.
400
401
@@ -448,7 +449,7 @@ def __float__(self, /) -> float:
448
449
"""
449
450
...
450
451
451
- def __floordiv__ (self : Self , other : int | float | Self , / ) -> Self :
452
+ def __floordiv__ (self : array , other : int | float | array , / ) -> array :
452
453
"""
453
454
Evaluates ``self_i // other_i`` for each element of an array instance with the respective element of the array ``other``.
454
455
@@ -473,7 +474,7 @@ def __floordiv__(self: Self, other: int | float | Self, /) -> Self:
473
474
"""
474
475
...
475
476
476
- def __ge__ (self : Self , other : int | float | Self , / ) -> Self :
477
+ def __ge__ (self : array , other : int | float | array , / ) -> array :
477
478
"""
478
479
Computes the truth value of ``self_i >= other_i`` for each element of an array instance with the respective element of the array ``other``.
479
480
@@ -499,10 +500,10 @@ def __ge__(self: Self, other: int | float | Self, /) -> Self:
499
500
...
500
501
501
502
def __getitem__ (
502
- self : Self ,
503
- key : int | slice | ellipsis | tuple [int | slice | ellipsis , ...] | Self ,
503
+ self : array ,
504
+ key : int | slice | ellipsis | tuple [int | slice | ellipsis , ...] | array ,
504
505
/ ,
505
- ) -> Self :
506
+ ) -> array :
506
507
"""
507
508
Returns ``self[key]``.
508
509
@@ -520,7 +521,7 @@ def __getitem__(
520
521
"""
521
522
...
522
523
523
- def __gt__ (self : Self , other : int | float | Self , / ) -> Self :
524
+ def __gt__ (self : array , other : int | float | array , / ) -> array :
524
525
"""
525
526
Computes the truth value of ``self_i > other_i`` for each element of an array instance with the respective element of the array ``other``.
526
527
@@ -605,7 +606,7 @@ def __int__(self, /) -> int:
605
606
"""
606
607
...
607
608
608
- def __invert__ (self : Self , / ) -> Self :
609
+ def __invert__ (self : array , / ) -> array :
609
610
"""
610
611
Evaluates ``~self_i`` for each element of an array instance.
611
612
@@ -625,7 +626,7 @@ def __invert__(self: Self, /) -> Self:
625
626
"""
626
627
...
627
628
628
- def __le__ (self : Self , other : int | float | Self , / ) -> Self :
629
+ def __le__ (self : array , other : int | float | array , / ) -> array :
629
630
"""
630
631
Computes the truth value of ``self_i <= other_i`` for each element of an array instance with the respective element of the array ``other``.
631
632
@@ -650,7 +651,7 @@ def __le__(self: Self, other: int | float | Self, /) -> Self:
650
651
"""
651
652
...
652
653
653
- def __lshift__ (self : Self , other : int | Self , / ) -> Self :
654
+ def __lshift__ (self : array , other : int | array , / ) -> array :
654
655
"""
655
656
Evaluates ``self_i << other_i`` for each element of an array instance with the respective element of the array ``other``.
656
657
@@ -672,7 +673,7 @@ def __lshift__(self: Self, other: int | Self, /) -> Self:
672
673
"""
673
674
...
674
675
675
- def __lt__ (self : Self , other : int | float | Self , / ) -> Self :
676
+ def __lt__ (self : array , other : int | float | array , / ) -> array :
676
677
"""
677
678
Computes the truth value of ``self_i < other_i`` for each element of an array instance with the respective element of the array ``other``.
678
679
@@ -697,7 +698,7 @@ def __lt__(self: Self, other: int | float | Self, /) -> Self:
697
698
"""
698
699
...
699
700
700
- def __matmul__ (self : Self , other : Self , / ) -> Self :
701
+ def __matmul__ (self : array , other : array , / ) -> array :
701
702
"""
702
703
Computes the matrix product.
703
704
@@ -746,7 +747,7 @@ def __matmul__(self: Self, other: Self, /) -> Self:
746
747
"""
747
748
...
748
749
749
- def __mod__ (self : Self , other : int | float | Self , / ) -> Self :
750
+ def __mod__ (self : array , other : int | float | array , / ) -> array :
750
751
"""
751
752
Evaluates ``self_i % other_i`` for each element of an array instance with the respective element of the array ``other``.
752
753
@@ -771,7 +772,7 @@ def __mod__(self: Self, other: int | float | Self, /) -> Self:
771
772
"""
772
773
...
773
774
774
- def __mul__ (self : Self , other : int | float | Self , / ) -> Self :
775
+ def __mul__ (self : array , other : int | float | array , / ) -> array :
775
776
r"""
776
777
Calculates the product for each element of an array instance with the respective element of the array ``other``.
777
778
@@ -802,7 +803,7 @@ def __mul__(self: Self, other: int | float | Self, /) -> Self:
802
803
...
803
804
804
805
# See note above __eq__ method for explanation of the `type: ignore`
805
- def __ne__ (self : Self , other : int | float | bool | Self , / ) -> Self : # type: ignore
806
+ def __ne__ (self : array , other : int | float | bool | array , / ) -> array : # type: ignore[override]
806
807
"""
807
808
Computes the truth value of ``self_i != other_i`` for each element of an array instance with the respective element of the array ``other``.
808
809
@@ -830,7 +831,7 @@ def __ne__(self: Self, other: int | float | bool | Self, /) -> Self: # type: ig
830
831
"""
831
832
...
832
833
833
- def __neg__ (self : Self , / ) -> Self :
834
+ def __neg__ (self : array , / ) -> array :
834
835
"""
835
836
Evaluates ``-self_i`` for each element of an array instance.
836
837
@@ -861,7 +862,7 @@ def __neg__(self: Self, /) -> Self:
861
862
"""
862
863
...
863
864
864
- def __or__ (self : Self , other : int | bool | Self , / ) -> Self :
865
+ def __or__ (self : array , other : int | bool | array , / ) -> array :
865
866
"""
866
867
Evaluates ``self_i | other_i`` for each element of an array instance with the respective element of the array ``other``.
867
868
@@ -883,7 +884,7 @@ def __or__(self: Self, other: int | bool | Self, /) -> Self:
883
884
"""
884
885
...
885
886
886
- def __pos__ (self : Self , / ) -> Self :
887
+ def __pos__ (self : array , / ) -> array :
887
888
"""
888
889
Evaluates ``+self_i`` for each element of an array instance.
889
890
@@ -908,7 +909,7 @@ def __pos__(self: Self, /) -> Self:
908
909
"""
909
910
...
910
911
911
- def __pow__ (self : Self , other : int | float | Self , / ) -> Self :
912
+ def __pow__ (self : array , other : int | float | array , / ) -> array :
912
913
r"""
913
914
Calculates an implementation-dependent approximation of exponentiation by raising each element (the base) of an array instance to the power of ``other_i`` (the exponent), where ``other_i`` is the corresponding element of the array ``other``.
914
915
@@ -940,7 +941,7 @@ def __pow__(self: Self, other: int | float | Self, /) -> Self:
940
941
"""
941
942
...
942
943
943
- def __rshift__ (self : Self , other : int | Self , / ) -> Self :
944
+ def __rshift__ (self : array , other : int | array , / ) -> array :
944
945
"""
945
946
Evaluates ``self_i >> other_i`` for each element of an array instance with the respective element of the array ``other``.
946
947
@@ -963,9 +964,9 @@ def __rshift__(self: Self, other: int | Self, /) -> Self:
963
964
...
964
965
965
966
def __setitem__ (
966
- self : Self ,
967
- key : int | slice | ellipsis | tuple [int | slice | ellipsis , ...] | Self ,
968
- value : int | float | bool | Self ,
967
+ self : array ,
968
+ key : int | slice | ellipsis | tuple [int | slice | ellipsis , ...] | array ,
969
+ value : int | float | bool | array ,
969
970
/ ,
970
971
) -> None :
971
972
"""
@@ -991,7 +992,7 @@ def __setitem__(
991
992
"""
992
993
...
993
994
994
- def __sub__ (self : Self , other : int | float | Self , / ) -> Self :
995
+ def __sub__ (self : array , other : int | float | array , / ) -> array :
995
996
"""
996
997
Calculates the difference for each element of an array instance with the respective element of the array ``other``.
997
998
@@ -1020,7 +1021,7 @@ def __sub__(self: Self, other: int | float | Self, /) -> Self:
1020
1021
"""
1021
1022
...
1022
1023
1023
- def __truediv__ (self : Self , other : int | float | Self , / ) -> Self :
1024
+ def __truediv__ (self : array , other : int | float | array , / ) -> array :
1024
1025
r"""
1025
1026
Evaluates ``self_i / other_i`` for each element of an array instance with the respective element of the array ``other``.
1026
1027
@@ -1052,7 +1053,7 @@ def __truediv__(self: Self, other: int | float | Self, /) -> Self:
1052
1053
"""
1053
1054
...
1054
1055
1055
- def __xor__ (self : Self , other : int | bool | Self , / ) -> Self :
1056
+ def __xor__ (self : array , other : int | bool | array , / ) -> array :
1056
1057
"""
1057
1058
Evaluates ``self_i ^ other_i`` for each element of an array instance with the respective element of the array ``other``.
1058
1059
@@ -1075,8 +1076,8 @@ def __xor__(self: Self, other: int | bool | Self, /) -> Self:
1075
1076
...
1076
1077
1077
1078
def to_device (
1078
- self : Self , device : Device , / , * , stream : int | Any | None = None
1079
- ) -> Self :
1079
+ self : array , device : Device , / , * , stream : int | Any | None = None
1080
+ ) -> array :
1080
1081
"""
1081
1082
Copy the array from the device on which it currently resides to the specified ``device``.
1082
1083
@@ -1101,6 +1102,4 @@ def to_device(
1101
1102
...
1102
1103
1103
1104
1104
- array = Array
1105
-
1106
- __all__ = ["array" ]
1105
+ __all__ = ["Array" ]
0 commit comments