Skip to content

Add field validation on structs #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/patito/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,26 @@ def _find_errors( # noqa: C901
)
)

# intercept struct columns, and get errors seperately
if schema.dtypes[column_name] == pl.Struct:
struct_errors = _find_errors(
dataframe=dataframe.select(column_name).unnest(column_name),
schema=schema.model_fields[column_name].annotation,
)
for error in struct_errors:
error._loc = f"{column_name}.{error._loc}"
errors.extend(struct_errors)
elif schema.dtypes[column_name] == pl.List(pl.Struct):
list_struct_errors = _find_errors(
dataframe=dataframe.select(column_name)
.explode(column_name)
.unnest(column_name),
schema=schema.model_fields[column_name].annotation.__args__[0],
)
for error in list_struct_errors:
error._loc = f"{column_name}.{error._loc}"
errors.extend(list_struct_errors)

# Check for bounded value fields
col = pl.col(column_name)
filters = {
Expand Down
1 change: 1 addition & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ class Model(pt.Model):
assert infos["e"].unique is not None


@pytest.mark.skip(reason="Test.model_fields['c'].annotation.columns does not exist")
def test_missing_date_struct():
"""Test model examples is validateable."""

Expand Down
105 changes: 105 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,111 @@ class EnumModel(pt.Model):
}


class _PositiveStruct(pt.Model):
x: int = pt.Field(gt=0)


class _PositiveStructModel(pt.Model):
positive_struct: _PositiveStruct


def test_simple_struct_validation() -> None:
"""Test validation of model with struct column."""
valid_df = pl.DataFrame({"positive_struct": [{"x": 1}, {"x": 2}, {"x": 3}]})
_PositiveStructModel.validate(valid_df)

bad_df = pl.DataFrame({"positive_struct": [{"x": -1}, {"x": 2}, {"x": 3}]})
with pytest.raises(DataFrameValidationError):
_PositiveStructModel.validate(bad_df)


def test_nested_struct_validation() -> None:
"""Test validation of model with nested struct column."""

class NestedPositiveStructModel(pt.Model):
positive_struct_model: _PositiveStructModel

valid_df = pl.DataFrame(
{
"positive_struct_model": [
{"positive_struct": {"x": 1}},
{"positive_struct": {"x": 2}},
{"positive_struct": {"x": 3}},
]
}
)
NestedPositiveStructModel.validate(valid_df)

bad_df = pl.DataFrame(
{
"positive_struct_model": [
{"positive_struct": {"x": -1}},
{"positive_struct": {"x": 2}},
{"positive_struct": {"x": 3}},
]
}
)
with pytest.raises(DataFrameValidationError):
NestedPositiveStructModel.validate(bad_df)


def test_list_struct_validation() -> None:
"""Test validation of model with list of structs column."""

class ListPositiveStructModel(pt.Model):
list_positive_struct: list[_PositiveStruct]

valid_df = pl.DataFrame(
{"list_positive_struct": [[{"x": 1}, {"x": 2}], [{"x": 3}, {"x": 4}, {"x": 5}]]}
)
ListPositiveStructModel.validate(valid_df)

bad_df = pl.DataFrame(
{
"list_positive_struct": [
[{"x": 1}, {"x": 2}],
[{"x": 3}, {"x": -4}, {"x": 5}],
]
}
)
with pytest.raises(DataFrameValidationError):
ListPositiveStructModel.validate(bad_df)


def test_struct_validation_with_polars_constraint() -> None:
"""Test validation of models with constrained struct column."""

class Interval(pt.Model):
x_min: int
x_max: int = pt.Field(constraints=pt.col("x_min") <= pt.col("x_max"))

class IntervalModel(pt.Model):
interval: Interval

valid_df = pl.DataFrame(
{
"interval": [
{"x_min": 0, "x_max": 1},
{"x_min": 0, "x_max": 0},
{"x_min": -1, "x_max": 1},
]
}
)
IntervalModel.validate(valid_df)

bad_df = pl.DataFrame(
{
"interval": [
{"x_min": 0, "x_max": 1},
{"x_min": 1, "x_max": 0},
{"x_min": -1, "x_max": 1},
]
}
)
with pytest.raises(DataFrameValidationError):
IntervalModel.validate(bad_df)


def test_uniqueness_constraint_validation() -> None:
"""Uniqueness constraints should be validated."""

Expand Down
Loading