Skip to content

Commit 0af3e08

Browse files
chuenchen309claude
andcommitted
fix(io): format dataframe-level checks in to_script
to_script(minimal=False) interpolated the raw statistics list for dataframe-level checks straight into the script template, while the column path 19 lines above formats its checks with _format_checks. The generated script runs and looks correct, but the schema it builds carries plain dicts where Check objects belong. Every validate() call on it then fails with TypeError("'dict' object is not callable"), including on data the original schema accepts, so the round-tripped schema is unusable rather than merely wrong. Format dataframe-level checks the same way the column path does. _format_checks(None) already returns "None", so schemas without dataframe-level checks are unaffected. The existing test_to_script only covers column checks, so this path was untested; add a round-trip test with a dataframe-level check. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 9d86aed commit 0af3e08

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

pandera/io/pandas_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ def to_script(dataframe_schema, path_or_buf=None, *, minimal: bool = True):
838838

839839
script = SCRIPT_TEMPLATE.format(
840840
columns=column_str,
841-
checks=statistics["checks"],
841+
checks=_format_checks(statistics["checks"]),
842842
index=index,
843843
dtype=dataframe_schema.dtype,
844844
coerce=dataframe_schema.coerce,

tests/io/test_pandas_io.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,28 @@ def test_to_script(index):
15471547
assert schema == schema_to_write
15481548

15491549

1550+
@pytest.mark.skipif(
1551+
platform.system() == "Windows",
1552+
reason="skipping due to issues with opening file names for temp files.",
1553+
)
1554+
def test_to_script_dataframe_level_checks():
1555+
"""Test that dataframe-level checks survive a to_script round trip."""
1556+
schema_to_write = pandera.DataFrameSchema(
1557+
{"a": pandera.Column(int)},
1558+
checks=[pandera.Check.gt(0)],
1559+
)
1560+
1561+
local_dict = {}
1562+
# pylint: disable=exec-used
1563+
exec(schema_to_write.to_script(minimal=False), globals(), local_dict)
1564+
schema = local_dict["schema"]
1565+
1566+
assert schema == schema_to_write
1567+
schema.validate(pd.DataFrame({"a": [1, 2]}))
1568+
with pytest.raises(pandera.errors.SchemaError):
1569+
schema.validate(pd.DataFrame({"a": [-1]}))
1570+
1571+
15501572
def test_to_script_lambda_check():
15511573
"""Test writing DataFrameSchema to a script with lambda check."""
15521574
schema1 = pandera.DataFrameSchema(

0 commit comments

Comments
 (0)