feat: add support for scalars in result_type#873
Conversation
|
As this PR has received one approval and follows changes proposed and discussed during workgroup meetings, I'll go ahead and merge. Any further refinements can be made in follow-up PRs. Thanks for reviewing, @lucascolley! |
|
Looking at the underlying issue, it doesn't seem to have come up, but I thought I'd check before opening a new one - was there an explicit decision to allow instances of Python scalars but NOT the types themselves (i.e., |
|
@mdhaber The spec does allow dtypes. See https://data-apis.org/array-api/latest/API_specification/generated/array_api.result_type.html. Maybe I am misunderstanding your question. |
|
A strict reading of the standard suggests that this will fail: import array_api_strict as xp
xp.result_type(xp.asarray(1), float)and it does.
|
|
@mdhaber I added to the upcoming workgroup agenda. |
|
A bit of due diligence for the support across array libraries: |
|
I commented on the NumPy issue: I think @mdhaber makes a good point there. For the standard however, if NumPy is inconsistent with itself and might be changing behavior (or not), and if PyTorch doesn't support passing in the |
|
I might be misunderstanding the use-case here, but you can easily go from a Python scalar type to an instance by calling it with no arguments >>> int()
0
>>> float()
0.0
>>> bool()
False
>>> complex()
0j |
|
Yes, it's easy to work around if you know to expect it. It just seemed accidental to accept arrays, scalars, and array dtypes but not scalar types. And it's surprising that the standard implicitly permits the behavior for scalars and scalar types to be different; this gotcha could easily lead to dtype bugs. |
This PR:
result_type#805 by adding scalar support toresult_type.result_type#805 (comment)).