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
API: make arange start
argument positional-only
#25336
Conversation
@@ -3062,7 +3062,7 @@ array_arange(PyObject *NPY_UNUSED(ignored), | |||
NPY_PREPARE_ARGPARSER; | |||
|
|||
if (npy_parse_arguments("arange", args, len_args, kwnames, | |||
"|start", NULL, &o_start, | |||
"", NULL, &o_start, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you make this "|"
you would still allow stop=
on its own, I think? Or is there some other reason to do this, besides arange(start=3)
being silly and it doesn't seem worthwhile to explicitly error out there to allow kwarg use for start?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, it's just consistency and cognitive load.
arange(3)
means arange(stop=3)
, which is arange(start=0, stop=3, step=1)
. What is stop
in arange(start=3)
(not what numpy allows currently, what it should be? I've no idea and no intuition).
Having the first argument "|"
feels strange. If the first argument is optional, it should mean that arange()
is legal. But it is not and should not be IMO.
Overall, I think what's here is the best approximation for arange(start=0, stop, step=1)
which is impossible without a crystal ball.
FWIW, a github search finds many real-world usages of at least the |
I wonder how much of the code from that github search is going to be broken by other NumPy 2.0. changes and cleanups. |
The goal with all the python migrations was either to expire existing deprecations, in which case breaking changes are fine, and otherwise look for existing usages and gauge whether to outright remove something or merely deprecate it in 2.0 based on the level of existing usage inferred by public code searches. There are definitely lots of new deprecations in numpy 2.0 - accessing or importing almost anything from |
This seems dangerous to me. Some downstream projects will need to make fixes when 2.0 comes out, but changing a widely used function may cause more churn than needed. It could maybe be justified if it uncovered unsuspected bugs, but it isn't clear to me that that is the case here. |
Let's just change this to keep allowing |
and
Not sure how to make it work, actually. Using diff --git a/numpy/_core/src/multiarray/multiarraymodule.c b/numpy/_core/src/multiarray/multiarraymodule.c
index d673d95c02..653895c554 100644
--- a/numpy/_core/src/multiarray/multiarraymodule.c
+++ b/numpy/_core/src/multiarray/multiarraymodule.c
@@ -3062,7 +3062,7 @@ array_arange(PyObject *NPY_UNUSED(ignored),
NPY_PREPARE_ARGPARSER;
if (npy_parse_arguments("arange", args, len_args, kwnames,
- "", NULL, &o_start,
+ "|", NULL, &o_start,
"|stop", NULL, &o_stop,
"|step", NULL, &o_step,
"|dtype", &PyArray_DescrConverter2, &typecode, results in
|
🤷, I suspect you can just change the parsing code to allow |
OK, I pushed the needed changes. You could even add |
This seems fine to me. It needs a release note though. If it turns out this breaks code and we get complaints we could consider only deprecating using |
Thank you Sebastian! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No comments, so will just apply thos e suggestions and merge, thanks.
4f85d74
to
8fd0ae5
Compare
I read through the NumPy 2.0 release notes and all makes perfect sense except this change. Leaving backward compatibility aside, the construct The Python built-in If the purpose is to disallow |
No big opinion from me. I think you can't translate to a pretty signature, but the change itself would be pretty simply since this is in C (which actually makes it more convenient here). |
I am confused as to why we did this change that breaks a lot of existing code, especially since we disallowed
Is this the entire motivation for this change, or is there more? Does "rules" refer to some typing problems? It seems a pure-python version could easily mimic whatever we do in C. |
Ahh, this fixes the problem mentioned elsewhere that
I think we should make that change, but allow Edit: that comment seems mistaken. I will continue the discussion on #25743. |
Make
np.arange
first argument positional-only. This does break some backwards compat. This PR removes a test, which also shows typical usages this patch disallows. The weirdest one is probablynp.arange(stop=3)
, where thestart
is inferred to have a zero default. FWIW, the currentarange
sinature,arange([start,] stop[, step,]
, where the first argument has a default value and the second does not, is not very easy to reconstruct with just pure python rules.cross-ref #23999 (comment) and #23999 (comment)