BUG: Fix misleading ValueError in convolve on empty inputs due to variable swapping#30277
BUG: Fix misleading ValueError in convolve on empty inputs due to variable swapping#30277WarrenWeckesser merged 6 commits intonumpy:mainfrom
Conversation
Previously, numpy.convolve swapped arguments for optimization if the second argument was longer than the first. This occurred before checking for empty arrays. Consequently, if the first argument was empty, the swap would cause the error message to incorrectly report that the second argument was empty (e.g., "v cannot be empty"). This commit moves the validation of empty inputs to occur before the optimization swap. This ensures that the ValueError correctly identifies which argument is actually empty.
This adds a regression test to ensure `numpy.convolve` raises the correct ValueError when the first argument is empty. Previously, internal variable swapping caused the error message to incorrectly attribute the empty input to the wrong argument.
seberg
left a comment
There was a problem hiding this comment.
I like that the fix is to just move things, but the test is failing, mixing up the parameter names.
(You wrote the test as if the variable name matters, but the argument name matters and we are passing things positionally.)
numpy/_core/tests/test_numeric.py
Outdated
|
|
||
| with pytest.raises(TypeError, match="Input should be a NumPy array"): | ||
| np.astype(data, np.float64) | ||
| np.astype(data, np.float64) No newline at end of file |
There was a problem hiding this comment.
Would be nice to not change this.
numpy/_core/numeric.py
Outdated
| raise ValueError('a cannot be empty') | ||
| if len(v) == 0: | ||
| raise ValueError('v cannot be empty') | ||
| if (len(v) > len(a)): |
There was a problem hiding this comment.
| if (len(v) > len(a)): | |
| if len(v) > len(a): |
might as well 🤷
There was a problem hiding this comment.
Thank you for pointing this out. made the change in next commit
Refactored the regression test for `convolve` to directly use `np.array` instead of named variables, avoiding confusion about parameter names vs argument names. Also removed unnecessary brakets in numeric.py
|
@seberg I've refactored the test to pass arrays directly to avoid confusion between variable names and argument positions. Let me know if it looks okay now! |
|
Lint error needs fixing, just needs the file to end with a newline. |
Just added missing new line at the end of |
|
Thanks @Jongwan93. I pushed a small fix to a comment. I'll merge once the latest CI run completes. |
…iable swapping (numpy#30277) * BUG: Fix misleading ValueError in convolve on empty inputs Previously, numpy.convolve swapped arguments for optimization if the second argument was longer than the first. This occurred before checking for empty arrays. Consequently, if the first argument was empty, the swap would cause the error message to incorrectly report that the second argument was empty (e.g., "v cannot be empty").
…iable swapping (numpy#30277) * BUG: Fix misleading ValueError in convolve on empty inputs Previously, numpy.convolve swapped arguments for optimization if the second argument was longer than the first. This occurred before checking for empty arrays. Consequently, if the first argument was empty, the swap would cause the error message to incorrectly report that the second argument was empty (e.g., "v cannot be empty").
Reference issue
Fixes #30272
What does this implement/fix?
This PR fixes a misleading error message in
numpy.convolvewhen the first argument is empty and the second is not.Previously, an internal optimization swapped the arguments (
aandv) based on their length before performing the empty check. This caused theValueErrorto incorrectly report that the second argument was empty when it was actually the first argument that was empty.This change moves the validation logic before the optimization swap.
Additional information
I have added a regression test case in
numpy/_core/tests/test_numeric.pyto ensure the correct error message is raised.