Skip to content
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

MAINT: Simplify element setting and use it for filling #20924

Merged
merged 3 commits into from Jun 13, 2022

Conversation

seberg
Copy link
Member

@seberg seberg commented Jan 28, 2022

This slightly modifies the behaviour of arr.fill() to be
arr.fill(scalar), i.e. match arr1d[0] = scalar, rather than
arr.fill(np.asarray(scalar)), which subtely different!
(Note that object was already special cased to have the scalar
logic.)

Otherwise, PyArray_Pack is now the actual, full featured, "scalar"
assignment logic. It is a bit strange due to that quantity/masked
array issue, but there is nothing to be done about it.
The simplifications in PyArray_AssignFromCache should not cause
any change in practice, because non 0-D arrays would have been
rejected earlier on in that path.
(Basically, it does not need the full PyArray_Pack logic, but that
is fine, I intially split the two, but consolidated them again.)

@seberg
Copy link
Member Author

seberg commented Jan 28, 2022

This is also fairly important, since it finally full consolidates what it means to set an item (at least to the extend possible). We also really should add PyArray_Pack to the public API in order to nudge people away from PyArray_SETITEM (although, effectively, I think we can just call it from the setitem function for the user...).
(There is a bit of a problem with HPy probably wanting an owner in case the descriptor has references, we could just add an owner to the signature, but it feels a bit weird if we don't even know how to use it correctly everywhere yet.)

This means that a followup will be to change all PyArray_SETITEM calls, though.

@seberg seberg added the triage review Issue/PR to be discussed at the next triage meeting label Feb 3, 2022
@mattip
Copy link
Member

mattip commented Feb 14, 2022

Didn't we discuss this at the triage meeting and decide to go ahead with it?

@seberg
Copy link
Member Author

seberg commented Feb 14, 2022

Yeah, although there is that weird thing about whether or not to unpack np.empty((), dtype=object) (0-D object array). This handles the unpacking "correctly" now.

I think we should just do this and go ahead, but I do need to ping the list and we may want to follow-up on the precise route.

@mattip
Copy link
Member

mattip commented Feb 20, 2022

Did the answers on the list help?

@seberg
Copy link
Member Author

seberg commented Feb 21, 2022

Not quite settled, considering that Stéfan and you seem to have different opinions :). I would go with this (i.e. store the array directly), for now. Not because I am 100% sure that this is the best way, but because my guess is that it is so much more common for us to do that (item assignment), that it probably barely matters to go ahead and change fill even if we want to flip it all again.
And I still think it is more important to try to have a "one right way" when it comes to item assignment.

@InessaPawson InessaPawson added triaged Issue/PR that was discussed in a triage meeting and removed triage review Issue/PR to be discussed at the next triage meeting labels Apr 20, 2022
@seberg
Copy link
Member Author

seberg commented Jun 2, 2022

I would like to go for this now that we are early in the next release cycle. Yes, there is some uncertainty about object arrays, but I prefer the consistency for now, and worry about flipping the switch later (rather than caring about keeping this one corner case on potential future behavior).

@mattip
Copy link
Member

mattip commented Jun 12, 2022

Here is the mailing list discussion. Can we add some documentation to np.fill to point out how using an object array will return a different result than when using assignment?

seberg and others added 2 commits June 12, 2022 13:56
This slightly modifies the behaviour of `arr.fill()` to be
`arr.fill(scalar)`, i.e. match `arr1d[0] = scalar`, rather than
`arr.fill(np.asarray(scalar))`, which subtely different!
(Note that object was already special cased to have the scalar
logic.)

Otherwise, `PyArray_Pack` is now the actual, full featured, "scalar"
assignment logic.  It is a bit strange due to that quantity/masked
array issue, but there is nothing to be done about it.
The simplifications in `PyArray_AssignFromCache` should not cause
any change in practice, because non 0-D arrays would have been
rejected earlier on in that path.
(Basically, it does not need the full `PyArray_Pack` logic, but that
is fine, I intially split the two, but consolidated them again.)
Co-authored-by: Matti Picus <matti.picus@gmail.com>
>>> a[...] = np.array(3)
>>> a
array([3, 3], dtype=object)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm, added this chunk as an example (and tweaked the release not a tiny bit). Not sure whether it isn't a bit much, but I guess it is just at the end of the examples.

Copy link
Member

Choose a reason for hiding this comment

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

+1, thanks

@mattip mattip merged commit d4baf36 into numpy:main Jun 13, 2022
@mattip
Copy link
Member

mattip commented Jun 13, 2022

Thanks @seberg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
03 - Maintenance 58 - Ready for review triaged Issue/PR that was discussed in a triage meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants