Skip to content

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

Merged
merged 1 commit into from
Mar 25, 2017

Conversation

zixu-zhao
Copy link

Raise the meaningful error msg for np.hstack() under empty input.

Closes #8790


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:
Copy link
Member

@eric-wieser eric-wieser Mar 20, 2017

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

Copy link
Author

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!

@@ -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')
Copy link
Member

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.

@@ -282,6 +282,10 @@ def hstack(tup):

"""
arrs = [atleast_1d(_m) for _m in tup]

if len(arrs) == 0:
Copy link
Member

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

@zixu-zhao zixu-zhao force-pushed the fix/hstack-error-msg branch from 68603f1 to 7523ac5 Compare March 20, 2017 21:51
@zixu-zhao
Copy link
Author

zixu-zhao commented Mar 20, 2017

@eric-wieser I have updated the implementation and squashed the previous commit. Thanks for the help!

@MSeifert04
Copy link
Contributor

MSeifert04 commented Mar 20, 2017

I'm not actually opposed but this will break backwards-compatibility. Code that checked for IndexError before needs to check for ValueError now. Not sure if anyone ever used this in a try: ... except IndexError: ... but these will get a nasty surprise.

Maybe safer to go with some sort of FutureWarning first or is it too unlikely?

In any case, shouldn't the change be tested?

@eric-wieser
Copy link
Member

eric-wieser commented Mar 20, 2017

@MSeifert04: I agree, changing the exception API is perhaps risky. But a futurewarning wouldn't help because:

  • People couldn't change their code to catch ValueError until the release where things are changed in numpy, so there's no way to prepare
    • To fix this, we could raise class FutureValueButCurrentlyIndexError(IndexError, ValueError): pass
  • Even then, we provide no way for the user to squash the warning, so they have no incentive to try and fix it

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, hstack can throw ValueError already for a different set of reasons, so the type of code with a try catch is likely to catch both


Yes, this needs tests. And if they're not there already, vstack and concatenate should test for this too.

@eric-wieser
Copy link
Member

Also, there's a typo in the commit message

@zixu-zhao zixu-zhao force-pushed the fix/hstack-error-msg branch from 7523ac5 to d5bbd85 Compare March 20, 2017 22:07
@zixu-zhao
Copy link
Author

@eric-wieser Fix the typo.Sorry about that.

@eric-wieser
Copy link
Member

I would have saved that fixup till you add the tests, but whatever

@zixu-zhao zixu-zhao changed the title BUG: fix the error msg of empty hastack input BUG: fix the error msg of empty hstack input Mar 20, 2017
@zixu-zhao zixu-zhao force-pushed the fix/hstack-error-msg branch from d5bbd85 to 9d225b9 Compare March 20, 2017 23:03
@zixu-zhao
Copy link
Author

@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!

@eric-wieser
Copy link
Member

eric-wieser commented Mar 20, 2017

Test looks good - but let's add one for vstack as well, since that also doesn't test this. No need to mention the test in the commit message - it's usually implied.

@@ -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, [])
Copy link
Member

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 []

@zixu-zhao zixu-zhao force-pushed the fix/hstack-error-msg branch from 9d225b9 to 235ce08 Compare March 20, 2017 23:51
@zixu-zhao
Copy link
Author

Updated the commit msg and tests.

@eric-wieser
Copy link
Member

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

@zixu-zhao zixu-zhao force-pushed the fix/hstack-error-msg branch from 235ce08 to b1bc302 Compare March 21, 2017 00:12
@zixu-zhao
Copy link
Author

Updated the 1.13 release notes.

Copy link
Member

@eric-wieser eric-wieser left a 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

@@ -29,6 +29,9 @@ Future Changes
Compatibility notes
===================

``numpy.hstack()`` now throws ValueError instead of IndexError when input is empty.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy link
Member

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

Copy link
Author

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.

@zixu-zhao zixu-zhao force-pushed the fix/hstack-error-msg branch from b1bc302 to 1273caf Compare March 21, 2017 22:13
@zixu-zhao
Copy link
Author

Updated the header and content of 1.13 release notes.

@@ -29,6 +29,12 @@ Future Changes
Compatibility notes
===================


Error type changes
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

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

My pleasure :)

@charris
Copy link
Member

charris commented Mar 25, 2017

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...

@charris
Copy link
Member

charris commented Mar 25, 2017

Thanks @ZZhaoTireless .

@zixu-zhao
Copy link
Author

Thanks for reviewing and merging my code :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants