From d4d74fdd0ef9b3ce08b7796cac70ce61cba41f45 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Thu, 4 Jan 2024 15:14:09 +0100 Subject: [PATCH] Resolve linting issues. --- sparse/_common.py | 22 ++++++++++++---------- sparse/_compressed/convert.py | 4 ++-- sparse/_coo/common.py | 9 ++++++--- sparse/_coo/core.py | 5 ++++- sparse/_coo/indexing.py | 15 ++++++++------- sparse/_coo/numba_extension.py | 2 +- sparse/_io.py | 4 ++-- sparse/_sparse_array.py | 10 +++++----- sparse/_umath.py | 14 +++++++------- sparse/_utils.py | 6 +++--- sparse/tests/test_coo.py | 12 ++++++------ sparse/tests/test_dot.py | 8 ++++++-- sparse/tests/test_elemwise.py | 2 +- 13 files changed, 63 insertions(+), 50 deletions(-) diff --git a/sparse/_common.py b/sparse/_common.py index 2429bd31..6be68266 100644 --- a/sparse/_common.py +++ b/sparse/_common.py @@ -204,10 +204,10 @@ def matmul(a, b): """ check_zero_fill_value(a, b) if not hasattr(a, "ndim") or not hasattr(b, "ndim"): - raise TypeError("Cannot perform dot product on types %s, %s" % (type(a), type(b))) + raise TypeError(f"Cannot perform dot product on types {type(a)}, {type(b)}") if check_class_nan(a) or check_class_nan(b): - warnings.warn("Nan will not be propagated in matrix multiplication", RuntimeWarning) + warnings.warn("Nan will not be propagated in matrix multiplication", RuntimeWarning, stacklevel=1) # When b is 2-d, it is equivalent to dot if b.ndim <= 2: @@ -283,7 +283,7 @@ def dot(a, b): """ check_zero_fill_value(a, b) if not hasattr(a, "ndim") or not hasattr(b, "ndim"): - raise TypeError("Cannot perform dot product on types %s, %s" % (type(a), type(b))) + raise TypeError(f"Cannot perform dot product on types {type(a)}, {type(b)}") if a.ndim == 1 and b.ndim == 1: if isinstance(a, SparseArray): @@ -813,7 +813,6 @@ def _dot_csc_ndarray_sparse(a_shape, b_shape, a_data, a_indices, a_indptr, b): mask[ind] = head head = ind length += 1 - start = nnz for _ in range(length): if sums[head] != 0: indices[nnz] = head @@ -953,7 +952,6 @@ def _dot_coo_coo(out_shape, a_coords, b_coords, a_data, b_data, a_indptr, b_indp head = k length += 1 - start = nnz for _ in range(length): if next_[head] != -1: coords[0, nnz] = i @@ -1199,7 +1197,7 @@ def _parse_einsum_input(operands): tmp_operands = list(operands) operand_list = [] subscript_list = [] - for p in range(len(operands) // 2): + for _ in range(len(operands) // 2): operand_list.append(tmp_operands.pop(0)) subscript_list.append(tmp_operands.pop(0)) @@ -1361,7 +1359,7 @@ def _einsum_single(lhs, rhs, operand): where.setdefault(ix, []).append(i) selector = None - for ix, locs in where.items(): + for locs in where.values(): loc0, *rlocs = locs if rlocs: # repeated index @@ -1956,7 +1954,9 @@ def pad(array, pad_width, mode="constant", **kwargs): Sparse array which is to be padded. pad_width : {sequence, array_like, int} - Number of values padded to the edges of each axis. ((before_1, after_1), … (before_N, after_N)) unique pad widths for each axis. ((before, after),) yields same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes. + Number of values padded to the edges of each axis. ((before_1, after_1), … (before_N, after_N)) unique pad + widths for each axis. ((before, after),) yields same before and after pad for each axis. (pad,) or int is a + shortcut for before = after = pad width for all axes. mode : str Pads to a constant value which is fill value. Currently only constant mode is implemented @@ -2059,7 +2059,7 @@ def asarray(obj, /, *, dtype=None, format="coo", backend="pydata", device=None, if backend == "pydata": if isinstance(obj, (COO, DOK, GCXS)): # TODO: consider `format` argument - warnings.warn("`format` argument was ignored") + warnings.warn("`format` argument was ignored", RuntimeWarning, stacklevel=1) return obj elif isinstance(obj, spmatrix): @@ -2092,7 +2092,9 @@ def wrapper_func(*args, **kwargs): if isinstance(x, (np.ndarray, np.number)): warnings.warn( f"Sparse {func.__name__} received dense NumPy array instead " - "of sparse array. Dispatching to NumPy function." + "of sparse array. Dispatching to NumPy function.", + RuntimeWarning, + stacklevel=2, ) return getattr(np, func.__name__)(*args, **kwargs) else: diff --git a/sparse/_compressed/convert.py b/sparse/_compressed/convert.py index 15dbc0bc..a38ae137 100644 --- a/sparse/_compressed/convert.py +++ b/sparse/_compressed/convert.py @@ -45,7 +45,7 @@ def compute_flat(increments, cols, operations): # pragma: no cover end = increments[-1].shape[0] positions = np.zeros(len(increments) - 1, dtype=np.intp) pos = len(increments) - 2 - for i in range(operations): + for _ in range(operations): to_add = 0 for j in range(len(increments) - 1): to_add += increments[j][positions[j]] @@ -314,7 +314,7 @@ def _convert_coords( new_compressed_shape, transpose, ): # pragma: no cover - if transpose == True: + if transpose: for i, n in enumerate(linear): # c ordering current_coords = unravel_index(n, reordered_shape)[sorted_axis_order] diff --git a/sparse/_coo/common.py b/sparse/_coo/common.py index 6b20d4c4..12146a9b 100644 --- a/sparse/_coo/common.py +++ b/sparse/_coo/common.py @@ -787,9 +787,11 @@ def roll(a, shift, axis=None): for sh, ax in zip(shift, axis): coords[ax] += sh coords[ax] %= a.shape[ax] - except UFuncTypeError: + except UFuncTypeError as e: if is_unsigned_dtype(coords.dtype): - raise ValueError(f"rolling with coords.dtype as {coords.dtype} is not safe. Try using a signed dtype.") + raise ValueError( + f"rolling with coords.dtype as {coords.dtype} is not safe. Try using a signed dtype." + ) from e return COO( coords, @@ -870,7 +872,8 @@ def diagonalize(a, axis=0): """ Diagonalize a COO array. The new dimension is appended at the end. - .. WARNING:: :obj:`diagonalize` is not :obj:`numpy` compatible as there is no direct :obj:`numpy` equivalent. The API may change in the future. + .. WARNING:: :obj:`diagonalize` is not :obj:`numpy` compatible as there is no direct :obj:`numpy` equivalent. The + API may change in the future. Parameters ---------- diff --git a/sparse/_coo/core.py b/sparse/_coo/core.py index 48aa5c74..b545eed3 100644 --- a/sparse/_coo/core.py +++ b/sparse/_coo/core.py @@ -221,6 +221,7 @@ def __init__( warnings.warn( "coords should be an ndarray. This will raise a ValueError in the future.", DeprecationWarning, + stacklevel=1, ) if data is None: @@ -249,6 +250,7 @@ def __init__( warnings.warn( "shape should be provided. This will raise a ValueError in the future.", DeprecationWarning, + stacklevel=1, ) if self.coords.nbytes: shape = tuple(self.coords.max(axis=1) + 1) @@ -292,6 +294,7 @@ def __init__( "memory than than an equivalent dense array. You may want to " "use a dense array here instead.", RuntimeWarning, + stacklevel=1, ) if not sorted: @@ -1134,7 +1137,7 @@ def resize(self, *args, refcheck=True, coords_dtype=np.intp): numpy.ndarray.resize : The equivalent Numpy function. """ - warnings.warn("resize is deprecated on all SpraseArray objects.", DeprecationWarning) + warnings.warn("resize is deprecated on all SpraseArray objects.", DeprecationWarning, stacklevel=1) if len(args) == 1 and isinstance(args[0], tuple): shape = args[0] elif all(isinstance(arg, int) for arg in args): diff --git a/sparse/_coo/indexing.py b/sparse/_coo/indexing.py index dd458fc7..2928af20 100644 --- a/sparse/_coo/indexing.py +++ b/sparse/_coo/indexing.py @@ -148,7 +148,8 @@ def _mask(coords, indices, shape): for ai in adv_idx: if len(ai) != adv_ix_len: raise IndexError( - "shape mismatch: indexing arrays could not be broadcast together. Ensure all indexing arrays are of the same length." + "shape mismatch: indexing arrays could not be broadcast together. Ensure all indexing arrays " + "are of the same length." ) if ai.ndim != 1: raise IndexError("Only one-dimensional iterable indices supported.") @@ -241,15 +242,15 @@ def _prune_indices(indices, shape, prune_none=True): indices = [idx for idx in indices if idx is not None] i = 0 - for idx, l in zip(indices[::-1], shape[::-1]): + for idx, sh in zip(indices[::-1], shape[::-1]): if not isinstance(idx, slice): break - if idx.start == 0 and idx.stop == l and idx.step == 1: + if idx.start == 0 and idx.stop == sh and idx.step == 1: i += 1 continue - if idx.start == l - 1 and idx.stop == -1 and idx.step == -1: + if idx.start == sh - 1 and idx.stop == -1 and idx.step == -1: i += 1 continue @@ -648,12 +649,12 @@ def _join_adjacent_pairs(starts_old, stops_old): # pragma: no cover @numba.jit(nopython=True, nogil=True) -def array_from_list_intp(l): # pragma: no cover - n = len(l) +def array_from_list_intp(x): # pragma: no cover + n = len(x) a = np.empty(n, dtype=np.intp) for i in range(n): - a[i] = l[i] + a[i] = x[i] return a diff --git a/sparse/_coo/numba_extension.py b/sparse/_coo/numba_extension.py index 71678318..c372cff6 100644 --- a/sparse/_coo/numba_extension.py +++ b/sparse/_coo/numba_extension.py @@ -210,7 +210,7 @@ def unbox_COO(typ: COOType, obj: COO, c) -> NativeValue: @box(COOType) -def box_COO(typ: COOType, val: "some LLVM thing", c) -> COO: +def box_COO(typ: COOType, val, c) -> COO: ret_ptr = cgutils.alloca_once(c.builder, c.pyapi.pyobj) fail_obj = c.pyapi.get_null_object() diff --git a/sparse/_io.py b/sparse/_io.py index 851d7d5b..eda1736b 100644 --- a/sparse/_io.py +++ b/sparse/_io.py @@ -128,5 +128,5 @@ def load_npz(filename): fill_value=fill_value, compressed_axes=comp_axes, ) - except KeyError: - raise RuntimeError(f"The file {filename!s} does not contain a valid sparse matrix") + except KeyError as e: + raise RuntimeError(f"The file {filename!s} does not contain a valid sparse matrix") from e diff --git a/sparse/_sparse_array.py b/sparse/_sparse_array.py index 2e525b2d..f1a41f82 100644 --- a/sparse/_sparse_array.py +++ b/sparse/_sparse_array.py @@ -33,10 +33,10 @@ def __init__(self, shape, fill_value=None): if not isinstance(shape, Iterable): shape = (shape,) - if not all(isinstance(l, Integral) and int(l) >= 0 for l in shape): + if not all(isinstance(sh, Integral) and int(sh) >= 0 for sh in shape): raise ValueError("shape must be an non-negative integer or a tuple " "of non-negative integers.") - self.shape = tuple(int(l) for l in shape) + self.shape = tuple(int(sh) for sh in shape) if fill_value is not None: if not hasattr(fill_value, "dtype") or fill_value.dtype != self.dtype: @@ -348,8 +348,8 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): (out,) = out if out.shape != result.shape: raise ValueError( - "non-broadcastable output operand with shape %s " - "doesn't match the broadcast shape %s" % (out.shape, result.shape) + f"non-broadcastable output operand with shape {out.shape} " + f"doesn't match the broadcast shape {result.shape}" ) out._make_shallow_copy_of(result) @@ -787,7 +787,7 @@ def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False): rcount = reduce(operator.mul, (self.shape[a] for a in axis), 1) # Make this warning show up on top. if ddof >= rcount: - warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning) + warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning, stacklevel=1) # Cast bool, unsigned int, and int to float64 by default if dtype is None and issubclass(self.dtype.type, (np.integer, np.bool_)): diff --git a/sparse/_umath.py b/sparse/_umath.py index 43642449..15c6d3e9 100644 --- a/sparse/_umath.py +++ b/sparse/_umath.py @@ -115,9 +115,9 @@ def _get_nary_broadcast_shape(*shapes): for shape in shapes: try: result_shape = _get_broadcast_shape(shape, result_shape) - except ValueError: + except ValueError as e: shapes_str = ", ".join(str(shape) for shape in shapes) - raise ValueError("operands could not be broadcast together with shapes %s" % shapes_str) + raise ValueError(f"operands could not be broadcast together with shapes {shapes_str}") from e return result_shape @@ -145,7 +145,7 @@ def _get_broadcast_shape(shape1, shape2, is_result=False): """ # https://stackoverflow.com/a/47244284/774273 if not all((l1 == l2) or (l1 == 1) or ((l2 == 1) and not is_result) for l1, l2 in zip(shape1[::-1], shape2[::-1])): - raise ValueError("operands could not be broadcast together with shapes %s, %s" % (shape1, shape2)) + raise ValueError(f"operands could not be broadcast together with shapes {shape1}, {shape2}") result_shape = tuple(l1 if l1 != 1 else l2 for l1, l2 in zip_longest(shape1[::-1], shape2[::-1], fillvalue=1))[::-1] @@ -214,7 +214,7 @@ def _get_reduced_shape(shape, params): reduced_coords : np.ndarray The reduced coordinates. """ - reduced_shape = tuple(l for l, p in zip(shape, params) if p) + reduced_shape = tuple(sh for sh, p in zip(shape, params) if p) return reduced_shape @@ -244,13 +244,13 @@ def _get_expanded_coords_data(coords, data, params, broadcast_shape): """ first_dim = -1 expand_shapes = [] - for d, p, l in zip(range(len(broadcast_shape)), params, broadcast_shape): + for d, p, sh in zip(range(len(broadcast_shape)), params, broadcast_shape): if p and first_dim == -1: expand_shapes.append(coords.shape[1]) first_dim = d if not p: - expand_shapes.append(l) + expand_shapes.append(sh) all_idx = _cartesian_product(*(np.arange(d, dtype=np.intp) for d in expand_shapes)) @@ -266,7 +266,7 @@ def _get_expanded_coords_data(coords, data, params, broadcast_shape): expanded_data = np.repeat(data, np.prod(broadcast_shape, dtype=np.int64)) return np.asarray(expanded_coords), np.asarray(expanded_data) - for d, p, l in zip(range(len(broadcast_shape)), params, broadcast_shape): + for d, p in zip(range(len(broadcast_shape)), params): if p: expanded_coords[d] = coords[dim, all_idx[first_dim]] else: diff --git a/sparse/_utils.py b/sparse/_utils.py index 12e7cb76..d784077a 100644 --- a/sparse/_utils.py +++ b/sparse/_utils.py @@ -110,7 +110,7 @@ def algD(n, N, random_state=None): random_state = seed for random number generation """ - if random_state != None: + if random_state is not None: np.random.seed(random_state) n = np.int64(n + 1) N = np.int64(N) @@ -169,7 +169,7 @@ def algA(n, N, random_state=None): N = size of system (elements) random_state = seed for random number generation """ - if random_state != None: + if random_state is not None: np.random.seed(random_state) n = np.int64(n) N = np.int64(N) @@ -498,7 +498,7 @@ def html_table(arr): info.append(str(arr.compressed_axes)) for h, i in zip(headings, info): - table += "" '%s' '%s' "" % (h, i) + table += "" f'{h}' f'{i}' "" table += "" table += "" return table diff --git a/sparse/tests/test_coo.py b/sparse/tests/test_coo.py index 15a891c4..42c9e6c2 100644 --- a/sparse/tests/test_coo.py +++ b/sparse/tests/test_coo.py @@ -227,7 +227,7 @@ def test_resize(a, b): orig_size = s.size x = s.todense() - x.resize(b) + x = np.resize(x, b) s.resize(b) temp = x.reshape(x.size) temp[orig_size:] = s.fill_value @@ -304,7 +304,7 @@ def test_moveaxis_error(source, destination): [(), ()], ], ) -def test_resize(a, b): +def test_resize_2(a, b): s = sparse.random(a, density=0.5) orig_size = s.size x = s.todense() @@ -839,7 +839,7 @@ def test_caching(): x = COO({(1, 1, 1, 1, 1, 1, 1, 2): 1}, cache=True) - for i in range(x.ndim): + for _ in range(x.ndim): x.reshape(x.size) assert len(x._cache["reshape"]) < 5 @@ -1238,7 +1238,7 @@ def test_pickle(): x = sparse.COO.from_numpy([1, 0, 0, 0, 0]).reshape((5, 1)) # Enable caching and add some data to it x.enable_caching() - x.T + x.T # noqa: B018 assert x._cache is not None # Pickle sends data but not cache x2 = pickle.loads(pickle.dumps(x)) @@ -1251,7 +1251,7 @@ def test_copy(deep): x = sparse.COO.from_numpy([1, 0, 0, 0, 0]).reshape((5, 1)) # Enable caching and add some data to it x.enable_caching() - x.T + x.T # noqa: B018 assert x._cache is not None x2 = x.copy(deep) @@ -1650,7 +1650,7 @@ def test_array_as_shape(): coords = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] data = [10, 20, 30, 40, 50] - s = sparse.COO(coords, data, shape=np.array((5, 5))) + sparse.COO(coords, data, shape=np.array((5, 5))) @pytest.mark.parametrize( diff --git a/sparse/tests/test_dot.py b/sparse/tests/test_dot.py index 4ccaf268..9925db08 100644 --- a/sparse/tests/test_dot.py +++ b/sparse/tests/test_dot.py @@ -292,7 +292,9 @@ def test_small_values(format1, format2): s1 = format1(sparse.COO(coords=[[0, 10]], data=[3.6e-100, 7.2e-009], shape=(20,))) s2 = format2(sparse.COO(coords=[[0, 0], [4, 28]], data=[3.8e-25, 4.5e-225], shape=(20, 50))) - dense_convertor = lambda x: x.todense() if isinstance(x, sparse.SparseArray) else x + def dense_convertor(x): + return x.todense() if isinstance(x, sparse.SparseArray) else x + x1, x2 = dense_convertor(s1), dense_convertor(s2) assert_eq(x1 @ x2, s1 @ s2) @@ -311,7 +313,9 @@ def test_complex(dtype1, dtype2, format1, format2, ndim1, ndim2): s1 = format1(sparse.random((20,) * ndim1, density=0.5).astype(dtype1)) s2 = format2(sparse.random((20,) * ndim2, density=0.5).astype(dtype2)) - dense_convertor = lambda x: x.todense() if isinstance(x, sparse.SparseArray) else x + def dense_convertor(x): + return x.todense() if isinstance(x, sparse.SparseArray) else x + x1, x2 = dense_convertor(s1), dense_convertor(s2) assert_eq(x1 @ x2, s1 @ s2) diff --git a/sparse/tests/test_elemwise.py b/sparse/tests/test_elemwise.py index 5b3d7085..827c9a4c 100644 --- a/sparse/tests/test_elemwise.py +++ b/sparse/tests/test_elemwise.py @@ -716,7 +716,7 @@ def test_nanmean_regression(dtype): def test_no_deprecation_warning(): a = np.array([1, 2]) s = sparse.COO(a, a, shape=(3,)) - s == s + assert_eq(s == s, np.broadcast_to(True, s.shape)) # Regression test for gh-587