Skip to content

Commit f6a0515

Browse files
Merge pull request #589 from DimitriPapadopoulos/ruff
Apply ruff preview rules
2 parents 0ef157b + 5f8656e commit f6a0515

12 files changed

Lines changed: 41 additions & 39 deletions

File tree

doc/getting_started/tutorials/09.ucodecs-ufilters.ipynb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,7 @@
621621
],
622622
"source": [
623623
"array = blosc2.zeros((30, 30))\n",
624-
"array.schunk.cparams = blosc2.CParams(\n",
625-
" **{\"codec\": 184, \"filters\": [filter_id], \"filters_meta\": [0], \"nthreads\": 1}\n",
626-
")\n",
624+
"array.schunk.cparams = blosc2.CParams(codec=184, filters=[filter_id], filters_meta=[0], nthreads=1)\n",
627625
"array.schunk.cparams"
628626
]
629627
}

doc/getting_started/tutorials/10.prefilters.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@
324324
},
325325
"outputs": [],
326326
"source": [
327-
"my_schunk.dparams = blosc2.DParams(**{\"nthreads\": 1}) # Disable parallelism for decompression\n",
327+
"my_schunk.dparams = blosc2.DParams(nthreads=1) # Disable parallelism for decompression\n",
328328
"\n",
329329
"\n",
330330
"@my_schunk.postfilter(input_dtype)\n",

src/blosc2/__init__.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,17 +393,21 @@ def _raise(exc):
393393
"max dimensions": MAX_DIM,
394394
},
395395
default_device=lambda: "cpu",
396-
default_dtypes=lambda device=None: {
397-
"real floating": DEFAULT_FLOAT,
398-
"complex floating": DEFAULT_COMPLEX,
399-
"integral": DEFAULT_INT,
400-
"indexing": DEFAULT_INDEX,
401-
}
402-
if (device == "cpu" or device is None)
403-
else _raise(ValueError("Only cpu devices allowed")),
404-
dtypes=lambda device=None, kind=None: np.__array_namespace_info__().dtypes(kind=kind, device=device)
405-
if (device == "cpu" or device is None)
406-
else _raise(ValueError("Only cpu devices allowed")),
396+
default_dtypes=lambda device=None: (
397+
{
398+
"real floating": DEFAULT_FLOAT,
399+
"complex floating": DEFAULT_COMPLEX,
400+
"integral": DEFAULT_INT,
401+
"indexing": DEFAULT_INDEX,
402+
}
403+
if (device == "cpu" or device is None)
404+
else _raise(ValueError("Only cpu devices allowed"))
405+
),
406+
dtypes=lambda device=None, kind=None: (
407+
np.__array_namespace_info__().dtypes(kind=kind, device=device)
408+
if (device == "cpu" or device is None)
409+
else _raise(ValueError("Only cpu devices allowed"))
410+
),
407411
devices=lambda: ["cpu"],
408412
name="blosc2",
409413
version=__version__,

src/blosc2/lazyexpr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def ne_evaluate(expression, local_dict=None, **kwargs):
115115
if e.args and e.args[0] == "NumExpr 2 does not support Unicode as a dtype.":
116116
pass
117117
else:
118-
raise e # unsafe expression
118+
raise # unsafe expression
119119
except Exception:
120120
pass
121121
# Try with blosc2 funcs as presence of non-numexpr funcs probably caused failure
@@ -143,7 +143,7 @@ def ne_evaluate(expression, local_dict=None, **kwargs):
143143

