-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: fix the error msg of empty hstack input #8801
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
numpy/core/shape_base.py
Outdated
|
||
if len(arrs) == 0: | ||
raise ValueError('need at least one element to concatenate') | ||
|
||
# As a special case, dimension 0 of 1-dimensional arrays is "horizontal" | ||
if arrs[0].ndim == 1: |
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.
An alternative fix would be to simply change this line to arrs and arrs[0].ndim == 1
, which would then allow concatenate
to produce an appropriate error message
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, that's really helpful!
numpy/core/shape_base.py
Outdated
@@ -282,6 +282,10 @@ def hstack(tup): | |||
|
|||
""" | |||
arrs = [atleast_1d(_m) for _m in tup] | |||
|
|||
if len(arrs) == 0: | |||
raise ValueError('need at least one element to concatenate') |
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 other error message uses array
not element
.
numpy/core/shape_base.py
Outdated
@@ -282,6 +282,10 @@ def hstack(tup): | |||
|
|||
""" | |||
arrs = [atleast_1d(_m) for _m in tup] | |||
|
|||
if len(arrs) == 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.
if not arrs
would be more pythonic IMO
68603f1
to
7523ac5
Compare
@eric-wieser I have updated the implementation and squashed the previous commit. Thanks for the help! |
I'm not actually opposed but this will break backwards-compatibility. Code that checked for Maybe safer to go with some sort of In any case, shouldn't the change be tested? |
@MSeifert04: I agree, changing the exception API is perhaps risky. But a futurewarning wouldn't help because:
From a somewhat mean and not terribly valid point of view, if someone is relying on this behaviour and did not think to report the (relatively obvious) bug here, then perhaps they deserve the breakage? At any rate, Yes, this needs tests. And if they're not there already, |
Also, there's a typo in the commit message |
7523ac5
to
d5bbd85
Compare
@eric-wieser Fix the typo.Sorry about that. |
I would have saved that fixup till you add the tests, but whatever |
d5bbd85
to
9d225b9
Compare
@eric-wieser Hi, I added the test. I haven't done testing addition before, so please let me know if I did something wrong. Thanks! |
Test looks good - but let's add one for |
numpy/core/tests/test_shape_base.py
Outdated
@@ -123,6 +123,9 @@ class TestHstack(TestCase): | |||
def test_non_iterable(self): | |||
assert_raises(TypeError, hstack, 1) | |||
|
|||
def test_empty_input(self): | |||
assert_raises(ValueError, hstack, []) |
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.
Nit-pick: the test for concatenate
uses ()
not []
9d225b9
to
235ce08
Compare
Updated the commit msg and tests. |
Looks good to me. Regarding @MSeifert04's comment, at the very least there should be something in the compatibility section of the 1.13 release notes |
235ce08
to
b1bc302
Compare
Updated the 1.13 release notes. |
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.
Generally looks good to me, but someone else should probably weigh in on @MSeifert04's point
doc/release/1.13.0-notes.rst
Outdated
@@ -29,6 +29,9 @@ Future Changes | |||
Compatibility notes | |||
=================== | |||
|
|||
``numpy.hstack()`` now throws ValueError instead of IndexError when input is empty. | |||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
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.
Bit unusual to have a heading with no content here
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.
I will change it. I didn't realize that is a heading line. I just thought that was a dividing line.
b1bc302
to
1273caf
Compare
Updated the header and content of 1.13 release notes. |
@@ -29,6 +29,12 @@ Future Changes | |||
Compatibility notes | |||
=================== | |||
|
|||
|
|||
Error type changes |
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.
You've reminded me that I should add another entry under this heading regarding #8584, so thanks for that
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.
My pleasure :)
In theory, we probably shouldn't be changing error types, in practice it isn't uncomon and doesn't seem to cause problems. I don't really see how we can avoid it if we refactor python code as things are likely to fail in different places. i suppose we could do all checks up front but we generally rely on ducktyping failing one place or another. The errors are rarely part of the documentation... |
Thanks @ZZhaoTireless . |
Thanks for reviewing and merging my code :D |
Raise the meaningful error msg for np.hstack() under empty input.
Closes #8790