# Buggy code
def __setitem__(self, key, x):
    row, col = self._validate_indices(key)

    if isinstance(row, INT_TYPES) and isinstance(col, INT_TYPES):
        x = np.asarray(x, dtype=self.dtype)
        if x.size != 1:
            raise ValueError("Trying to assign a sequence to an item")
        self._set_intXint(row, col, x.flat[0])
        return

    if isinstance(row, slice):
        row = np.arange(*row.indices(self.shape[0]))[:, None]
    else:
        row = np.atleast_1d(row)

    if isinstance(col, slice):
        col = np.arange(*col.indices(self.shape[1]))[None, :]
        if row.ndim == 1:
            row = row[:, None]
    else:
        col = np.atleast_1d(col)

    i, j = _broadcast_arrays(row, col)
    if i.shape != j.shape:
        raise IndexError("number of row and column indices differ")

    from .base import isspmatrix

    if isspmatrix(x):
        if i.ndim == 1:
            # Inner indexing, so treat them like row vectors.
            i = i[None]
            j = j[None]
        broadcast_row = x.shape[0] == 1 and i.shape[0] != 1
        broadcast_col = x.shape[1] == 1 and i.shape[1] != 1
        if not (
            (broadcast_row or x.shape[0] == i.shape[0])
            and (broadcast_col or x.shape[1] == i.shape[1])
        ):
            raise ValueError("shape mismatch in assignment")
        if x.shape[0] == 0 or x.shape[1] == 0:
            return
        x = x.tocoo(copy=True)
        x.sum_duplicates()
        self._set_arrayXarray_sparse(i, j, x)
    else:
        # Make x and i into the same shape
        x = np.asarray(x, dtype=self.dtype)
        x, _ = _broadcast_arrays(x, i)
        if x.size == 0:
            return
        x = x.reshape(i.shape)
        self._set_arrayXarray(i, j, x)


# Traceback (ValueError)
logfile, "wrong broadcasting" marked --
-- A: ndarray (3, 3)
A[0,:] = ones
A[:,0] = ones

-- A: lil_matrix (3, 3)
A[0,:] = ones
A[:,0] = ones
Traceback (most recent call last):
File "setcol.py", line 26, in <module>
A[:,0] = ones
│        └ array([1., 1., 1.])
└ <3x3 sparse matrix of type '<class 'numpy.float64'>'
with 5 stored elements in List of Lists format>
File "/opt/local/py3/lib/python3.7/site-packages/scipy/sparse/lil.py", line 333, in __setitem__
IndexMixin.__setitem__(self, key, x)
│                      │     │    └ array([1., 1., 1.])
│                      │     └ (slice(None, None, None), 0)
│                      └ <3x3 sparse matrix of type '<class 'numpy.float64'>'
----------------------------- 3x3: wrong broadcasting
with 5 stored elements in List of Lists format>
└ <class 'scipy.sparse._index.IndexMixin'>
File "/opt/local/py3/lib/python3.7/site-packages/scipy/sparse/_index.py", line 123, in __setitem__
x = x.reshape(i.shape)
│   │         └ array([[0],
[1],
[2]])
│   └ array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
└ array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
ValueError: cannot reshape array of size 9 into shape (3,1)

# Fixed code
def __setitem__(self, key, x):
    row, col = self._validate_indices(key)

    if isinstance(row, INT_TYPES) and isinstance(col, INT_TYPES):
        x = np.asarray(x, dtype=self.dtype)
        if x.size != 1:
            raise ValueError("Trying to assign a sequence to an item")
        self._set_intXint(row, col, x.flat[0])
        return

    if isinstance(row, slice):
        row = np.arange(*row.indices(self.shape[0]))[:, None]
    else:
        row = np.atleast_1d(row)

    if isinstance(col, slice):
        col = np.arange(*col.indices(self.shape[1]))[None, :]
        if row.ndim == 1:
            row = row[:, None]
    else:
        col = np.atleast_1d(col)

    i, j = _broadcast_arrays(row, col)
    if i.shape != j.shape:
        raise IndexError("number of row and column indices differ")

    from .base import isspmatrix

    if isspmatrix(x):
        if i.ndim == 1:
            # Inner indexing, so treat them like row vectors.
            i = i[None]
            j = j[None]
        broadcast_row = x.shape[0] == 1 and i.shape[0] != 1
        broadcast_col = x.shape[1] == 1 and i.shape[1] != 1
        if not (
            (broadcast_row or x.shape[0] == i.shape[0])
            and (broadcast_col or x.shape[1] == i.shape[1])
        ):
            raise ValueError("shape mismatch in assignment")
        if x.shape[0] == 0 or x.shape[1] == 0:
            return
        x = x.tocoo(copy=True)
        x.sum_duplicates()
        self._set_arrayXarray_sparse(i, j, x)
    else:
        # Make x and i into the same shape
        x = np.asarray(x, dtype=self.dtype)
        if x.squeeze().shape != i.squeeze().shape:
            x = np.broadcast_to(x, i.shape)
        if x.size == 0:
            return
        x = x.reshape(i.shape)
        self._set_arrayXarray(i, j, x)