Skip to content

fix: reject broadcast_to targets with fewer dimensions - #1034

Merged
hameerabbasi merged 1 commit into
pydata:mainfrom
Doribelove:fix/broadcast-to-target-rank
Nov 18, 2026
Merged

hameerabbasi merged 1 commit into
pydata:mainfrom
Doribelove:fix/broadcast-to-target-rank

Conversation

@Doribelove

Copy link
Copy Markdown
Contributor

What type of PR is this? (check all applicable)

  • 🐞 Bug Fix
  • 🧪 Test
  • 📚 Documentation

Related issues

The Array API definition of broadcast_to requires the result to have the requested shape.

Checklist

  • Code follows style guide
  • Tests added
  • Documented the changes

Please explain your changes below.

On ba20019, sparse.broadcast_to(sparse.zeros((2, 3)), (3,)).shape returns (2, 3). NumPy raises ValueError for this request. The shape check compares matching trailing dimensions and misses the extra leading axis. Check the target rank before expanding coordinates when the caller specifies an output shape.

The three regression cases cover a leading axis of size two, a leading singleton axis, and a scalar target. All three fail on the original implementation. The existing element-wise tests also exercise valid broadcasting between operands of different ranks.

Validation on the Numba backend:

  • Numba tests and module doctests: 7,455 passed, 1 skipped, 95 xfailed, 59 xpassed.
  • Compared all 1,600 source/target shape pairs of ranks 0–3 with axis sizes 0, 1 and 2 against NumPy, using two fill values. Both the Sparse entry point and NumPy dispatch matched the values and shapes or raised the expected exception.
  • The repository's pre-commit hooks on the changed files and git diff --check passed.

Signed-off-by: 李永祺 <doribelove@gmail.com>
@github-actions github-actions Bot added the fix label Nov 18, 2026
@hameerabbasi
hameerabbasi enabled auto-merge (squash) November 18, 2026 12:01
@hameerabbasi
hameerabbasi merged commit d0da78b into pydata:main Nov 18, 2026
11 of 12 checks passed
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.

2 participants