Skip to content

Commit

Permalink
fix validation and coercion on init bug (#1868)
Browse files Browse the repository at this point in the history
* fix validation and coercion on init bug

Signed-off-by: Niels Bantilan <[email protected]>

* fix test

Signed-off-by: Niels Bantilan <[email protected]>

---------

Signed-off-by: Niels Bantilan <[email protected]>
  • Loading branch information
cosmicBboy authored Dec 4, 2024
1 parent 0d5d05d commit efcd7b1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pandera/typing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,17 @@ def __setattr__(self, name: str, value: Any) -> None:

# prevent the double validation problem by preventing checks for
# dataframes with a defined pandera.schema
pandera_accessor = getattr(self, "pandera")
pandera_accessor = getattr(self, "pandera", None)

if (
pandera_accessor.schema is None
pandera_accessor is None
or pandera_accessor.schema is None
or pandera_accessor.schema != schema_model.to_schema()
):
pandera_accessor.add_schema(schema_model.to_schema())
self.__dict__ = schema_model.validate(self).__dict__
if pandera_accessor is None:
pandera_accessor = getattr(self, "pandera")
pandera_accessor.add_schema(schema_model.to_schema())


# pylint:disable=too-few-public-methods
Expand Down
Empty file added tests/core/modules/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions tests/core/modules/validate_on_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Module for unit testing validation on initialization."""

import numpy as np
import pandas as pd
import pandera as pa
from pandera.typing import DataFrame


class ExampleSchema(pa.DataFrameModel):
class Config:
coerce = True

a: np.int64


ExampleDataFrame = DataFrame[ExampleSchema]
validated_dataframe = ExampleDataFrame(pd.DataFrame([], columns=["a"]))
15 changes: 15 additions & 0 deletions tests/core/test_model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Tests schema creation and validation from type annotations."""

# pylint:disable=missing-class-docstring,missing-function-docstring,too-few-public-methods
import os
import re
import runpy
from copy import deepcopy
from typing import Any, Generic, Iterable, List, Optional, TypeVar

import numpy as np
import pandas as pd
import pytest

Expand Down Expand Up @@ -1081,6 +1084,18 @@ class Config:
DataFrame[SchemaNoCoerce](raw_data)


def test_validate_on_init_module():
"""Make sure validation on initialization works when run as a module."""
path = os.path.join(
os.path.dirname(__file__),
"modules",
"validate_on_init.py",
)
result = runpy.run_path(path)
expected = pd.DataFrame([], columns=["a"], dtype=np.int64)
pd.testing.assert_frame_equal(result["validated_dataframe"], expected)


def test_from_records_validates_the_schema():
"""Test that DataFrame[Schema] validates the schema"""

Expand Down

0 comments on commit efcd7b1

Please sign in to comment.