Skip to content

Commit 3c9f0ec

Browse files
committed
Linting
1 parent a70f63c commit 3c9f0ec

File tree

7 files changed

+111
-71
lines changed

7 files changed

+111
-71
lines changed

src/mdio/core/v1/_overloads.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
2. To simplify the API for users where it makes sense (e.g. MDIO v1 uses Zarr and not HDF5).
66
"""
77

8+
from collections.abc import Mapping
9+
810
import xarray as xr
911
from xarray import DataArray as _DataArray
1012
from xarray import Dataset as _Dataset
@@ -15,7 +17,12 @@ class MDIODataset(_Dataset):
1517

1618
__slots__ = ()
1719

18-
def to_mdio(self, store=None, *args, **kwargs):
20+
def to_mdio(
21+
self,
22+
store: str | None = None,
23+
*args: str | int | float | bool,
24+
**kwargs: Mapping[str, str | int | float | bool],
25+
) -> None:
1926
"""Alias for `.to_zarr()`, prints a greeting, and writes to Zarr store."""
2027
print("👋 hello world from mdio.to_mdio!")
2128
return super().to_zarr(*args, store=store, **kwargs)
@@ -26,7 +33,12 @@ class MDIODataArray(_DataArray):
2633

2734
__slots__ = ()
2835

29-
def to_mdio(self, store=None, *args, **kwargs):
36+
def to_mdio(
37+
self,
38+
store: str | None = None,
39+
*args: str | int | float | bool,
40+
**kwargs: Mapping[str, str | int | float | bool],
41+
) -> None:
3042
"""Alias for `.to_zarr()`, prints a greeting, and writes to Zarr store."""
3143
print("👋 hello world from mdio.to_mdio!")
3244
return super().to_zarr(*args, store=store, **kwargs)
@@ -39,7 +51,13 @@ class MDIO:
3951
DataArray = MDIODataArray
4052

4153
@staticmethod
42-
def open(store, *args, engine="zarr", consolidated=False, **kwargs):
54+
def open(
55+
store: str,
56+
*args: str | int | float | bool,
57+
engine: str = "zarr",
58+
consolidated: bool = False,
59+
**kwargs: Mapping[str, str | int | float | bool],
60+
) -> MDIODataset:
4361
"""Open a Zarr store as an MDIODataset.
4462
4563
Casts the returned xarray.Dataset (and its variables) to the MDIO subclasses.
@@ -55,9 +73,9 @@ def open(store, *args, engine="zarr", consolidated=False, **kwargs):
5573
# Cast Dataset to MDIODataset
5674
ds.__class__ = MDIODataset
5775
# Cast each DataArray in data_vars and coords
58-
for _name, var in ds.data_vars.items():
76+
for _name, var in ds.data_vars.values():
5977
var.__class__ = MDIODataArray
60-
for _name, coord in ds.coords.items():
78+
for _name, coord in ds.coords.values():
6179
coord.__class__ = MDIODataArray
6280
return ds
6381

src/mdio/core/v1/_serializer.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def make_coordinate(
5555
)
5656

5757

