I had a rough idea that things like this could belong in a new array_api_extra.numpy namespace, which would be enabled by an optional extra with a numpy dependency.
Some other things that could potentially live in such a namespace:
_as_numpy_array:
|
def _as_numpy_array( # numpydoc ignore=PR01,RT01 |
- a version of
array_namespace which falls back to np.asarray for non-array input: https://github.com/scipy/scipy/blob/94070c2f3540a1a949876ca61c5f877212874e90/scipy/_lib/_array_api_override.py#L73
is_python_scalar, a boolean check which excludes NumPy scalars
|
def is_python_scalar(x: object) -> TypeIs[complex]: # numpydoc ignore=PR01,RT01 |
|
"""Return True if `x` is a Python scalar, False otherwise.""" |
|
# isinstance(x, float) returns True for np.float64 |
|
# isinstance(x, complex) returns True for np.complex128 |
|
# bool is a subclass of int |
|
return isinstance(x, int | float | complex) and not is_numpy_array(x) |
isscalar, a boolean check for NumPy scalars OR 0-D arrays https://github.com/scipy/scipy/blob/94070c2f3540a1a949876ca61c5f877212874e90/scipy/_lib/_array_api.py#L1184-L1185
- a version of
asarray with extra params like order, check_finite, subok, which are only effective for NumPy https://github.com/scipy/scipy/blob/94070c2f3540a1a949876ca61c5f877212874e90/scipy/_lib/_array_api.py#L76-L120
- sklearn has a util for retrieving the matching NumPy dtype for an array: https://github.com/scikit-learn/scikit-learn/blob/9888db252fbc4c526d9ba9632caacf958ba4753c/sklearn/utils/_array_api.py#L1433-L1441
This would be a namespace of things that are useless for new libraries which can adopt the standard from the get-go, but that are helpful for existing libraries written against NumPy that need to transition.
If people agree, then we could think about this some more, but it should definitely be discussed in one of the public array API community meetings if so.
Originally posted by @lucascolley in #1021
Originally posted by @lucascolley in #1021