BUG: zero-length tensor contractions ignore requested output types

Open
#1,005 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@nawaaaaaAaar is already working on this.

Since Oct 22, 2026.

  • #1010 by @nawaaaaaAaar — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
numpy, python
Domain
data

Research direction

Start by reading the zero-contraction fast path in sparse.tensordot and the existing tests for tensor contractions. Add regression coverage for COO, GCXS and mixed dense inputs across the supported return_type values; the issue says a 32-case matrix reproduces the behavior. Done means output type, values, shape and dtype match the requested type and NumPy expectations.

Written by the indexing model from the issue text.

Description

Bug

The zero-contraction fast path in sparse.tensordot ignores return_type and does not preserve the normal default GCXS output selection.

Reproduced on main at d63255057744331b96626a1f1c24504b3a3783cd.

import numpy as np
import sparse

a = sparse.COO.from_numpy(np.empty((2, 0), dtype=np.float32))
b = sparse.COO.from_numpy(np.empty((0, 3), dtype=np.float32))
result = sparse.tensordot(a, b, axes=1, return_type=np.ndarray)
assert isinstance(result, np.ndarray)  # fails: result is COO

Conversely, mixing a dense operand with a sparse one returns an ndarray even when return_type=COO or GCXS is explicitly requested. Two GCXS operands also return COO by default on this fast path, unlike non-empty contractions.

A fix and a 32-case regression matrix are ready. The matrix covers COO, GCXS and mixed dense inputs and all supported explicit output types; 19 cases fail before the production change. Numerical values, output shapes and dtype are compared with NumPy.

This is an AI-assisted bug investigation with locally executed reproductions and tests. No assignment is requested.

Dominant language
Python
Stars
670
Forks
167
Avg merge
3d 4h
Merged PRs (30d)
7

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydata/sparse

All issues in pydata/sparse

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.