Skip to content

Commit a3dab6c

Browse files
jbuseckepre-commit-ci[bot]TomNicholas
authored
Bump Ruff version and add formatting (#98)
* Bump Ruff version and add formatting - [ ] Closes #96 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add config options to conform closer to black for linting * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update pyproject.toml * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tom Nicholas <tom@cworthy.org>
1 parent 3a0fa4c commit a3dab6c

File tree

13 files changed

+145
-101
lines changed

13 files changed

+145
-101
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ repos:
1010
- id: check-yaml
1111

1212
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
# Ruff version.
1314
rev: "v0.4.3"
1415
hooks:
16+
# Run the linter.
1517
- id: ruff
16-
args: ["--fix"]
17-
# - repo: https://github.com/Carreau/velin
18-
# rev: 0.0.8
19-
# hooks:
20-
# - id: velin
21-
# args: ["--write", "--compact"]
18+
args: [ --fix ]
19+
# Run the formatter.
20+
- id: ruff-format
21+
2222
- repo: https://github.com/pre-commit/mirrors-mypy
2323
rev: v1.10.0
2424
hooks:

pyproject.toml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,21 @@ datatree = ["py.typed"]
7575
files = "virtualizarr/**/*.py"
7676
show_error_codes = true
7777

78-
79-
80-
81-
8278
[tool.ruff]
83-
line-length = 100
79+
# Same as Black.
80+
line-length = 88
81+
indent-width = 4
8482
target-version = "py39"
8583

8684
exclude = [
8785
"docs",
8886
".eggs"]
8987

90-
9188
[tool.ruff.lint]
9289
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
9390
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
9491
# McCabe complexity (`C901`) by default.
95-
select = ["E4", "E7", "E9", "F"]
92+
select = ["E4", "E7", "E9", "F", "I"]
9693
per-file-ignores = {}
9794

9895
# E402: module level import not at top of file
@@ -101,7 +98,13 @@ per-file-ignores = {}
10198

10299
ignore = ["E402", "E731"]
103100

101+
# Allow fix for all enabled rules (when `--fix`) is provided.
102+
fixable = ["ALL"]
103+
unfixable = []
104+
104105
[tool.ruff.format]
106+
# Like Black, use double quotes for strings.
107+
quote-style = "double"
105108
# Indent with spaces, rather than tabs.
106109
indent-style = "space"
107110
# Respect magic trailing commas.

virtualizarr/kerchunk.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
from enum import Enum, auto
2222

23+
2324
class AutoName(Enum):
2425
# Recommended by official Python docs for auto naming:
2526
# https://docs.python.org/3/library/enum.html#using-automatic-values
2627
def _generate_next_value_(name, start, count, last_values):
2728
return name
2829

30+
2931
class FileType(AutoName):
3032
netcdf3 = auto()
3133
netcdf4 = auto()
@@ -34,6 +36,7 @@ class FileType(AutoName):
3436
fits = auto()
3537
zarr = auto()
3638

39+
3740
def read_kerchunk_references_from_file(
3841
filepath: str, filetype: Optional[FileType]
3942
) -> KerchunkStoreRefs:
@@ -57,6 +60,7 @@ def read_kerchunk_references_from_file(
5760

5861
if filetype.name.lower() == "netcdf3":
5962
from kerchunk.netCDF3 import NetCDF3ToZarr
63+
6064
refs = NetCDF3ToZarr(filepath, inline_threshold=0).translate()
6165

6266
elif filetype.name.lower() == "netcdf4":
@@ -87,7 +91,7 @@ def _automatically_determine_filetype(filepath: str) -> FileType:
8791

8892
if file_extension == ".nc":
8993
# based off of: https://github.com/TomNicholas/VirtualiZarr/pull/43#discussion_r1543415167
90-
with open(filepath, 'rb') as f:
94+
with open(filepath, "rb") as f:
9195
magic = f.read()
9296
if magic[0:3] == b"CDF":
9397
filetype = FileType.netcdf3

virtualizarr/manifests/manifest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ def from_zarr_json(cls, filepath: str) -> "ChunkManifest":
117117
with open(filepath, "r") as manifest_file:
118118
entries_dict = json.load(manifest_file)
119119

120-
entries = {cast(ChunkKey, k): ChunkEntry(**entry) for k, entry in entries_dict.items()}
120+
entries = {
121+
cast(ChunkKey, k): ChunkEntry(**entry) for k, entry in entries_dict.items()
122+
}
121123
return cls(entries=entries)
122124

123125
def to_zarr_json(self, filepath: str) -> None:

virtualizarr/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
import xarray as xr
33

4+
45
@pytest.fixture
56
def netcdf4_file(tmpdir):
67
# Set up example xarray dataset

virtualizarr/tests/test_kerchunk.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import numpy as np
2+
import pytest
23
import ujson # type: ignore
34
import xarray as xr
45
import xarray.testing as xrt
5-
import pytest
6-
76

8-
from virtualizarr.kerchunk import _automatically_determine_filetype, FileType
7+
from virtualizarr.kerchunk import FileType, _automatically_determine_filetype
98
from virtualizarr.manifests import ChunkEntry, ChunkManifest, ManifestArray
109
from virtualizarr.xarray import dataset_from_kerchunk_refs
1110

11+
1212
def gen_ds_refs(
13-
zgroup: str = '{"zarr_format":2}',
14-
zarray: str = '{"chunks":[2,3],"compressor":null,"dtype":"<i8","fill_value":null,"filters":null,"order":"C","shape":[2,3],"zarr_format":2}',
15-
zattrs: str = '{"_ARRAY_DIMENSIONS":["x","y"]}',
16-
chunk: list = ["test1.nc", 6144, 48],
13+
zgroup: str = '{"zarr_format":2}',
14+
zarray: str = '{"chunks":[2,3],"compressor":null,"dtype":"<i8","fill_value":null,"filters":null,"order":"C","shape":[2,3],"zarr_format":2}',
15+
zattrs: str = '{"_ARRAY_DIMENSIONS":["x","y"]}',
16+
chunk: list = ["test1.nc", 6144, 48],
1717
):
1818
return {
1919
"version": 1,
@@ -25,9 +25,10 @@ def gen_ds_refs(
2525
},
2626
}
2727

28+
2829
def test_dataset_from_df_refs():
2930
ds_refs = gen_ds_refs()
30-
ds = dataset_from_kerchunk_refs(ds_refs)
31+
ds = dataset_from_kerchunk_refs(ds_refs)
3132
assert "a" in ds
3233
da = ds["a"]
3334
assert isinstance(da.data, ManifestArray)
@@ -45,11 +46,21 @@ def test_dataset_from_df_refs():
4546
"0.0": {"path": "test1.nc", "offset": 6144, "length": 48}
4647
}
4748

49+
4850
def test_dataset_from_df_refs_with_filters():
49-
filters = [{"elementsize":4,"id":"shuffle"},{"id":"zlib","level":4}]
50-
zarray = {"chunks":[2,3],"compressor":None,"dtype":"<i8","fill_value":None,"filters":filters,"order":"C","shape":[2,3],"zarr_format":2}
51+
filters = [{"elementsize": 4, "id": "shuffle"}, {"id": "zlib", "level": 4}]
52+
zarray = {
53+
"chunks": [2, 3],
54+
"compressor": None,
55+
"dtype": "<i8",
56+
"fill_value": None,
57+
"filters": filters,
58+
"order": "C",
59+
"shape": [2, 3],
60+
"zarr_format": 2,
61+
}
5162
ds_refs = gen_ds_refs(zarray=ujson.dumps(zarray))
52-
ds = dataset_from_kerchunk_refs(ds_refs)
63+
ds = dataset_from_kerchunk_refs(ds_refs)
5364
da = ds["a"]
5465
assert da.data.zarray.filters == filters
5566

@@ -163,15 +174,13 @@ def test_automatically_determine_filetype_netcdf3_netcdf4():
163174
assert FileType("netcdf4") == _automatically_determine_filetype(netcdf4_file_path)
164175

165176

166-
167-
168177
def test_FileType():
169178
# tests if FileType converts user supplied strings to correct filetype
170-
assert 'netcdf3' == FileType("netcdf3").name
171-
assert 'netcdf4' == FileType("netcdf4").name
172-
assert 'grib' == FileType("grib").name
173-
assert 'tiff' == FileType("tiff").name
174-
assert 'fits' == FileType("fits").name
175-
assert 'zarr' == FileType("zarr").name
179+
assert "netcdf3" == FileType("netcdf3").name
180+
assert "netcdf4" == FileType("netcdf4").name
181+
assert "grib" == FileType("grib").name
182+
assert "tiff" == FileType("tiff").name
183+
assert "fits" == FileType("fits").name
184+
assert "zarr" == FileType("zarr").name
176185
with pytest.raises(ValueError):
177186
FileType(None)

virtualizarr/tests/test_manifests/test_array.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ def test_not_equal_chunk_entries(self):
130130
assert not (marr1 == marr2).all()
131131

132132
@pytest.mark.skip(reason="Not Implemented")
133-
def test_partly_equals(self):
134-
...
133+
def test_partly_equals(self): ...
135134

136135

137136
# TODO we really need some kind of fixtures to generate useful example data

virtualizarr/tests/test_manifests/test_manifest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ def test_stack(self):
156156

157157
@pytest.mark.skip(reason="Not implemented")
158158
class TestSerializeManifest:
159-
def test_serialize_manifest_to_zarr(self):
160-
...
159+
def test_serialize_manifest_to_zarr(self): ...
161160

162-
def test_deserialize_manifest_from_zarr(self):
163-
...
161+
def test_deserialize_manifest_from_zarr(self): ...

virtualizarr/tests/test_xarray.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,6 @@ def test_concat_dim_coords_along_existing_dim(self):
225225
assert result.data.zarray.zarr_format == zarray.zarr_format
226226

227227

228-
229-
230-
231228
class TestOpenVirtualDatasetIndexes:
232229
def test_no_indexes(self, netcdf4_file):
233230
vds = open_virtual_dataset(netcdf4_file, indexes={})
@@ -273,7 +270,7 @@ def test_combine_by_coords(self, netcdf4_files):
273270

274271
class TestLoadVirtualDataset:
275272
def test_loadable_variables(self, netcdf4_file):
276-
vars_to_load = ['air', 'time']
273+
vars_to_load = ["air", "time"]
277274
vds = open_virtual_dataset(netcdf4_file, loadable_variables=vars_to_load)
278275

279276
for name in vds.variables:

virtualizarr/tests/test_zarr.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
import xarray as xr
21
import numpy as np
2+
import xarray as xr
33
import xarray.testing as xrt
4-
from virtualizarr import open_virtual_dataset, ManifestArray
4+
5+
from virtualizarr import ManifestArray, open_virtual_dataset
56
from virtualizarr.manifests.manifest import ChunkEntry
67

78

89
def test_zarr_v3_roundtrip(tmpdir):
910
arr = ManifestArray(
10-
chunkmanifest={"0.0": ChunkEntry(path="test.nc", offset=6144, length=48)},
11-
zarray=dict(
12-
shape=(2, 3),
13-
dtype=np.dtype("<i8"),
14-
chunks=(2, 3),
15-
compressor=None,
16-
filters=None,
17-
fill_value=None,
18-
order="C",
19-
zarr_format=3,
20-
),
21-
)
11+
chunkmanifest={"0.0": ChunkEntry(path="test.nc", offset=6144, length=48)},
12+
zarray=dict(
13+
shape=(2, 3),
14+
dtype=np.dtype("<i8"),
15+
chunks=(2, 3),
16+
compressor=None,
17+
filters=None,
18+
fill_value=None,
19+
order="C",
20+
zarr_format=3,
21+
),
22+
)
2223
original = xr.Dataset({"a": (["x", "y"], arr)}, attrs={"something": 0})
2324

2425
original.virtualize.to_zarr(tmpdir / "store.zarr")
25-
roundtrip = open_virtual_dataset(tmpdir / "store.zarr", filetype="zarr_v3", indexes={})
26+
roundtrip = open_virtual_dataset(
27+
tmpdir / "store.zarr", filetype="zarr_v3", indexes={}
28+
)
2629

2730
xrt.assert_identical(roundtrip, original)

0 commit comments

Comments
 (0)