Uh oh!
There was an error while loading. Please reload this page.
Allow Python scalars in array_namespace - #147
Conversation
They are just ignored. This makes array_namespace easier to use for functions that accept either arrays or scalars. I'm not sure if I should have this behavior by default, or if it should be enabled by a flag.
asmeurer
commented
Jun 26, 2024
We can also automatically ignore |
I would say this should be opt-in, given the standard really focusses on just arrays. In practice, it will probably be used more often than not (at least for now), but given that (for now) most projects will wrap In SciPy, it doesn't really matter either way as we coerce scalars to NumPy arrays before passing them to |
asmeurer
commented
Jun 28, 2024
There are some active discussions on allowing scalars in more places in the array API. See data-apis/array-api#805 and data-apis/array-api#807.
I'm trying to make that not the case. Some projects like SciPy will have specific reasons they'll need to wrap things, but ideally whatever most projects need should be implemented already in the wrappers here. |
lucascolley
commented
Jun 29, 2024
I think another form of wrapping that will be very common is using a default backend when all arguments of |
asmeurer
commented
Jul 1, 2024
That would be easy to add. I would caution against it though unless you are doing it for backwards compatibility. If you default to NumPy, for instance, you could end up in a situation where a user gets a NumPy array from one function because they passed in scalars, and another array from another function because they passed in arrays. |
lucascolley
commented
Jul 1, 2024
Yeah, I'm thinking that the reason would be to maintain BC |
From experience adding array api support to
In general, I'd say the logic for unknown arguments (None, possibly scalars) should be "ignore an unknown object and deduce the namespace from all other arguments". Scipy does it in a couple of places, e.g. there's a scipy PR which makes In that view, ignoring scalars is fine as long as |
asmeurer
commented
Nov 16, 2024
That's very useful feedback. It sounds like the behavior in this PR matches what you are suggesting (it does indeed raise if inputs are only scalars or None, as you can see from the tests). |
ev-br
commented
Nov 16, 2024
Tried it locally and it does work as expected. Would be great to be able to use this downstream. So, for what it's worth, LGTM. |
asmeurer
commented
Nov 19, 2024
Looks like this broke some of the tests. We should have merged the latest main before merging here since it was an older PR. |
asmeurer
commented
Nov 19, 2024
This breaks NumPy scalar support because of this annoying fact: >>>isinstance(np.float64(0), float)
True |
They are just ignored. This makes array_namespace easier to use for functions that accept either arrays or scalars.
I'm not sure if I should have this behavior by default, or if it should be enabled by a flag.