Skip to content

Commit ca46bab

Browse files
Add more ruff rules (#2460)
* Enforce ruff/flake8-executable rules (EXE) * Enforce ruff/flake8-future-annotations rules (FA) * Enforce ruff/flake8-logging rules (LOG) * Apply ruff/flake8-pie rule PIE790 PIE790 Unnecessary `pass` statement PIE790 Unnecessary `...` literal * Apply ruff/flake8-pie rule PIE800 PIE800 Unnecessary spread `**` * Apply ruff/flake8-pie rule PIE808 PIE808 Unnecessary `start` argument in `range` * Enforce ruff/flake8-pie rules (PIE) * Apply ruff preview rule RUF022 RUF022 `__all__` is not sorted * Sort pyproject.toml * Apply ruff/flake8-simplify rule SIM102 SIM102 Use a single `if` statement instead of nested `if` statements * Apply ruff/flake8-simplify rule SIM114 SIM114 Combine `if` branches using logical `or` operator * Apply ruff/flake8-simplify rule SIM118 SIM118 Use `key in dict` instead of `key in dict.keys()` * Enforce ruff/flake8-simplify rules (SIM) * Enforce ruff/flake8-slots rules (SLOT)
1 parent d1075de commit ca46bab

File tree

14 files changed

+84
-107
lines changed

14 files changed

+84
-107
lines changed

pyproject.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,25 @@ extend-exclude = [
269269
extend-select = [
270270
"ANN", # flake8-annotations
271271
"B", # flake8-bugbear
272+
"EXE", # flake8-executable
272273
"C4", # flake8-comprehensions
274+
"FA", # flake8-future-annotations
273275
"FLY", # flynt
274276
"FURB", # refurb
275277
"G", # flake8-logging-format
276278
"I", # isort
277279
"ISC", # flake8-implicit-str-concat
280+
"LOG", # flake8-logging
278281
"PERF", # Perflint
282+
"PIE", # flake8-pie
279283
"PGH", # pygrep-hooks
280284
"PT", # flake8-pytest-style
281285
"PYI", # flake8-pyi
282-
"RSE", # flake8-raise
283286
"RET", # flake8-return
287+
"RSE", # flake8-raise
284288
"RUF",
289+
"SIM", # flake8-simplify
290+
"SLOT", # flake8-slots
285291
"TCH", # flake8-type-checking
286292
"TRY", # tryceratops
287293
"UP", # pyupgrade
@@ -298,6 +304,7 @@ ignore = [
298304
"RET505",
299305
"RET506",
300306
"RUF005",
307+
"SIM108",
301308
"TRY003",
302309
"UP027", # deprecated
303310
"UP038", # https://github.com/astral-sh/ruff/issues/7871
@@ -319,7 +326,7 @@ ignore = [
319326
]
320327

321328
[tool.ruff.lint.extend-per-file-ignores]
322-
"tests/**" = ["ANN001", "ANN201"]
329+
"tests/**" = ["ANN001", "ANN201", "RUF029", "SIM117", "SIM300"]
323330

324331
[tool.mypy]
325332
python_version = "3.11"

src/zarr/abc/codec.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def validate(self, *, shape: ChunkCoords, dtype: np.dtype[Any], chunk_grid: Chun
106106
chunk_grid : ChunkGrid
107107
The array chunk grid
108108
"""
109-
...
110109

111110
async def _decode_single(self, chunk_data: CodecOutput, chunk_spec: ArraySpec) -> CodecInput:
112111
raise NotImplementedError

src/zarr/abc/metadata.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,5 @@ def from_dict(cls, data: dict[str, JSON]) -> Self:
4242
"""
4343
Create an instance of the model from a dictionary
4444
"""
45-
...
4645

4746
return cls(**data)

src/zarr/api/asynchronous.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,8 @@ async def create(
878878
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)
879879

880880
mode = kwargs.pop("mode", None)
881-
if mode is None:
882-
if not isinstance(store, Store | StorePath):
883-
mode = "a"
881+
if mode is None and not isinstance(store, Store | StorePath):
882+
mode = "a"
884883

885884
store_path = await make_store_path(store, path=path, mode=mode, storage_options=storage_options)
886885

src/zarr/codecs/gzip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
def parse_gzip_level(data: JSON) -> int:
2222
if not isinstance(data, (int)):
2323
raise TypeError(f"Expected int, got {type(data)}")
24-
if data not in range(0, 10):
24+
if data not in range(10):
2525
raise ValueError(
2626
f"Expected an integer from the inclusive range (0, 9). Got {data} instead."
2727
)

src/zarr/core/chunk_grids.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def to_dict(self) -> dict[str, JSON]:
182182

183183
def all_chunk_coords(self, array_shape: ChunkCoords) -> Iterator[ChunkCoords]:
184184
return itertools.product(
185-
*(range(0, ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape, strict=False))
185+
*(range(ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape, strict=False))
186186
)
187187

188188
def get_nchunks(self, array_shape: ChunkCoords) -> int:

src/zarr/core/group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ def _members_consolidated(
12251225

12261226
# we kind of just want the top-level keys.
12271227
if consolidated_metadata is not None:
1228-
for key in consolidated_metadata.metadata.keys():
1228+
for key in consolidated_metadata.metadata:
12291229
obj = self._getitem_consolidated(
12301230
self.store_path, key, prefix=self.name
12311231
) # Metadata -> Group/Array

src/zarr/core/indexing.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,13 @@ def is_pure_fancy_indexing(selection: Any, ndim: int) -> bool:
241241
# is mask selection
242242
return True
243243

244-
if ndim == 1:
245-
if is_integer_list(selection) or is_integer_array(selection) or is_bool_list(selection):
246-
return True
244+
if ndim == 1 and (
245+
is_integer_list(selection) or is_integer_array(selection) or is_bool_list(selection)
246+
):
247+
return True
247248

248-
# if not, we go through the normal path below, because a 1-tuple
249-
# of integers is also allowed.
249+
# if not, we go through the normal path below, because a 1-tuple
250+
# of integers is also allowed.
250251
no_slicing = (
251252
isinstance(selection, tuple)
252253
and len(selection) == ndim

src/zarr/core/metadata/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
T_ArrayMetadata = TypeVar("T_ArrayMetadata", ArrayV2Metadata, ArrayV3Metadata)
99

1010
__all__ = [
11-
"ArrayV2Metadata",
12-
"ArrayV3Metadata",
1311
"ArrayMetadata",
1412
"ArrayMetadataDict",
15-
"ArrayV3MetadataDict",
13+
"ArrayV2Metadata",
1614
"ArrayV2MetadataDict",
15+
"ArrayV3Metadata",
16+
"ArrayV3MetadataDict",
1717
]

src/zarr/core/metadata/v3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ def parse_fill_value(
481481
except (ValueError, OverflowError, TypeError) as e:
482482
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}") from e
483483
# Check if the value is still representable by the dtype
484-
if fill_value == "NaN" and np.isnan(casted_value):
485-
pass
486-
elif fill_value in ["Infinity", "-Infinity"] and not np.isfinite(casted_value):
484+
if (fill_value == "NaN" and np.isnan(casted_value)) or (
485+
fill_value in ["Infinity", "-Infinity"] and not np.isfinite(casted_value)
486+
):
487487
pass
488488
elif np_dtype.kind == "f":
489489
# float comparison is not exact, especially when dtype <float64

src/zarr/core/strings.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ def cast_to_string_dtype(
6161
return cast_array(data)
6262
# out = data.astype(STRING_DTYPE, copy=False)
6363
# return cast(np.ndarray[Any, np.dtypes.StringDType | np.dtypes.ObjectDType], out)
64-
if _NUMPY_SUPPORTS_VLEN_STRING:
65-
if np.issubdtype(data.dtype, _STRING_DTYPE):
66-
# already a valid string variable length string dtype
67-
return cast_array(data)
64+
if _NUMPY_SUPPORTS_VLEN_STRING and np.issubdtype(data.dtype, _STRING_DTYPE):
65+
# already a valid string variable length string dtype
66+
return cast_array(data)
6867
if np.issubdtype(data.dtype, np.object_):
6968
# object arrays require more careful handling
7069
if _NUMPY_SUPPORTS_VLEN_STRING:

tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_save(store: Store, n_args: int, n_kwargs: int) -> None:
154154
assert isinstance(group, Group)
155155
for array in group.array_values():
156156
assert_array_equal(array[:], data)
157-
for k in kwargs.keys():
157+
for k in kwargs:
158158
assert k in group
159159
assert group.nmembers() == n_args + n_kwargs
160160

tests/test_codecs/test_codecs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def test_sharding_pickle() -> None:
5656
"""
5757
Test that sharding codecs can be pickled
5858
"""
59-
pass
6059

6160

6261
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])

0 commit comments

Comments
 (0)