Skip to content

BUG: Fix misleading ValueError in convolve on empty inputs due to variable swapping#30277

Merged
WarrenWeckesser merged 6 commits intonumpy:mainfrom
Jongwan93:issue-30272
Nov 28, 2025
Merged

BUG: Fix misleading ValueError in convolve on empty inputs due to variable swapping#30277
WarrenWeckesser merged 6 commits intonumpy:mainfrom
Jongwan93:issue-30272

Conversation

@Jongwan93
Copy link
Copy Markdown
Contributor

Reference issue

Fixes #30272

What does this implement/fix?

This PR fixes a misleading error message in numpy.convolve when the first argument is empty and the second is not.

Previously, an internal optimization swapped the arguments (a and v) based on their length before performing the empty check. This caused the ValueError to 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.py to ensure the correct error message is raised.

image

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.
Copy link
Copy Markdown
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

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


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

Choose a reason for hiding this comment

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

Would be nice to not change this.

raise ValueError('a cannot be empty')
if len(v) == 0:
raise ValueError('v cannot be empty')
if (len(v) > len(a)):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (len(v) > len(a)):
if len(v) > len(a):

might as well 🤷

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
@Jongwan93
Copy link
Copy Markdown
Contributor Author

@seberg
Thanks for the review!

I've refactored the test to pass arrays directly to avoid confusion between variable names and argument positions.
I also reverted the unrelated formatting changes.

Let me know if it looks okay now!

@charris
Copy link
Copy Markdown
Member

charris commented Nov 27, 2025

Lint error needs fixing, just needs the file to end with a newline.

@Jongwan93
Copy link
Copy Markdown
Contributor Author

Lint error needs fixing, just needs the file to end with a newline.

Just added missing new line at the end of test_numeric.py file. Thank you

@WarrenWeckesser
Copy link
Copy Markdown
Member

Thanks @Jongwan93. I pushed a small fix to a comment. I'll merge once the latest CI run completes.

@WarrenWeckesser WarrenWeckesser merged commit 3a811fb into numpy:main Nov 28, 2025
74 checks passed
cakedev0 pushed a commit to cakedev0/numpy that referenced this pull request Dec 5, 2025
…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").
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
…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").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: numpy.convolve raises misleading ValueError on empty inputs due to internal variable swapping

5 participants