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

check_types decorator doesnt validate after inplace mutation with method #1475

Open
stephansmit opened this issue Jan 31, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@stephansmit
Copy link

stephansmit commented Jan 31, 2024

I have this code, as stated in the docs:

from pandera.typing import DataFrame
from pandera import SchemaModel, check_types
class Schema(SchemaModel):
    id: int
    name: str

    class Config:
        strict = True

class SchemaOut(SchemaModel):
    age: int


@check_types
def fn_mutate_inplace(df: DataFrame[Schema]) -> DataFrame[SchemaOut]:
    out = df.assign(age=30).pipe(DataFrame[SchemaOut])
    out.drop(["age"], axis=1, inplace=True)
    return out


df = pd.DataFrame({
    "id": [1, 2, 3],
    "name": ["a", "b", "c"]
})

df2 = fn_mutate_inplace(df.pipe(DataFrame[Schema]))
df2

I expect that mypy will succeed, but that the check_types decorator will validate the output at runtime and therefore will fail. However, this code runs, and gives me back a dataframe which is not consistent with the SchemaOut.

Is this correct behaviour, and should I only pipe to the final schema at the very end of a function?

If I dont pipe the dataframe to SchemaOut, the code will fail and pandera will catch the error at runtime, but then mypy will fail. It seems that the check_types decorator is not checking anymore when the types are already piped in the method.

Python version 3.10.11

pandas==2.2.0
pandas-stubs==2.1.4.231227
pandera==0.18.0
pydantic==2.6.0
pydantic_core==2.16.1

@stephansmit stephansmit added the bug Something isn't working label Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant