8000 DEP,BUG: Coercion/cast of array to a subarray dtype will be fixed by seberg · Pull Request #17596 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DEP,BUG: Coercion/cast of array to a subarray dtype will be fixed #17596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2020

Conversation

seberg
Copy link
Member
@seberg seberg commented Oct 20, 2020

This currently appends the subarray dtype dimensions first and then
tries to assign to the result array which uses incorrect broadcasting
(broadcasting against the subarray dimensions instead of repeating
each element according to the subarray dimensions).

This also fixes the python scalar pathway np.array(2, dtype="(2)f4,")
which previously only filled the first value. I consider that a clear
bug fix.

Closes gh-17511

@seberg seberg force-pushed the subarray-dtype-futurewarning branch from 760be52 to 81b8611 Compare October 20, 2020 21:47
@@ -314,6 +314,23 @@ def test_union_struct(self):
'formats':['i1', 'O'],
'offsets':[np.dtype('intp').itemsize, 0]})

@pytest.mark.parametrize(["obj", "dtype", "expected"],
[(3, "(3)f4,", [3, 3, 3]),
(np.float64(2), "(2)f4,", [2, 2]),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, the first case here previously returned [3, uninitialized, uninitialized], while the second case actually did the right thing. So I considered that a simple bugfix here.

@seberg seberg added this to the 1.20.0 release milestone Oct 20, 2020
@mhvk
Copy link
Contributor
mhvk commented Oct 21, 2020

@seberg - this looks quite good. With this change, will np.array([], '2f4') still raise a FutureWarning? I think it doesn't have to, since the result will not change in the future, but I didn't see a test to check that (and didn't quite follow the code close enough to see if this special case was caught).

This currently appends the subarray dtype dimensions first and then
tries to assign to the result array which uses incorrect broadcasting
(broadcasting against the subarray dimensions instead of repeating
each element according to the subarray dimensions).

This also fixes the python scalar pathway `np.array(2, dtype="(2)f4,")`
which previously only filled the first value.  I consider that a clear
bug fix.

Closes numpygh-17511
@seberg seberg force-pushed the subarray-dtype-futurewarning branch from 22a10b1 to fcc3940 Compare October 21, 2020 17:39
@seberg
Copy link
Member Author
seberg commented Oct 21, 2020

Thanks for taking a look! I am scanning the full input for array(-like) objects in the code, it is a bit annoying, but feel this is fringe enough that it is OK. Since I look for array likes, it will not trigger for empty sequences.

There are cases where a warning will be given even though nothing will change, e.g. np.array(3).astype("(4)f4") has the same future behaviour as with the current broadcast behaviour.

@mhvk
Copy link
Contributor
mhvk commented Oct 21, 2020

OK, that sounds good! And I can see that you cannot do everything... But perhaps good to add a test for the empty list case, just in case?

@seberg
Copy link
Member Author
seberg commented Oct 21, 2020

Oh, seems I accidentally deleted that sentence snippet: I had just added the test.

@seberg seberg closed this Oct 21, 2020
@seberg seberg reopened this Oct 21, 2020
@mattip mattip merged commit 08f9eeb into numpy:master Oct 29, 2020
@mattip
Copy link
Member
mattip commented Oct 29, 2020

Thanks @seberg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Initializing arrays with subarray dtype
3 participants
0