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

Set/override config options when calling .validate() #1837

Open
gwerbin opened this issue Oct 25, 2024 · 0 comments
Open

Set/override config options when calling .validate() #1837

gwerbin opened this issue Oct 25, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@gwerbin
Copy link

gwerbin commented Oct 25, 2024

Often I want to reuse the same schema/model for multiple different purposes.

When loading data from an outside source, I want to run parsers, including the built-in type coercion, and set strict=False to avoid crashing at runtime due to the presence of extra columns that I don't care about. But when running my unit tests, I want strict=True to assert that extra columns are not present, but I do not want to run any parsers or coercion.

In the current setup, I need to create separate schemas/models with different configs for different purposes, and that's very verbose.

Currently I have to write this:

class _WidgetBaseModel(DataFrameModel):
    foo_id: Index[int] = Field(check_name=True)
    width: Series[float]
    height: Series[float]

class WidgetInputModel(_WidgetBaseModel):
    class Config:
        strict = False
        coerce = True

class WidgetOutputModel(_WidgetBaseModel):
    class Config:
        strict = True
        coerce = False

WidgetInputModel.validate(df)  # strict=False, coerce=True
WidgetOutputModel.validate(df)  # strict=True, coerce=False

But I want to be able to write this:

class WidgetModelBase(DataFrameModel):
    foo_id: Index[int] = Field(check_name=True)
    width: Series[float]
    height: Series[float]

WidgetModelBase.validate(df, strict=False, coerce=True)
WidgetModelBase.validate(df, strict=True, coerce=False)

The examples above use the class-based "model" syntax, but this request applies equally to the functional "schema" syntax as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant