Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify metaclass to allow DataFrame[Foo] type propagation #99

Merged
merged 2 commits into from
Oct 22, 2024

Conversation

thomasaarholt
Copy link
Collaborator

@thomasaarholt thomasaarholt commented Aug 18, 2024

The main goal of this was to enable the following pyright type annotations:

import patito as pt
import polars as pl

class Foo(pt.Model):
    name: str


df1 = Foo.DataFrame({"name":["Bean"]})
df2 = pt.DataFrame({"name":["Bean"]}).set_model(Foo) # Type of "df" is "DataFrame[Unknown]"

Before

df1 and df2 report: # Type of "df" is "DataFrame[Unknown]"

After

df1 and df2 report: # Type of "df" is "DataFrame[Foo]"

Discussion

This allows us to robustly pass around dataframes that have the patito Model embedded in the type annotation.
I am still a bit unsure on how I feel about a df being a DataFrame[Foo] before .validate() but after .set_model(). @dsgibbons and @JakobGM, any thoughts on this?

@thomasaarholt thomasaarholt requested a review from JakobGM August 18, 2024 13:29
@dsgibbons
Copy link
Contributor

If Foo.DataFrame and set_model are supposed to be analogous to creating a Pydantic model, shouldn't we be running validate before returning the DataFrame? This would make patito function more like Pydantic, and, as a bonus, we then know that the resulting table is actually of type Foo. I guess the main issue is that validate may be quite expensive, but wouldn't everyone using this library want to run validate anyway?

@gab23r
Copy link

gab23r commented Oct 21, 2024

I don't think the validation should be done in set_model under the hood, I don't think that it a problem either to have the DataFrame type without the validation. Maybe we can add the validate=False argument to set_model to make it clearer.

In any case, I think having the typing correct if a HUGE quality of life improvement, is there other things bloking this PR ?

@thomasaarholt
Copy link
Collaborator Author

I think I'll merge this one as-is, and we can have a think about further improvements going ahead.

@thomasaarholt thomasaarholt merged commit 967b629 into main Oct 22, 2024
3 checks passed
@thomasaarholt thomasaarholt deleted the thomasaarholt/meta branch October 22, 2024 10:26
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.

3 participants