-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ak.from_numpy should fail on zero-dimensional arrays. (#3161)
* Fixing policy issue 1057 At this point it's a matter of consistency. Now *both* `ak.Array()` and `ak.from_numpy()` will throw a TypeError when passed either a python scalar, a numpy numeric value like `np.float64(5.2)`, **or** a numpy zero-dimensional-array like `np.array(5.2)`. But in the latter case there is an option to allow this, which is necessary for lots of internal functions, by providing a value for `primitive_policy` other than "error." Prior to this patch the first test, ak.Array(), was already passing without modifications. But the second test was not -- ak.from_numpy(). Fix is in _layout.py. No policy options are passed to _layout.from_arraylib(). * Adding/passing a primitive_policy kwarg From from_numpy, from_cupy, from_jax, and from_dlpack To from_arraylib --------- Co-authored-by: Ianna Osborne <[email protected]>
- Loading branch information
1 parent
03f6169
commit db6cece
Showing
7 changed files
with
74 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/test_1057_akarray_and_akfrom_numpy_should_not_accept_zero_dimensional_arrays.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE | ||
|
||
from __future__ import annotations | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import awkward as ak | ||
|
||
|
||
def test_akarray_from_zero_dim_nparray(): | ||
np_scalar = np.array(2.7) # A kind of scalar in numpy. | ||
assert np_scalar.ndim == 0 and np_scalar.shape == () | ||
with pytest.raises(TypeError): | ||
# Conversion to ak.Array ought to throw here: | ||
b = ak.Array(np_scalar) # (bugged) value: <Array [2.7] type='1 * int64'> | ||
# Now we're failing. Here's why. | ||
c = ak.to_numpy(b) # value: array([2.7]) | ||
assert np_scalar.shape == c.shape # this fails | ||
|
||
with pytest.raises(TypeError): | ||
b = ak.from_numpy(np_scalar) | ||
c = ak.to_numpy(b) | ||
assert np_scalar.shape == c.shape |