Skip to content

Commit

Permalink
Merge pull request #126 from adrienDog/fix/empty-lists-should-not-try…
Browse files Browse the repository at this point in the history
…-to-use-nested-schema
  • Loading branch information
thomasaarholt authored Dec 26, 2024
2 parents e23992e + 1154f2d commit 9268375
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/patito/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ def _find_errors( # noqa: C901

dataframe_tmp = (
dataframe_tmp.select(column_name)
.filter(pl.col(column_name).list.len() > 0)
.explode(column_name)
.unnest(column_name)
)
Expand Down
8 changes: 8 additions & 0 deletions tests/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
import patito as pt


class VerySmallModel(pt.Model):
"""Very small model for testing."""

a: int
b: str


class SmallModel(pt.Model):
"""Small model for testing."""

Expand Down Expand Up @@ -37,6 +44,7 @@ class ManyTypes(pt.Model):
date_value: date
datetime_value: datetime
pt_model_value: SmallModel
pt_list_model_value: list[VerySmallModel]


class CompleteModel(pt.Model):
Expand Down
12 changes: 10 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
DATE_DTYPES,
TIME_DTYPES,
)
from tests.examples import CompleteModel, ManyTypes, SmallModel
from tests.examples import CompleteModel, ManyTypes, SmallModel, VerySmallModel


def test_model_example() -> None:
Expand All @@ -47,11 +47,13 @@ def test_model_example() -> None:
"date_value": date(year=1970, month=1, day=1),
"datetime_value": datetime(year=1970, month=1, day=1),
"pt_model_value": SmallModel.example().model_dump(),
"pt_list_model_value": [VerySmallModel.example().model_dump()],
}
assert ManyTypes.example(
bool_value=True,
default_value="override",
optional_value=1,
pt_list_model_value=[],
).model_dump() == {
"int_value": -1,
"float_value": -0.5,
Expand All @@ -64,10 +66,16 @@ def test_model_example() -> None:
"date_value": date(year=1970, month=1, day=1),
"datetime_value": datetime(year=1970, month=1, day=1),
"pt_model_value": SmallModel.example().model_dump(),
"pt_list_model_value": [],
}

ManyTypes.validate(ManyTypes.examples({"int_value": range(200)}))

# Empty list should be valid
ManyTypes.validate(
ManyTypes.examples({"pt_list_model_value": [[], [VerySmallModel.example()]]})
)

# For now, valid regex data is not implemented
class RegexModel(pt.Model):
regex_column: str = pt.Field(pattern=r"[0-9a-f]")
Expand Down Expand Up @@ -559,7 +567,7 @@ class AliasModel(pt.Model):


def test_validation_returns_df(): # noqa: D103
for Model in [SmallModel, ManyTypes, CompleteModel]:
for Model in [VerySmallModel, SmallModel, ManyTypes, CompleteModel]:
df = Model.examples()
remade_model = Model.validate(df)
assert_frame_equal(remade_model, df)
Expand Down

0 comments on commit 9268375

Please sign in to comment.