Skip to content

Commit 9cd6bde

Browse files
Last batch of ruff rules (#658)
* Enforce ruff rule RUF001 Disable the rule directly in the incriminated test file. * Enforce ruff/pyupgrade rule UP007 UP007 Use `X | Y` for type annotations * Enforce ruff/flake8-bugbear rules B028 and B904 * Exclude c-blosc from ruff linting * Ignore ruff/pycodestyle rule W391 astral-sh/ruff#13763 * Enforce ruff/flake8-logging rules (LOG) * Enforce ruff/flake8-implicit-str-concat rules (ISC) * Enforce ruff/flake8-executable rules (EXE) * Enforce ruff/flake8-future-annotations rules (FA) * Enforce ruff/flake8-return rules (RET) * Enforce ruff/flake8-slots rules (SLOT) * Add "numcodecs" to "src" The directories to consider when resolving first- vs. third-party imports. * Enforce ruff/flake8-simplify rules (SIM) * Enforce ruff/flake8-tidy-imports rules (TID) * Conflicting ruff lint rules The linter includes some rules that, when enabled, can cause conflicts with the formatter, leading to unexpected behavior. None None of these rules are included in Ruff's default configuration. However, since we have enabled some relevant rule sets, we disable these rules. * Enforce ruff/tryceratops rules (TRY) * Update ruff to 0.8.0 * Ignore ruff/flake8-simplify rule SIM105 SIM105 Use `contextlib.suppress(ImportError)` instead of `try`-`except`-`pass` * Ignore ruff/tryceratops rule TRY301 TRY301 Abstract `raise` to an inner function * Apply ruff/flake8-bugbear rule B904 B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling * Apply ruff/flake8-bugbear rule B028 B028 No explicit `stacklevel` keyword argument found * Apply ruff rule RUF022 RUF022 `__all__` is not sorted
1 parent bfa1c0d commit 9cd6bde

18 files changed

+74
-82
lines changed

adhoc/blosc_memleak_check.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import sys
22

3+
import numcodecs
34
import numpy as np
45
from numpy.testing import assert_array_equal
56

6-
import numcodecs
7-
87
codec = numcodecs.Blosc()
98
data = np.arange(int(sys.argv[1]))
109
for _ in range(int(sys.argv[2])):

notebooks/benchmark_vlen.ipynb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
}
2525
],
2626
"source": [
27-
"import numpy as np\n",
28-
"\n",
2927
"import numcodecs\n",
28+
"import numpy as np\n",
3029
"\n",
3130
"numcodecs.__version__"
3231
]

numcodecs/astype.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ def encode(self, buf):
4949
arr = ensure_ndarray(buf).view(self.decode_dtype)
5050

5151
# convert and copy
52-
enc = arr.astype(self.encode_dtype)
53-
54-
return enc
52+
return arr.astype(self.encode_dtype)
5553

5654
def decode(self, buf, out=None):
5755
# normalise input
@@ -61,9 +59,7 @@ def decode(self, buf, out=None):
6159
dec = enc.astype(self.decode_dtype)
6260

6361
# handle output
64-
out = ndarray_copy(dec, out)
65-
66-
return out
62+
return ndarray_copy(dec, out)
6763

6864
def get_config(self):
6965
return {

numcodecs/base64.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ def encode(self, buf):
1313
# normalise inputs
1414
buf = ensure_contiguous_ndarray(buf)
1515
# do compression
16-
compressed = _base64.standard_b64encode(buf)
17-
return compressed
16+
return _base64.standard_b64encode(buf)
1817

1918
def decode(self, buf, out=None):
2019
# normalise inputs

numcodecs/categorize.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,15 @@ def decode(self, buf, out=None):
8080
dec[enc == (i + 1)] = label
8181

8282
# handle output
83-
dec = ndarray_copy(dec, out)
84-
85-
return dec
83+
return ndarray_copy(dec, out)
8684

8785
def get_config(self):
88-
config = {
86+
return {
8987
'id': self.codec_id,
9088
'labels': self.labels,
9189
'dtype': self.dtype.str,
9290
'astype': self.astype.str,
9391
}
94-
return config
9592

9693
def __repr__(self):
9794
# make sure labels part is not too long

numcodecs/delta.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ def decode(self, buf, out=None):
8585
np.cumsum(enc, out=dec)
8686

8787
# handle output
88-
out = ndarray_copy(dec, out)
89-
90-
return out
88+
return ndarray_copy(dec, out)
9189

9290
def get_config(self):
9391
# override to handle encoding dtypes

numcodecs/fixedscaleoffset.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ def encode(self, buf):
9494
enc = np.around(enc)
9595

9696
# convert dtype
97-
enc = enc.astype(self.astype, copy=False)
98-
99-
return enc
97+
return enc.astype(self.astype, copy=False)
10098

10199
def decode(self, buf, out=None):
102100
# interpret buffer as numpy array

numcodecs/gzip.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def encode(self, buf):
2828
compressed = io.BytesIO()
2929
with _gzip.GzipFile(fileobj=compressed, mode='wb', compresslevel=self.level) as compressor:
3030
compressor.write(buf)
31-
compressed = compressed.getvalue()
32-
33-
return compressed
31+
return compressed.getvalue()
3432

3533
# noinspection PyMethodMayBeStatic
3634
def decode(self, buf, out=None):

numcodecs/lzma.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
try:
66
import lzma as _lzma
77
except ImportError: # pragma: no cover
8-
try:
8+
try: # noqa: SIM105
99
from backports import lzma as _lzma # type: ignore[no-redef]
1010
except ImportError:
1111
pass

numcodecs/ndarray_like.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, ClassVar, Optional, Protocol, runtime_checkable
1+
from typing import Any, ClassVar, Protocol, runtime_checkable
22

33

44
class _CachedProtocolMeta(Protocol.__class__): # type: ignore[name-defined]
@@ -53,7 +53,7 @@ def __getitem__(self, key) -> Any: ... # pragma: no cover
5353

5454
def __setitem__(self, key, value): ... # pragma: no cover
5555

56-
def tobytes(self, order: Optional[str] = ...) -> bytes: ... # pragma: no cover
56+
def tobytes(self, order: str | None = ...) -> bytes: ... # pragma: no cover
5757

5858
def reshape(self, *shape: int, order: str = ...) -> "NDArrayLike": ... # pragma: no cover
5959

0 commit comments

Comments
 (0)