Repository: scipy/scipy
File: scipy/sparse/_dok.py (Python)

Code before:
                raise TypeError('Invalid input format.') from e

            if arg1.ndim > 2:
                raise ValueError(f"DOK arrays don't yet support {arg1.ndim}D input.")

            if arg1.ndim == 1:
                if dtype is not None:
                    arg1 = arg1.astype(dtype)
                self._dict = {i: v for i, v in enumerate(arg1) if v != 0}
                self.dtype = getdtype(arg1.dtype)
            else:
                d = self._coo_container(arg1, shape=shape, dtype=dtype).todok()
                self._dict = d._dict
                self.dtype = getdtype(d.dtype)
            self._shape = check_shape(arg1.shape, allow_nd=self._allow_nd)

    def update(self, val):
        """Update values from a dict, sparse dok or iterable of 2-tuples like .items()

        Keys of the input must be sequences of nonnegative integers less than the shape
        for each axis.
        """
        if isinstance(val, dict):
            inputs = val.items()
        elif issparse(val) and val.format == "dok":
            inputs = val._dict.items()
        else:
            inputs = val

        for key, value in inputs:
            index = (key,) if isintlike(key) else tuple(key)
            if len(index) != self.ndim:
                raise IndexError(f'Index {key} length needs to match self.shape')
            if not all(
                isintlike(idx) and 0 <= idx < max_idx
                for idx, max_idx in zip(index, self.shape)
            ):
                # Error handling. Re-search to find which error occured
                for idx, max_idx in zip(index, self.shape):
                    if not isintlike(idx):
                        raise IndexError(f'integer keys required for update. Got {key}')
                    if idx < 0:
                        raise IndexError(f'negative index {key} not allowed in update')
                    if idx >= max_idx:
                        raise IndexError(f'index {key} is too large for self.shape')
        # actually update
        self._dict.update(inputs)

    def _getnnz(self, axis=None):
        if axis is not None:
            raise NotImplementedError(

Review comment (suggestion):
Both the array and matrix version of `dok` are going to get fully captured by the conditional above, right?

```python
In [3]: isinstance(scipy.sparse.dok_array((3, 3)), dict)
Out[3]: True

In [4]: isinstance(scipy.sparse.dok_matrix((3, 3)), dict)
Out[4]: True
```

Looks like this line isn't covered by tests at the moment either, at least when I check locally. Any idea how we'd reach it?

The rest of the diff seems well covered by the new testing.

Code after:
                raise TypeError('Invalid input format.') from e

            if arg1.ndim > 2:
                raise ValueError(f"DOK arrays don't yet support {arg1.ndim}D input.")

            if arg1.ndim == 1:
                if dtype is not None:
                    arg1 = arg1.astype(dtype)
                self._dict = {i: v for i, v in enumerate(arg1) if v != 0}
                self.dtype = getdtype(arg1.dtype)
            else:
                d = self._coo_container(arg1, shape=shape, dtype=dtype).todok()
                self._dict = d._dict
                self.dtype = getdtype(d.dtype)
            self._shape = check_shape(arg1.shape, allow_nd=self._allow_nd)

    def update(self, val):
        """Update values from a dict, sparse dok or iterable of 2-tuples like .items()

        Keys of the input must be sequences of nonnegative integers less than the shape
        for each axis.
        """
        if isinstance(val, dict):
            inputs = val.items()
        else:
            inputs = val

        for key, value in inputs:
            index = (key,) if isintlike(key) else tuple(key)
            if len(index) != self.ndim:
                raise IndexError(f'Index {key} length needs to match self.shape')
            if not all(
                isintlike(idx) and 0 <= idx < max_idx
                for idx, max_idx in zip(index, self.shape)
            ):
                # Error handling. Re-search to find which error occured
                for idx, max_idx in zip(index, self.shape):
                    if not isintlike(idx):
                        raise IndexError(f'integer keys required for update. Got {key}')
                    if idx < 0:
                        raise IndexError(f'negative index {key} not allowed in update')
                    if idx >= max_idx:
                        raise IndexError(f'index {key} is too large for self.shape')
        # do the update
        self._dict.update(inputs)

    def _getnnz(self, axis=None):
        if axis is not None:
            raise NotImplementedError(
                "_getnnz over an axis is not implemented for DOK format."
            )