Skip to content

Fix shape mismatch ignored by in-place array operators - #10671

Open
eyupcanakman wants to merge 2 commits into
numba:mainfrom
eyupcanakman:fix/inplace-broadcast-9166
Open

eyupcanakman wants to merge 2 commits into
numba:mainfrom
eyupcanakman:fix/inplace-broadcast-9166

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

An in-place operator on arrays whose shapes don't broadcast was lowered without a shape check, so the loop ran over the output's shape and read past the smaller input. @njit returned a wrong result with no error, while NumPy and Python raise a ValueError.

When the caller supplies the output (an in-place operator or an explicit out=), the broadcast check in _build_array is skipped. Numba now validates each input against the output shape there and raises a ValueError. a + b and other implicit-output ufuncs are unaffected.

Closes #9166

An in-place operator on arrays whose shapes don't broadcast was lowered
without a shape check, so the loop ran over the output's shape and read
past the smaller input. @njit returned a wrong result with no error,
while NumPy and Python raise a ValueError.

When the caller supplies the output (an in-place operator or an explicit
out=), the broadcast check in _build_array is skipped. Numba now
validates each input against the output shape there and raises a
ValueError. a + b and other implicit-output ufuncs are unaffected.

Closes numba#9166
@guilhermeleobas

Copy link
Copy Markdown
Contributor

Port NumPy's test_out_broadcast_errors: a provided output may broadcast the inputs but cannot be smaller than the result, so a too-small out raises instead of silently writing.
@eyupcanakman

Copy link
Copy Markdown
Contributor Author

Done, ported it as test_out_broadcast_errors. numba raises "unable to broadcast" rather than numpy's "non-broadcastable", so I matched the assertion to that.

@guilhermeleobas

Copy link
Copy Markdown
Contributor

There's one test failure now for python 3.10. Can you fix it?

@eyupcanakman

Copy link
Copy Markdown
Contributor Author

That's the docs job (py310_np124_cov_doc), not the added test. sphinx-build fails to import on 3.10 (from types import Union in sphinx/util/typing.py), so make html dies before the tests run, while the other py310 jobs pass with the new test. The docs build installs sphinx unpinned in setup_conda_environment.sh, so a pin there would unblock it. I can open a separate PR for that if it helps.

@guilhermeleobas

Copy link
Copy Markdown
Contributor

oh, ok. It's not related to your PR.

@guilhermeleobas guilhermeleobas added 4 - Waiting on second reviewer Patch needs a second reviewer. and removed 3 - Ready for Review labels Jul 3, 2026
@guilhermeleobas

Copy link
Copy Markdown
Contributor

@kc611 could you review this one?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4 - Waiting on second reviewer Patch needs a second reviewer.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

shape mismatch ignored when using inplace operator

2 participants