unionai-oss / pandera

A light-weight, flexible, and expressive statistical data testing library
https://www.union.ai/pandera
MIT License
3.38k stars 310 forks source link

SchemaModel not able to use registered checks #372

Closed seb84924 closed 3 years ago

seb84924 commented 3 years ago

Describe the bug pa.SchemaModel is not able to use custom checks.


import pandas as pd
import pandera as pa
from pandera import Field
import pandera.extensions as extensions
from pandera.typing import Series

@extensions.register_check_method(statistics=["min_value", "max_value"])
def is_between(pandas_obj, *, min_value, max_value):
    return (min_value <= pandas_obj) & (pandas_obj <= max_value)

df = pd.DataFrame([10], columns=["col"])

schema = pa.DataFrameSchema(
    {"col": pa.Column(int, pa.Check.is_between(min_value=1, max_value=9))}
)

try:
    schema.validate(df)
except pa.errors.SchemaError:
    print("fails as expected")

class Schema(pa.SchemaModel):
    col: Series[int] = Field(is_between={"min_value": 1, "max_value": 9})

Schema.validate(df)
print("does not fail as expected")

Expected behavior

I would expect Schema.validate(df) to cause an error.

Desktop (please complete the following information):

cosmicBboy commented 3 years ago

Thanks for finding this issue @seb84924! this bug is kind of embarrassing đŸ˜…. #373 should address it. Will release this fix as part of 0.6.1.