# Buggy code
def check_shape(args, current_shape=None):
    """Imitate numpy.matrix handling of shape arguments"""
    if len(args) == 0:
        raise TypeError("function missing 1 required positional argument: 'shape'")
    elif len(args) == 1:
        try:
            shape_iter = iter(args[0])
        except TypeError:
            new_shape = (operator.index(args[0]),)
        else:
            new_shape = tuple(operator.index(arg) for arg in shape_iter)
    else:
        new_shape = tuple(operator.index(arg) for arg in args)

    if current_shape is None:
        if len(new_shape) != 2:
            raise ValueError("shape must be a 2-tuple of positive integers")
        elif new_shape[0] < 0 or new_shape[1] < 0:
            raise ValueError("'shape' elements cannot be negative")

    else:
        # Check the current size only if needed
        current_size = np.prod(current_shape, dtype=int)

        # Check for negatives
        negative_indexes = [i for i, x in enumerate(new_shape) if x < 0]
        if len(negative_indexes) == 0:
            new_size = np.prod(new_shape, dtype=int)
            if new_size != current_size:
                raise ValueError(
                    "cannot reshape array of size {} into shape {}".format(
                        current_size, new_shape
                    )
                )
        elif len(negative_indexes) == 1:
            skip = negative_indexes[0]
            specified = np.prod(new_shape[0:skip] + new_shape[skip + 1 :])
            unspecified, remainder = divmod(current_size, specified)
            if remainder != 0:
                err_shape = tuple("newshape" if x < 0 else x for x in new_shape)
                raise ValueError(
                    "cannot reshape array of size {} into shape {}".format(
                        current_size, err_shape
                    )
                )
            new_shape = new_shape[0:skip] + (unspecified,) + new_shape[skip + 1 :]
        else:
            raise ValueError("can only specify one unknown dimension")

    if len(new_shape) != 2:
        raise ValueError("matrix shape must be two-dimensional")

    return new_shape


# Traceback (ValueError)
Traceback (most recent call last):

File "<ipython-input-14-b3568b621ba3>", line 3, in <module>
A.reshape((10,-1))

File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\coo.py", line 195, in reshape
shape = check_shape(args, self.shape)

File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\sputils.py", line 307, in check_shape
''.format(current_size, err_shape))

ValueError: cannot reshape array of size -262142 into shape (10, 'newshape')

# Fixed code
def check_shape(args, current_shape=None):
    """Imitate numpy.matrix handling of shape arguments"""
    if len(args) == 0:
        raise TypeError("function missing 1 required positional argument: 'shape'")
    elif len(args) == 1:
        try:
            shape_iter = iter(args[0])
        except TypeError:
            new_shape = (operator.index(args[0]),)
        else:
            new_shape = tuple(operator.index(arg) for arg in shape_iter)
    else:
        new_shape = tuple(operator.index(arg) for arg in args)

    if current_shape is None:
        if len(new_shape) != 2:
            raise ValueError("shape must be a 2-tuple of positive integers")
        elif new_shape[0] < 0 or new_shape[1] < 0:
            raise ValueError("'shape' elements cannot be negative")

    else:
        # Check the current size only if needed
        current_size = prod(current_shape)

        # Check for negatives
        negative_indexes = [i for i, x in enumerate(new_shape) if x < 0]
        if len(negative_indexes) == 0:
            new_size = prod(new_shape)
            if new_size != current_size:
                raise ValueError(
                    "cannot reshape array of size {} into shape {}".format(
                        current_size, new_shape
                    )
                )
        elif len(negative_indexes) == 1:
            skip = negative_indexes[0]
            specified = prod(new_shape[0:skip] + new_shape[skip + 1 :])
            unspecified, remainder = divmod(current_size, specified)
            if remainder != 0:
                err_shape = tuple("newshape" if x < 0 else x for x in new_shape)
                raise ValueError(
                    "cannot reshape array of size {} into shape {}".format(
                        current_size, err_shape
                    )
                )
            new_shape = new_shape[0:skip] + (unspecified,) + new_shape[skip + 1 :]
        else:
            raise ValueError("can only specify one unknown dimension")

    if len(new_shape) != 2:
        raise ValueError("matrix shape must be two-dimensional")

    return new_shape