-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
BUG: np.arange: Allow stop
not start
as sole kwargs.
#17878
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
Conversation
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.
Thanks Maia! I think the logic can be simplified if you check for positional arguments instead of kwarg
. I.e. if args are passed in and there is at least one element, you know that start
was passed by position and not by name (that also avoids the slightly annoying dictionary keyword creation, etc. which just makes me want to micro-optimize it ;)):
If o_stop == NULL
:
- If
args != NULL && PyTuple_GET_SIZE(args) > 0
(not sure args can be NULL...):start
must have been passed by position.o_stop = o_start; o_start = NULL
- Else:
raise TypeError(...)
Py_DECREF(start_str); | ||
Py_DECREF(stop_str); | ||
return NULL; | ||
} else if (PyDict_Contains(kws, start_str) == 0 && |
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.
Nitpick: We usually put the else
on a new lin from the closing bracket }
.
|
||
class TestArange: | ||
def test_require_range(self): | ||
import pytest |
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.
The pytest import should be top-level, if it is not already.
(I am a bit surprised by the codecov comments, since those lines seem covered by these tests...)
|
||
assert len(keyword_stop) == 3 | ||
assert len(keyword_zerotostop) == 3 | ||
assert (keyword_stop == keyword_zerotostop).all() |
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.
Just FYI, we also have a assert_arrays_equal
, which checks also shape/dtype (difference that all()
might miss). It doesn't matter here though.
I am not sure about the micro-optimization. I think the function is most likely to be called without kwargs, so |
Good point, but I still think it will make for much simpler/shorter code. I guess you could keep the |
Ah, looking at it again. The C-level function handles
|
stop
not start
as sole kwargs. (gh-17764)stop
not start
as sole kwargs.
Thanks for the comments and the suggestion! please also delight with me in my closing/reopening the ticket -- proper newcomer :) I have a few questions:
re:
|
About point 1. I am not sure the called function supports About 2. my working assumption was you meant to allow About the test location. It is fine by me, maybe someone has a better idea. I guess there isn't a perfect place :(. |
9398cfb
to
8a70a4f
Compare
…mpygh-17764) * This matches args behaviour. * Avoids `arange(start=4)` from returning a range from 0 to 4 * Allows `arange(stop=4)` to run without error, to return range from 0 to 4
8a70a4f
to
b864085
Compare
Thanks Maia! If you are still wondering about the codecov, I expect it is just due to compiler optimizing pretty much everything away. |
if (args == NULL || PyTuple_GET_SIZE(args) == 0){ | ||
PyErr_SetString(PyExc_TypeError, | ||
"arange() requires stop to be specified."); | ||
return NULL; |
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.
Sorry, I a bit of a sloppy day for me... I just noticed that this branch is missing the Py_XDECREF(typecode);
One reason why I am sloppy about these things though, is that nowadays reference count checkers catch these bugs pretty well most of the time (Unrelated note for anyone who wonders: No I have not run pytest-leaks
on master/1.20 after branching, only valgrind which is typically not sensitive enough for this type of leak usually.)
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.
Maybe a short note in the docs about how to check this would be good. Is there a way to run a checker only on a single test like python <some command> test_start_stop_kwarg
?
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.
Follow up #17966
Fixes gh-17764
start
argument in argparse format stringarange(start=4)
returning a range from 0 to 4arange(stop=4)
to run without error, to return range from 0 to 4