Validate an index according to the array API. The array API specification only requires a subset of indices that are supported by NumPy. This function will reject any index that is allowed by NumPy but not required by the array API specification. We always r
(self, key)
| 257 | # Note: A large fraction of allowed indices are disallowed here (see the |
| 258 | # docstring below) |
| 259 | def _validate_index(self, key): |
| 260 | """ |
| 261 | Validate an index according to the array API. |
| 262 | |
| 263 | The array API specification only requires a subset of indices that are |
| 264 | supported by NumPy. This function will reject any index that is |
| 265 | allowed by NumPy but not required by the array API specification. We |
| 266 | always raise ``IndexError`` on such indices (the spec does not require |
| 267 | any specific behavior on them, but this makes the NumPy array API |
| 268 | namespace a minimal implementation of the spec). See |
| 269 | https://data-apis.org/array-api/latest/API_specification/indexing.html |
| 270 | for the full list of required indexing behavior |
| 271 | |
| 272 | This function raises IndexError if the index ``key`` is invalid. It |
| 273 | only raises ``IndexError`` on indices that are not already rejected by |
| 274 | NumPy, as NumPy will already raise the appropriate error on such |
| 275 | indices. ``shape`` may be None, in which case, only cases that are |
| 276 | independent of the array shape are checked. |
| 277 | |
| 278 | The following cases are allowed by NumPy, but not specified by the array |
| 279 | API specification: |
| 280 | |
| 281 | - Indices to not include an implicit ellipsis at the end. That is, |
| 282 | every axis of an array must be explicitly indexed or an ellipsis |
| 283 | included. This behaviour is sometimes referred to as flat indexing. |
| 284 | |
| 285 | - The start and stop of a slice may not be out of bounds. In |
| 286 | particular, for a slice ``i:j:k`` on an axis of size ``n``, only the |
| 287 | following are allowed: |
| 288 | |
| 289 | - ``i`` or ``j`` omitted (``None``). |
| 290 | - ``-n <= i <= max(0, n - 1)``. |
| 291 | - For ``k > 0`` or ``k`` omitted (``None``), ``-n <= j <= n``. |
| 292 | - For ``k < 0``, ``-n - 1 <= j <= max(0, n - 1)``. |
| 293 | |
| 294 | - Boolean array indices are not allowed as part of a larger tuple |
| 295 | index. |
| 296 | |
| 297 | - Integer array indices are not allowed (with the exception of 0-D |
| 298 | arrays, which are treated the same as scalars). |
| 299 | |
| 300 | Additionally, it should be noted that indices that would return a |
| 301 | scalar in NumPy will return a 0-D array. Array scalars are not allowed |
| 302 | in the specification, only 0-D arrays. This is done in the |
| 303 | ``Array._new`` constructor, not this function. |
| 304 | |
| 305 | """ |
| 306 | _key = key if isinstance(key, tuple) else (key,) |
| 307 | for i in _key: |
| 308 | if isinstance(i, bool) or not ( |
| 309 | isinstance(i, SupportsIndex) # i.e. ints |
| 310 | or isinstance(i, slice) |
| 311 | or i == Ellipsis |
| 312 | or i is None |
| 313 | or isinstance(i, Array) |
| 314 | or isinstance(i, np.ndarray) |
| 315 | ): |
| 316 | raise IndexError( |
no test coverage detected