Skip to content

Commit f58cdac

Browse files
authored
use field default-factory for optional dict fields (#63)
ensure that all fields with default values are marked as optional and for fields that default to empty-dict use a field-factory
1 parent cba6fc7 commit f58cdac

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121

2222
- correct chunk size estimate [\#59](https://github.com/mllam/mllam-data-prep/pull/59), @ealerskans
2323
- fix bug arising when variables provided to derived functions are renamed [\#56](https://github.com/mllam/mllam-data-prep/pull/56), @leifdenby
24+
- ensure config fields defaulting to `None` are typed as `Optional` and fields defaulting to `{}` are given a default-factory so that serialization with default values works correctly [\#63](https://github.com/mllam/mllam-data-prep/pull/63), @leifdenby
2425

2526
### Maintenance
2627

mllam_data_prep/config.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Range:
7474

7575
start: Union[str, int, float]
7676
end: Union[str, int, float]
77-
step: Union[str, int, float] = None
77+
step: Optional[Union[str, int, float]] = None
7878

7979

8080
@dataclass
@@ -93,7 +93,7 @@ class ValueSelection:
9393
"""
9494

9595
values: Union[List[Union[float, int]], Range]
96-
units: str = None
96+
units: Optional[str] = None
9797

9898

9999
@dataclass
@@ -177,8 +177,8 @@ class DimMapping:
177177
method: str
178178
dims: Optional[List[str]] = None
179179
dim: Optional[str] = None
180-
name_format: str = field(default=None)
181-
coord_ranges: Dict[str, Range] = None
180+
name_format: Optional[str] = field(default=None)
181+
coord_ranges: Optional[Dict[str, Range]] = field(default_factory=dict)
182182

183183

184184
@dataclass
@@ -275,7 +275,7 @@ class Split:
275275

276276
start: str
277277
end: str
278-
compute_statistics: Statistics = None
278+
compute_statistics: Optional[Statistics] = None
279279

280280

281281
@dataclass
@@ -334,9 +334,9 @@ class Output:
334334
"""
335335

336336
variables: Dict[str, List[str]]
337-
coord_ranges: Dict[str, Range] = None
337+
coord_ranges: Dict[str, Range] = field(default_factory=dict)
338338
chunking: Dict[str, int] = field(default_factory=dict)
339-
splitting: Splitting = None
339+
splitting: Optional[Splitting] = None
340340

341341

342342
@dataclass
@@ -373,7 +373,7 @@ class Config(dataclass_wizard.JSONWizard, dataclass_wizard.YAMLWizard):
373373
inputs: Dict[str, InputDataset]
374374
schema_version: str
375375
dataset_version: str
376-
extra: Dict[str, Any] = None
376+
extra: Dict[str, Any] = field(default_factory=dict)
377377

378378
def __post_init__(self):
379379
validate_config(self.inputs)

mllam_data_prep/create_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def create_dataset(config: Config):
128128
f" {', '.join(SUPPORTED_CONFIG_VERSIONS)} are supported by mllam-data-prep "
129129
f"v{__version__}."
130130
)
131-
if config.schema_version == "v0.2.0" and config.extra is not None:
131+
if config.schema_version == "v0.2.0" and config.extra:
132132
raise ValueError(
133133
"Config schema version v0.2.0 does not support the `extra` field. Please "
134134
"update the schema version used in your config to v0.5.0."

0 commit comments

Comments
 (0)