Skip to content

Commit

Permalink
fix(pydantic) : migrate field_validator to model_validator
Browse files Browse the repository at this point in the history
  • Loading branch information
regislon committed Oct 10, 2023
1 parent 36e5472 commit 50a0fe1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
43 changes: 24 additions & 19 deletions disdrodb/l0/check_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List, Optional, Union

import numpy as np
from pydantic import BaseModel, ValidationError, field_validator
from pydantic import BaseModel, ValidationError, field_validator, model_validator

from disdrodb.l0.standards import (
available_sensor_name,
Expand Down Expand Up @@ -128,25 +128,31 @@ class NetcdfEncodingSchema(BaseModel):
chunksizes: Optional[Union[int, List[int]]]

# if contiguous=False, chunksizes specified, otherwise should be not !
@field_validator("chunksizes")
def check_chunksizes(cls, v, values):
if not values.get("contiguous") and not v:
@model_validator(mode="before")
def check_chunksizes_and_zlib(cls, values):
contiguous = values.get("contiguous")
chunksizes = values.get("chunksizes")
if not contiguous and not chunksizes:
raise ValueError("'chunksizes' must be defined if 'contiguous' is False")
return v
return values

# if contiguous = True, then zlib must be set to False
@field_validator("zlib")
def check_zlib(cls, v, values):
if values.get("contiguous") and v:
@model_validator(mode="before")
def check_contiguous_and_zlib(cls, values):
contiguous = values.get("contiguous")
zlib = values.get("zlib")
if contiguous and zlib:
raise ValueError("'zlib' must be set to False if 'contiguous' is True")
return v
return values

# if contiguous = True, then fletcher32 must be set to False
@field_validator("fletcher32")
def check_fletcher32(cls, v, values):
if values.get("contiguous") and v:
@model_validator(mode="before")
def check_contiguous_and_fletcher32(cls, values):
contiguous = values.get("contiguous")
fletcher32 = values.get("fletcher32")
if contiguous and fletcher32:
raise ValueError("'fletcher32' must be set to False if 'contiguous' is True")
return v
return values


def check_l0b_encoding(sensor_name: str) -> None:
Expand Down Expand Up @@ -199,12 +205,12 @@ class RawDataFormatSchema(BaseModel):
n_decimals: Optional[int]
n_naturals: Optional[int]
data_range: Optional[List[float]]
nan_flags: Optional[str]
valid_values: Optional[List[float]]
dimension_order: Optional[List[str]]
n_values: Optional[int]
nan_flags: Optional[Union[int, str]] = None
valid_values: Optional[List[float]] = None
dimension_order: Optional[List[str]] = None
n_values: Optional[int] = None

@field_validator("data_range", pre=True)
@field_validator("data_range")
def check_list_length(cls, value):
if value:
if len(value) != 2:
Expand All @@ -224,7 +230,6 @@ def check_raw_data_format(sensor_name: str) -> None:

# check that the second level of the dictionary match the schema
for key, value in data.items():
print(key, value)
schema_error(
object_to_validate=value,
schema=RawDataFormatSchema,
Expand Down
6 changes: 2 additions & 4 deletions disdrodb/l0/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ def check_issue_file(fpath: str) -> None:

def _write_issue_docs(f):
"""Provide template for issue.yml"""
f.write(
"""# This file is used to store timesteps/time periods with wrong/corrupted observation.
f.write("""# This file is used to store timesteps/time periods with wrong/corrupted observation.
# The specified timesteps are dropped during the L0 processing.
# The time format used is the isoformat : YYYY-mm-dd HH:MM:SS.
# The 'timesteps' key enable to specify the list of timesteps to be discarded.
Expand All @@ -267,8 +266,7 @@ def _write_issue_docs(f):
# - ['2018-08-01 12:00:00', '2018-08-01 14:00:00']
# - ['2018-08-01 15:44:30', '2018-08-01 15:59:31']
# - ['2018-08-02 12:44:30', '2018-08-02 12:59:31'] \n
"""
)
""")
return None


Expand Down
4 changes: 2 additions & 2 deletions disdrodb/tests/test_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
import yaml
from pydantic import BaseModel, validator
from pydantic import BaseModel, field_validator


# Define the pydantic models for *.bins.yaml config files
Expand All @@ -13,7 +13,7 @@ class raw_data_format_2n_level(BaseModel):
n_decimals: Optional[int]
data_range: Optional[list]

@validator("data_range", pre=True)
@field_validator("data_range")
def check_list_length(cls, value):
if value:
if len(value) != 2:
Expand Down

0 comments on commit 50a0fe1

Please sign in to comment.