58-
def make_variable( # noqa: C901
58+
def make_variable( # noqa: PLR0913 PLR0912
5959
name: str,
6060
dimensions: list[NamedDimension | str],
6161
data_type: ScalarType | StructuredType,
@@ -118,7 +118,8 @@ def make_variable( # noqa: C901
118118
elif isinstance(metadata, VariableMetadata):
119119
var_metadata = metadata
120120
else:
121-
raise TypeError(f"Unsupported metadata type: {type(metadata)}")
121+
msg = f"Unsupported metadata type: {type(metadata)}"
122+
raise TypeError(msg)
122123

123124
# Create the variable with all attributes explicitly set
124125
return Variable(
@@ -170,7 +171,8 @@ def _convert_compressor(
170171
)
171172
if isinstance(model, ZFP):
172173
if zfpy_base is None or ZFPY is None:
173-
raise ImportError("zfpy and numcodecs are required to use ZFP compression")
174+
msg = "zfpy and numcodecs are required to use ZFP compression"
175+
raise ImportError(msg)
174176
return ZFPY(
175177
mode=model.mode.value,
176178
tolerance=model.tolerance,
@@ -179,10 +181,11 @@ def _convert_compressor(
179181
)
180182
if model is None:
181183
return None
182-
raise TypeError(f"Unsupported compressor model: {type(model)}")
184+
msg = f"Unsupported compressor model: {type(model)}"
185+
raise TypeError(msg)
183186

184187

185-
def _construct_mdio_dataset(mdio_ds: MDIODataset) -> mdio.Dataset: # noqa: C901
188+
def _construct_mdio_dataset(mdio_ds: MDIODataset) -> mdio.Dataset: # noqa: PLR0912
186189
"""Build an MDIO dataset with correct dimensions and dtypes.
187190
188191
This internal function constructs the underlying data structure for an MDIO dataset,
@@ -215,7 +218,8 @@ def _construct_mdio_dataset(mdio_ds: MDIODataset) -> mdio.Dataset: # noqa: C901
215218
elif isinstance(dt, StructuredType):
216219
dtype = np.dtype([(f.name, f.format.value) for f in dt.fields])
217220
else:
218-
raise TypeError(f"Unsupported data_type: {dt}")
221+
msg = f"Unsupported data_type: {dt}"
222+
raise TypeError(msg)
219223
arr = np.zeros(shape, dtype=dtype)
220224
data_array = mdio.DataArray(arr, dims=dim_names)
221225
data_array.encoding["fill_value"] = 0.0

src/mdio/core/v1/builder.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Builder pattern implementation for MDIO v1 schema models."""
22

3+
from collections.abc import Mapping
34
from datetime import UTC
45
from datetime import datetime
56
from enum import Enum
@@ -53,7 +54,7 @@ class MDIODatasetBuilder:
5354

5455
def __init__(self, name: str, attributes: dict[str, Any] | None = None):
5556
self.name = name
56-
self.api_version = "1.0.0" # TODO: Pull from package metadata
57+
self.api_version = "1.0.0" # TODO(BrianMichell, #0): Pull from package metadata
5758
self.created_on = datetime.now(UTC)
5859
self.attributes = attributes
5960
self._dimensions: list[NamedDimension] = []
@@ -62,7 +63,7 @@ def __init__(self, name: str, attributes: dict[str, Any] | None = None):
6263
self._state = _BuilderState.INITIAL
6364
self._unnamed_variable_counter = 0
6465

65-
def add_dimension(
66+
def add_dimension( # noqa: PLR0913
6667
self,
6768
name: str,
6869
size: int,
@@ -101,7 +102,7 @@ def add_dimension(
101102
self._state = _BuilderState.HAS_DIMENSIONS
102103
return self
103104

104-
def add_coordinate(
105+
def add_coordinate( # noqa: PLR0913
105106
self,
106107
name: str = "",
107108
*,
@@ -112,7 +113,8 @@ def add_coordinate(
112113
) -> "MDIODatasetBuilder":
113114
"""Add a coordinate after adding at least one dimension."""
114115
if self._state == _BuilderState.INITIAL:
115-
raise ValueError("Must add at least one dimension before adding coordinates")
116+
msg = "Must add at least one dimension before adding coordinates"
117+
raise ValueError(msg)
116118

117119
if name == "":
118120
name = f"coord_{len(self._coordinates)}"
@@ -127,7 +129,8 @@ def add_coordinate(
127129
if isinstance(dim, str):
128130
dim_obj = next((d for d in self._dimensions if d.name == dim), None)
129131
if dim_obj is None:
130-
raise ValueError(f"Dimension {dim!r} not found")
132+
msg = f"Dimension {dim!r} not found"
133+
raise ValueError(msg)
131134
dim_objects.append(dim_obj)
132135
else:
133136
dim_objects.append(dim)
@@ -144,7 +147,7 @@ def add_coordinate(
144147
self._state = _BuilderState.HAS_COORDINATES
145148
return self
146149

147-
def add_variable(
150+
def add_variable( # noqa: PLR0913
148151
self,
149152
name: str = "",
150153
*,
@@ -157,7 +160,8 @@ def add_variable(
157160
) -> "MDIODatasetBuilder":
158161
"""Add a variable after adding at least one dimension."""
159162
if self._state == _BuilderState.INITIAL:
160-
raise ValueError("Must add at least one dimension before adding variables")
163+
msg = "Must add at least one dimension before adding variables"
164+
raise ValueError(msg)
161165

162166
if name == "":
163167
name = f"var_{self._unnamed_variable_counter}"
@@ -171,7 +175,8 @@ def add_variable(
171175
if isinstance(dim, str):
172176
dim_obj = next((d for d in self._dimensions if d.name == dim), None)
173177
if dim_obj is None:
174-
raise ValueError(f"Dimension {dim!r} not found")
178+
msg = f"Dimension {dim!r} not found"
179+
raise ValueError(msg)
175180
dim_objects.append(dim_obj)
176181
else:
177182
dim_objects.append(dim)
@@ -193,7 +198,8 @@ def add_variable(
193198
def build(self) -> Dataset:
194199
"""Build the final dataset."""
195200
if self._state == _BuilderState.INITIAL:
196-
raise ValueError("Must add at least one dimension before building")
201+
msg = "Must add at least one dimension before building"
202+
raise ValueError(msg)
197203

198204
metadata = make_dataset_metadata(
199205
self.name, self.api_version, self.created_on, self.attributes
@@ -216,7 +222,11 @@ def build(self) -> Dataset:
216222
return make_dataset(all_variables, metadata)
217223

218224

219-
def write_mdio_metadata(mdio_ds: Dataset, store: str, **kwargs: Any) -> mdio.Dataset:
225+
def write_mdio_metadata(
226+
mdio_ds: Dataset,
227+
store: str,
228+
**kwargs: Mapping[str, str | int | float | bool],
229+
) -> mdio.Dataset:
220230
"""Write MDIO metadata to a Zarr store and return the constructed mdio.Dataset.
221231
222232
This function constructs an mdio.Dataset from the MDIO dataset and writes its metadata
@@ -238,7 +248,7 @@ def _generate_encodings() -> dict:
238248
Returns:
239249
Dictionary mapping variable names to their encoding configurations.
240250
"""
241-
# TODO: Re-enable chunk_key_encoding when supported by xarray
251+
# TODO(Anybody, #10274): Re-enable chunk_key_encoding when supported by xarray
242252
# dimension_separator_encoding = V2ChunkKeyEncoding(separator="/").to_dict()
243253
global_encodings = {}
244254
for var in mdio_ds.variables:
@@ -250,7 +260,7 @@ def _generate_encodings() -> dict:
250260
chunks = var.metadata.chunk_grid.configuration.chunk_shape
251261
global_encodings[var.name] = {
252262
"chunks": chunks,
253-
# TODO: Re-enable chunk_key_encoding when supported by xarray
263+
# TODO(Anybody, #10274): Re-enable chunk_key_encoding when supported by xarray
254264
# "chunk_key_encoding": dimension_separator_encoding,
255265
"_FillValue": fill_value,
256266
"dtype": var.data_type,

src/mdio/core/v1/factory.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
"""MDIO factories for seismic data."""
22

3-
# TODO(BrianMichell): Add implementations for other canonical datasets.
3+
# TODO(BrianMichell, #535): Add implementations for other canonical datasets.
44

55
from __future__ import annotations
66

77
from enum import Enum
88
from enum import auto
9+
from typing import TYPE_CHECKING
910
from typing import Any
1011

1112
from mdio.core.v1.builder import MDIODatasetBuilder
1213
from mdio.schemas.compressors import Blosc
1314
from mdio.schemas.dtype import ScalarType
1415
from mdio.schemas.dtype import StructuredType
15-
from mdio.schemas.v1.dataset import Dataset
16+
17+
if TYPE_CHECKING:
18+
from mdio.schemas.v1.dataset import Dataset
1619

1720

1821
class MDIOSchemaType(Enum):
@@ -28,15 +31,15 @@ class MDIOSchemaType(Enum):
2831
class Seismic3DPostStackGeneric:
2932
"""Generic 3D seismic post stack dataset."""
3033

31-
def __init__(self):
34+
def __init__(self) -> None:
3235
self._dim_names = ["inline", "crossline", "sample"]
3336
self._chunks = [128, 128, 128] # 8 mb
3437
self._coords = {
3538
"cdp-x": ("float64", {"unitsV1": {"length": "m"}}, self._dim_names[:-1]),
3639
"cdp-y": ("float64", {"unitsV1": {"length": "m"}}, self._dim_names[:-1]),
3740
}
3841

39-
def create(
42+
def create( # noqa: PLR0913
4043
self,
4144
name: str,
4245
shape: list[int],
@@ -128,11 +131,11 @@ def create(
128131
class Seismic3DPostStack(Seismic3DPostStackGeneric):
129132
"""3D seismic post stack dataset with domain-specific attributes."""
130133

131-
def __init__(self, domain: str):
134+
def __init__(self, domain: str) -> None:
132135
super().__init__()
133136
self._dim_names = ["inline", "crossline", domain]
134137

135-
def create(
138+
def create( # noqa: PLR0913
136139
self,
137140
name: str,
138141
shape: list[int],
@@ -170,7 +173,7 @@ def create(
170173
class Seismic3DPreStack(Seismic3DPostStackGeneric):
171174
"""3D seismic pre stack dataset."""
172175

173-
def __init__(self, domain: str):
176+
def __init__(self, domain: str) -> None:
174177
super().__init__()
175178
self._dim_names = ["inline", "crossline", "offset", domain]
176179
self._chunks = [1, 1, 512, 4096] # 8 mb
@@ -179,7 +182,7 @@ def __init__(self, domain: str):
179182
"cdp-y": ("float64", {"length": "m"}, self._dim_names[:-2]),
180183
}
181184

182-
def create(
185+
def create( # noqa: PLR0913
183186
self,
184187
name: str,
185188
shape: list[int],

tests/integration/test_v1_serialization.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Integration test for MDIO v1 Xarray Zarr constructor."""
22

33
from datetime import datetime
4+
from pathlib import Path
45

56
import numpy as np
67

8+
from mdio.core.v1._overloads import MDIODataset
79
from mdio.core.v1._serializer import make_dataset
810
from mdio.core.v1._serializer import make_dataset_metadata
911
from mdio.core.v1._serializer import make_named_dimension
@@ -15,7 +17,7 @@
1517
from mdio.schemas.dtype import StructuredType
1618

1719

18-
def build_toy_dataset():
20+
def build_toy_dataset() -> MDIODataset:
1921
"""Build a toy dataset for testing."""
2022
# core dimensions
2123
inline = make_named_dimension("inline", 256)
@@ -165,7 +167,7 @@ def build_toy_dataset():
165167
)
166168

167169

168-
def test_to_mdio_writes_and_returns_mdio(tmp_path):
170+
def test_to_mdio_writes_and_returns_mdio(tmp_path: Path) -> None:
169171
"""Test that to_mdio writes and returns an mdio.Dataset."""
170172
ds_in = build_toy_dataset()
171173
store_path = tmp_path / "toy.mdio"

0 commit comments

Comments
 (0)