144144
def _get_result(expression, chunk_operands, ne_args, where=None, indices=None, _order=None):
145145
chunk_indices = None
146-
if (expression == "o0" or expression == "(o0)") and where is None:
146+
if expression in {"o0", "(o0)"} and where is None:
147147
# We don't have an actual expression, so avoid a copy except to make contiguous (later)
148148
return chunk_operands["o0"], None
149149
# Apply the where condition (in result)
@@ -1750,7 +1750,7 @@ def slices_eval( # noqa: C901
17501750
# Check whether current cslice intersects with _slice
17511751
cslice = chunk_slice.raw
17521752
nchunk = (
1753-
builtins.sum([c.start // chunks[i] * np.prod(ratio[i + 1 :]) for i, c in enumerate(cslice)])
1753+
builtins.sum(c.start // chunks[i] * np.prod(ratio[i + 1 :]) for i, c in enumerate(cslice))
17541754
if 0 not in chunks
17551755
else 0
17561756
)
@@ -2273,7 +2273,7 @@ def reduce_slices( # noqa: C901
22732273
for chunk_slice in intersecting_chunks:
22742274
cslice = chunk_slice.raw
22752275
nchunk = (
2276-
builtins.sum([c.start // chunks[i] * np.prod(ratio[i + 1 :]) for i, c in enumerate(cslice)])
2276+
builtins.sum(c.start // chunks[i] * np.prod(ratio[i + 1 :]) for i, c in enumerate(cslice))
22772277
if 0 not in chunks
22782278
else 0
22792279
)

src/blosc2/ndarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4352,7 +4352,7 @@ def __getitem__(
43524352

43534353
def __setitem__(
43544354
self,
4355-
key: None | int | slice | Sequence[slice | int | np.bool_ | np.ndarray[int | np.bool_] | None],
4355+
key: int | slice | Sequence[slice | int | np.bool_ | np.ndarray[int | np.bool_] | None] | None,
43564356
value: object,
43574357
):
43584358
"""Set a slice of the array.

src/blosc2/proxy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def _convert_dtype(dt: str | DTypeLike):
586586
except TypeError: # likely passed e.g. a torch.float64
587587
return np.dtype(str(dt).split(".")[1])
588588
except Exception as e:
589-
raise TypeError("Could not parse dtype arg {dt}.") from e
589+
raise TypeError(f"Could not parse dtype arg {dt}.") from e
590590

591591

592592
class SimpleProxy(blosc2.Operand):
@@ -631,7 +631,7 @@ def is_ints_sequence(src, attr):
631631
chunks = src.chunks if chunks is None and is_ints_sequence(src, "chunks") else chunks
632632
blocks = src.blocks if blocks is None and is_ints_sequence(src, "blocks") else blocks
633633
self.chunks, self.blocks = blosc2.compute_chunks_blocks(
634-
self.shape, chunks, blocks, self.dtype, **{"cparams": cparams}
634+
self.shape, chunks, blocks, self.dtype, cparams=cparams
635635
)
636636

637637
@property

src/blosc2/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def linalg_shape(func_name, args, kwargs): # noqa: C901
262262
axis = 0 if axis is None else axis
263263
# normalize negative axis
264264
axis = axis + len(shapes[0]) if axis < 0 else axis
265-
concat_dim = builtins.sum([s[axis] for s in shapes])
265+
concat_dim = builtins.sum(s[axis] for s in shapes)
266266
return tuple(s if i != axis else concat_dim for i, s in enumerate(shapes[0]))
267267

268268
# --- diagonal ---
@@ -581,7 +581,7 @@ def visit_Call(self, node): # noqa : C901
581581
return (num,)
582582
raise ValueError("linspace requires either shape or num argument")
583583

584-
elif base_name == "frombuffer" or base_name == "fromiter":
584+
elif base_name in {"frombuffer", "fromiter"}:
585585
count = kwargs.get("count")
586586
return (count,) if count else ()
587587

@@ -895,7 +895,7 @@ def _indices(iters):
895895
]
896896
if nchunk:
897897
yield builtins.sum(
898-
[c.start // chunks[i] * np.prod(ratio[i + 1 :]) for i, c in enumerate(my_list)]
898+
c.start // chunks[i] * np.prod(ratio[i + 1 :]) for i, c in enumerate(my_list)
899899
)
900900
else:
901901
yield ndindex.Tuple(*my_list)

tests/ndarray/test_copy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ def test_values(shape, chunks1, blocks1, chunks2, blocks2, dtype):
5555
b = a.copy(chunks=chunks2, blocks=blocks2, cparams=cparams2)
5656
assert a.shape == b.shape
5757
assert a.schunk.dparams == b.schunk.dparams
58-
for key in cparams2:
58+
for key, value in cparams2.items():
5959
if key in ("filters", "filters_meta"):
60-
assert getattr(b.schunk.cparams, key)[: len(cparams2[key])] == cparams2[key]
60+
assert getattr(b.schunk.cparams, key)[: len(value)] == value
6161
continue
62-
assert getattr(b.schunk.cparams, key) == cparams2[key]
62+
assert getattr(b.schunk.cparams, key) == value
6363
assert b.chunks == tuple(chunks2)
6464
assert b.blocks == tuple(blocks2)
6565
assert a.dtype == b.dtype

tests/ndarray/test_elementwise_funcs.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _test_unary_func_impl(np_func, blosc_func, dtype, shape, chunkshape):
105105
):
106106
assert True
107107
else:
108-
raise e
108+
raise
109109

110110

111111
def _test_binary_func_proxy(np_func, blosc_func, dtype, shape, chunkshape, xp):
@@ -168,12 +168,12 @@ def _test_binary_func_proxy(np_func, blosc_func, dtype, shape, chunkshape, xp):
168168
): # not supported for complex dtypes
169169
assert True
170170
else:
171-
raise e
171+
raise
172172
except NotImplementedError as e:
173173
if np_func.__name__ in ("left_shift", "right_shift", "floor_divide", "power", "remainder"):
174174
assert True
175175
else:
176-
raise e
176+
raise
177177
except AssertionError as e:
178178
if np_func.__name__ == "power" and blosc2.isdtype(
179179
dtype, "integral"
@@ -189,7 +189,7 @@ def _test_binary_func_proxy(np_func, blosc_func, dtype, shape, chunkshape, xp):
189189
)
190190
pytest.skip("minimum and maximum for numexpr do not match NaN behaviour for numpy")
191191
else:
192-
raise e
192+
raise
193193

194194

195195
def _test_unary_func_proxy(np_func, blosc_func, dtype, shape, xp):
@@ -228,7 +228,7 @@ def _test_unary_func_proxy(np_func, blosc_func, dtype, shape, xp):
228228
):
229229
assert True
230230
else:
231-
raise e
231+
raise
232232

233233

234234
def _test_binary_func_impl(np_func, blosc_func, dtype, shape, chunkshape):
@@ -283,12 +283,12 @@ def _test_binary_func_impl(np_func, blosc_func, dtype, shape, chunkshape):
283283
): # not supported for complex dtypes
284284
assert True
285285
else:
286-
raise e
286+
raise
287287
except NotImplementedError as e:
288288
if np_func.__name__ in ("left_shift", "right_shift", "floor_divide", "power", "remainder"):
289289
assert True
290290
else:
291-
raise e
291+
raise
292292
except AssertionError as e:
293293
if np_func.__name__ == "power" and blosc2.isdtype(
294294
dtype, "integral"
@@ -304,7 +304,7 @@ def _test_binary_func_impl(np_func, blosc_func, dtype, shape, chunkshape):
304304
)
305305
pytest.skip("minimum and maximum for numexpr do not match NaN behaviour for numpy")
306306
else:
307-
raise e
307+
raise
308308

309309

310310
@pytest.mark.parametrize(("np_func", "blosc_func"), UNARY_FUNC_PAIRS)

tests/ndarray/test_jit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_expr_kwargs(sample_data):
6868
# Testing jit decorator with kwargs
6969
cparams = blosc2.CParams(clevel=1, codec=blosc2.Codec.LZ4, filters=[blosc2.Filter.BITSHUFFLE])
7070

71-
@blosc2.jit(**{"cparams": cparams})
71+
@blosc2.jit(cparams=cparams)
7272
def expr_jit_cparams(a, b, c):
7373
return ((a**3 + np.sin(a * 2)) < c) & (b > 0)
7474

0 commit comments

Comments
 (0)