rules/scipy/empty-csr-write.md

Writing into a CSR or CSC matrix that started empty warns, raises under python -W error, and copies the whole matrix each time. Build it as lil_array, dok_array or from coordinates, then convert once.

diff
1-A = sparse.csr_matrix((n, n))
2+A = sparse.lil_array((n, n))
3 for i, j, v in triples:
4 A[i, j] = v
5+A = A.tocsr()

What the rule reports

The rule reports a write into a csr_matrix, csc_matrix, csr_array or csc_array built from a shape alone, as in A = csr_matrix((n, m)) followed by A[i, j] = v, A[i, j] += v or a slice write, in a function that doesn't assign A again. Each write adds a stored value and copies the whole matrix. On SciPy 1.18.1 a write emits SparseEfficiencyWarning, which raises under python -W error. Moreover, 1,000 writes ran 30 times slower than the same writes into a lil_matrix.

The finding points to the constructor. A shape supplied as X.shape can match, while constructors given stored values and index arrays are left alone.

How to fix it

xpd check --fix --unsafe-fixes rewrites module-qualified empty csr_matrix/csc_matrix constructors to lil_matrix and csr_array/csc_array constructors to lil_array. Bare imported names remain manual, and you can convert back with .tocsr() or .tocsc() once writes finish.