Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Address np.clip PR review feedback
  • Loading branch information
VenkateswarluNagineni committed Sep 23, 2026
commit 8d5ea140c5ceb56a60bd6b6e971c2aed6dde396e
19 changes: 5 additions & 14 deletions docs/upcoming_changes/10767.bug_fix.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
Fix ``np.clip`` shape and layout preservation
----------------------------------------------
Fix ``np.clip`` result shape for broadcast bounds
-------------------------------------------------

Two bugs are fixed:

1. **Wrong shape when bounds broadcast larger than** ``a``: the result was
previously allocated with ``np.empty_like(a)`` before the broadcast shape
was computed, so a bound that broadcasts to a shape larger than ``a``
silently produced the wrong shape and wrote outside the result buffer.

2. **F-contiguous layout not preserved**: switching from ``np.empty_like(a)``
to ``np.empty(shape, a.dtype)`` always produced a C-contiguous result even
when ``a`` was F-contiguous. Both issues are now fixed by dispatching the
allocation at the type level so that F-contiguous inputs produce
F-contiguous outputs.
``np.clip`` now returns the correct result shape when ``a_min`` or ``a_max``
broadcasts to a shape larger than ``a``. The output allocation preserves
NumPy-compatible layout behavior for F-contiguous inputs.
7 changes: 4 additions & 3 deletions numba/tests/test_array_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -2041,9 +2041,10 @@ def test_clip_fortran_order(self):
"one-None-bound clip lost F-contiguous layout")

# array bounds that broadcast to a larger shape (np_clip_impl path)
big = np.zeros((3, 4))
result = cfunc(a, big, 10.0)
expected = np.clip(a, big, 10.0)
narrow = np.asfortranarray(np.arange(6.0).reshape(2, 3, 1))
big = np.asfortranarray(np.zeros((2, 3, 4)))
result = cfunc(narrow, big, 10.0)
expected = np.clip(narrow, big, 10.0)
np.testing.assert_array_equal(result, expected)
self.assertTrue(result.flags.f_contiguous,
"broadcast clip lost F-contiguous layout")
Expand Down