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
30
31
31
@@ -57,22 +57,42 @@ class Array(
57
57
HasArrayNamespace [NS_co ],
58
58
op .CanPosSelf ,
59
59
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 ],
60
+ op .CanAddSame [Other_contra , R_co ],
61
+ op .CanSubSame [Other_contra , R_co ],
62
+ op .CanMulSame [Other_contra , R_co ],
63
+ op .CanTruedivSame [Other_contra , R_co ],
64
+ op .CanFloordivSame [Other_contra , R_co ],
65
+ op .CanModSame [Other_contra , R_co ],
66
+ op .CanPowSame [Other_contra , R_co ],
67
+ Protocol [Other_contra , R_co , NS_co ],
68
68
):
69
- """Array API specification for array object attributes and methods."""
69
+ """Array API specification for array object attributes and methods.
70
+
71
+ The type is: ``Array[-Other = Never, +R = Never, +NS = ModuleType] =
72
+ Array[Self | Other, Self | R, NS]`` where:
73
+
74
+ - `Other` is the type of objects that can be used with the array (e.g., for
75
+ binary operations). For example, with numeric arrays, it is common to be
76
+ able to add `float` and `int` objects to the array, not just other arrays
77
+ of the same dtype. This would be annotated as `Other = float`. When not
78
+ specified, `Other` only allows `Self` objects.
79
+ - `R` is the return type of the array operations. For example, the return
80
+ type of the division between integer arrays can be a float array. This
81
+ would be annotated as `R = float`. When not specified, `R` only allows
82
+ `Self` objects.
83
+ - `NS` is the type of the array namespace. It defaults to `ModuleType`,
84
+ which is the most common form of array namespace (e.g., `numpy`, `cupy`,
85
+ etc.). However, it can be any type, e.g. a `types.SimpleNamespace`, to
86
+ allow for wrapper libraries to semi-dynamically define their own array
87
+ namespaces based on the wrapped array type.
88
+
89
+ """
70
90
71
91
72
92
BoolArray : TypeAlias = Array [bool , Array [float , Never , NS_co ], NS_co ]
73
93
"""Array API specification for boolean array object attributes and methods.
74
94
75
- Specifically, this type alias fills the `T_contra ` type variable with
95
+ Specifically, this type alias fills the `Other_contra ` type variable with
76
96
`bool`, allowing for `bool` objects to be added, subtracted, multiplied, etc. to
77
97
the array object.
78
98
@@ -81,7 +101,7 @@ class Array(
81
101
NumericArray : TypeAlias = Array [float | int , NS_co ]
82
102
"""Array API specification for numeric array object attributes and methods.
83
103
84
- Specifically, this type alias fills the `T_contra ` type variable with `float
104
+ Specifically, this type alias fills the `Other_contra ` type variable with `float
85
105
| int`, allowing for `float | int` objects to be added, subtracted, multiplied,
86
106
etc. to the array object.
87
107
0 commit comments