JakobGM / patito

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

feat: decorator to validate pt.Model type hints #38

Open s-breeze opened 6 months ago

s-breeze commented 6 months ago

@validate_hints decorates functions or methods running .validate on function arguments and the return value if they inherit from pt.pydantic.ModelMetaclass. Example usage:


class MyModel(pt.Model):
    a: int

@pt.validation_hints
def func(arg: MyModel) -> MyModel:
    ...
    return result

func(pl.DataFrame({"a": [1, 2, 3]})  # validates argument and return value
thomasaarholt commented 4 months ago

Sorry for only getting to this now! I like implementation and the idea is neat, but I'm missing a bit more info on what the usecase is here. I'd only want to add features that have a well-defined usecase so as to minimize what we have to maintain.

I understand how the feature should work, but I'm missing some examples on what could replace the ... in the following example.

import patito as pt
import polars as pl

class MyModel(pt.Model):
    a: int

@pt.validate_hints
def func(arg: MyModel) -> MyModel:
    # ... <- What should go here?
    return arg

# this df will raise an error when validated
df = pl.DataFrame({"a": [1.0, 2.0, 3.0]})

func(df)
MyModel.validate(df) # this does the same thing as the above