-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
BUG: NEP 50 inconsistency in copyto
#26381
Comments
Agreed with this - they should behave the same as much as possible, so I think we should go for 2. But your question about |
+1 for If this means that short ints may raise, OK, this is self-consistent. An alternative is to basically set
|
Right, but two question:
|
IMO, copyto(python scalar) can indeed wait until numpy 2.1 or something. It is annoying yes, but can be worked around. Why does
What looks like a bug to me is value-based logic in 0D arrays. As discussed offline:
OTOH, if it's more convenient to fix this together with python scalars, downstream can adapt. |
Argg. Partially, because We allow: Now, the closest to just saying "all fine", to me seems like saying: OK, if casting is passed explicitly, we just use the default dtype (even for uint that isn't same-kind to Python int). But if |
TBH and FWIW, what you write sounds too tricky. I've no idea how to predict what's going on or how to explain what's going on (the "how to teach this" PEP rubric). For
|
Well, it's what we have: I have struggled with this and brought up the fact that it is tricky many times and over a very long time...
So you have no choice but to do the promotion by default by basically ignoring the value and saying a Python integer could be any integer really. And that works fine, except that there is a line when code calls But back to the problem: This is mostly fine for promotion, but if you look at casting things get tricky because while promotion used to be implemented via Unless you are suggesting to not do any NEP 50 style weak promotion at all, which of course is the only truly consistent thing without pitfalls, but... |
FWIW, my understanding is that in the last meeting it seemed like there was a preference for just considering these casts safe (in particular |
As @ev-br noticed: Copyto is currently inconsistent in NEP 50, which will be noticed relatively rarely, because the default is
same-kind
casting, where it just doesn't matter.It turns out,
copyto
is the only (meaningful) user ofPyArray_AssignRawScalar
when the input is 0-D, and that function is the only direct user of the old scalar logic.Now,
copyto
runs into the same difficulty as ufuncs (even more niche), about how you define "safe" casting for Python int/float/complex. In principle, we could:src
is an array, and ignore any subtleties (right now it uses old logic, which also kicks in for 0-d arrays).np.copyto(uint8_arr, 1000)
wraps the integer (which is what happens right now anyway).I don't care too much which way we go here, but I think we should punt this to NumPy 2.1 and then need to make a decision though. We were bound to have such issues, that it is actually using old logic is annoying, but not much worse than similar ones we must still have in Python.
The advantage of defining things as 2. is that it can make
copyto
behave like a ufunc:np.positive(src, out=dst, casting=...)
(positive is just effectively an identity ufunc).That part has the subtlety about mixing
casting=
with Python scalars (but not the issue about 0-D arrays).I would be much obliged for input, there is a reason this is still there: Deciding what to do with scalars and cast-safety is not trivial and probably more of an "expectation" decision than trying to reason what is "right".
The text was updated successfully, but these errors were encountered: