JakobGM / patito

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

bug: Returned model loses all field definitions after using `rename`, `with_fields`, `select` & `drop` #15

Closed ion-elgreco closed 5 months ago

ion-elgreco commented 12 months ago

In the example below, you can see I create id with Uint16, however all the models after using rename, with_fields , select & drop lose their field definitions that were provided in pt.Field.

class Product(pt.Model):
    id: int = pt.Field(unique=True, dtype=pl.UInt16)
    row_id: int
print(Product.dtypes)

{'id': UInt16, 'row_id': Int64}

With_fields

print(Product.with_fields().dtypes)
{'id': Int64, 'row_id': Int64}

Rename

print(Product.rename({'row_id':'row'}).dtypes)
{'id': Int64, 'row': Int64}

Drop

print(Product.drop('row_id').dtypes)
{'id': Int64}

@JakobGM @thomasaarholt

ion-elgreco commented 12 months ago

From what I can see, here is the issue, and every class_method is affected that uses _derive_model:

https://github.com/JakobGM/patito/blob/77923db45194acea7de49be8194100870833e982/src/patito/pydantic.py#L1391-L1394

It should actually not only grab the defaults but also pass the other schema_propertiesinto it.