25
25
_array_docstrings = tomllib .load (f )["docstrings" ]
26
26
27
27
NS_co = TypeVar ("NS_co" , covariant = True , default = ModuleType )
28
- T_contra = TypeVar ("T_contra " , contravariant = True )
28
+ Other_contra = TypeVar ("Other_contra " , contravariant = True )
29
29
R_co = TypeVar ("R_co" , covariant = True , default = Never )
30
+ DType_co = TypeVar ("DType_co" , covariant = True )
30
31
31
32
32
33
class HasArrayNamespace (Protocol [NS_co ]):
@@ -52,27 +53,60 @@ def __array_namespace__(
52
53
) -> NS_co : ...
53
54
54
55
56
+ class HasDType (Protocol [DType_co ]):
57
+ """Protocol for array classes that have a data type attribute."""
58
+
59
+ @property
60
+ def dtype (self ) -> DType_co :
61
+ """Data type of the array elements."""
62
+ ...
63
+
64
+
55
65
@docstring_setter (** _array_docstrings )
56
66
class Array (
67
+ # ------ Attributes -------
68
+ HasDType [DType_co ],
69
+ # ------ Methods -------
57
70
HasArrayNamespace [NS_co ],
58
71
op .CanPosSelf ,
59
72
op .CanNegSelf ,
60
- op .CanAddSame [T_contra , R_co ],
61
- op .CanSubSame [T_contra , R_co ],
62
- op .CanMulSame [T_contra , R_co ],
63
- op .CanTruedivSame [T_contra , R_co ],
64
- op .CanFloordivSame [T_contra , R_co ],
65
- op .CanModSame [T_contra , R_co ],
66
- op .CanPowSame [T_contra , R_co ],
67
- Protocol [T_contra , R_co , NS_co ],
73
+ op .CanAddSame [Other_contra , R_co ],
74
+ op .CanSubSame [Other_contra , R_co ],
75
+ op .CanMulSame [Other_contra , R_co ],
76
+ op .CanTruedivSame [Other_contra , R_co ],
77
+ op .CanFloordivSame [Other_contra , R_co ],
78
+ op .CanModSame [Other_contra , R_co ],
79
+ op .CanPowSame [Other_contra , R_co ],
80
+ Protocol [DType_co , Other_contra , R_co , NS_co ],
68
81
):
69
- """Array API specification for array object attributes and methods."""
82
+ """Array API specification for array object attributes and methods.
83
+
84
+ The type is: ``Array[+DType, -Other = Never, +R = Never, +NS = ModuleType] =
85
+ Array[+DType, Self | Other, Self | R, NS]`` where:
86
+
87
+ - `DType` is the data type of the array elements.
88
+ - `Other` is the type of objects that can be used with the array (e.g., for
89
+ binary operations). For example, with numeric arrays, it is common to be
90
+ able to add `float` and `int` objects to the array, not just other arrays
91
+ of the same dtype. This would be annotated as `Other = float`. When not
92
+ specified, `Other` only allows `Self` objects.
93
+ - `R` is the return type of the array operations. For example, the return
94
+ type of the division between integer arrays can be a float array. This
95
+ would be annotated as `R = float`. When not specified, `R` only allows
96
+ `Self` objects.
97
+ - `NS` is the type of the array namespace. It defaults to `ModuleType`,
98
+ which is the most common form of array namespace (e.g., `numpy`, `cupy`,
99
+ etc.). However, it can be any type, e.g. a `types.SimpleNamespace`, to
100
+ allow for wrapper libraries to semi-dynamically define their own array
101
+ namespaces based on the wrapped array type.
102
+
103
+ """
70
104
71
105
72
106
BoolArray : TypeAlias = Array [bool , Array [float , Never , NS_co ], NS_co ]
73
107
"""Array API specification for boolean array object attributes and methods.
74
108
75
- Specifically, this type alias fills the `T_contra ` type variable with
109
+ Specifically, this type alias fills the `Other_contra ` type variable with
76
110
`bool`, allowing for `bool` objects to be added, subtracted, multiplied, etc. to
77
111
the array object.
78
112
@@ -81,7 +115,7 @@ class Array(
81
115
NumericArray : TypeAlias = Array [float | int , NS_co ]
82
116
"""Array API specification for numeric array object attributes and methods.
83
117
84
- Specifically, this type alias fills the `T_contra ` type variable with `float
118
+ Specifically, this type alias fills the `Other_contra ` type variable with `float
85
119
| int`, allowing for `float | int` objects to be added, subtracted, multiplied,
86
120
etc. to the array object.
87
121
0 commit comments