JakobGM / patito

A data modelling layer built on top of polars and pydantic
MIT License
289 stars 23 forks source link

Validation fails on an empty list #103

Open dhimmel opened 5 hours ago

dhimmel commented 5 hours ago

The following model validation fails on an empty list:

import polars as pl
from patito import Model

class TestModel(Model):
    list_field: list[str]

df = pl.DataFrame({"list_field": [["a", "b"], []]})
TestModel.validate(df)

With the error message:

DataFrameValidationError: 1 validation error for TestModel
list_field
  1 missing value in lists (type=value_error.missingvalues)

I would expect an empty list to validate as list[str], at least Pydantic thinks so:

from pydantic import BaseModel

class TestPydanticModel(BaseModel):
    list_field: list[str]

TestPydanticModel(list_field=[])  # happy
dhimmel commented 5 hours ago

Changing to list_field: list[str | None] passes validation.