Skip to content

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

Open
rm-shanebreeze wants to merge 1 commit intoJakobGM:mainfrom
rm-shanebreeze:decorators
Open

feat: decorator to validate pt.Model type hints#38
rm-shanebreeze wants to merge 1 commit intoJakobGM:mainfrom
rm-shanebreeze:decorators

Conversation

@rm-shanebreeze
Copy link
Copy Markdown

@rm-shanebreeze rm-shanebreeze commented Jan 25, 2024

@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
Copy link
Copy Markdown
Collaborator

thomasaarholt commented Mar 31, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants