# 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.size == 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.shape != i.shape:
            raise ValueError("shape mismatch in assignment")
        if x.size == 0:
            return
        self._set_arrayXarray(i, j, x)


# Traceback (ValueError)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/philippe/anaconda3/envs/venv/lib/python3.7/site-packages/scipy/sparse/_index.py", line 146, in __setitem__
raise ValueError("shape mismatch in assignment")
ValueError: shape mismatch in assignment

# 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.size == 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)