Skip to main content

Validate.to_code()

Render this validation plan as canonical Pointblank Python code.

Usage

Source

Validate.to_code()

The to_code() method walks the validation plan and reconstructs the equivalent pb.Validate(...).col_vals_*()... method chain as a string. This is the inverse of writing the plan by hand: it takes an in-memory Validate object and produces source code that, when executed, recreates the same plan.

This is useful for sharing a plan, reviewing it in a diff, persisting it alongside results, and it is the foundation for the AI edit/iterate flow (see EditValidation), which sends the current plan to a model as editable code.

Returns

str
The validation plan as a block of Python code. The data source is rendered as the placeholder your_data, since the original data variable name is not known to the plan. Reference data given as a file path or connection string is written out as-is; an in-memory reference table becomes the placeholder your_reference_data (with a warning).

Notes On Fidelity

Most validation steps round-trip exactly. Steps that carry non-serializable Python objects, such as pre= preprocessing callables, actions=, callable active= conditions, or the expressions used by col_vals_expr(), conjointly(), and specially(), cannot be reproduced from memory. For those, a syntactically valid placeholder is emitted and a warning is raised so the code still parses and the loss is visible.

Examples

Let’s create a validation plan for the small_table dataset, with thresholds and four steps, and render it as code.

import pointblank as pb

small_table = pb.load_dataset(dataset="small_table", tbl_type="polars")

validation = (
    pb.Validate(data=small_table, tbl_name="small_table", thresholds=(0.1, 0.2))
    .col_vals_gt(columns="d", value=100)
    .col_vals_not_null(columns="c")
    .col_vals_in_set(columns="f", set=["low", "mid", "high"])
    .rows_distinct()
)

print(validation.to_code())
import pointblank as pb

validation = (
    pb.Validate(
        data=your_data,  # Replace your_data with the actual data variable
        tbl_name="small_table",
        thresholds=pb.Thresholds(warning=0.1, error=0.2),
    )
    .col_vals_gt(columns="d", value=100)
    .col_vals_not_null(columns="c")
    .col_vals_in_set(columns="f", set=["low", "mid", "high"])
    .rows_distinct()
)

validation

The code has every step with its arguments, and the thresholds written out as a Thresholds object. The table is the placeholder your_data, and the code doesn’t call interrogate(), since it describes the plan rather than a run of it. Running the code with your_data defined recreates the plan, which can then be interrogated:

namespace = {"your_data": small_table}

exec(validation.to_code(), namespace)

namespace["validation"].interrogate()
Pointblank Validation
2026-11-05|00:18:31
Polarssmall_tableWARNING0.1ERROR0.2CRITICAL—
STEP COLUMNS VALUES TBL EVAL UNITS PASS FAIL W E C EXT
#4CA64C 1
col_vals_gt
col_vals_gt()
d 100 ✓ 13 13
1.00
0
0.00
○ ○ — —
#AAAAAA 2
col_vals_not_null
col_vals_not_null()
c — ✓ 13 11
0.85
2
0.15
● ○ —
#4CA64C 3
col_vals_in_set
col_vals_in_set()
f low, mid, high ✓ 13 13
1.00
0
0.00
○ ○ — —
#AAAAAA 4
rows_distinct
rows_distinct()
ALL COLUMNS — ✓ 13 11
0.85
2
0.15
● ○ —

The recreated plan finds the same failures as the original would (the two missing values in c and the duplicated rows). When a step has something that can’t be written as code, such as a pre= function, the code still parses but a warning says what was lost:

validation_pre = pb.Validate(data=small_table).col_vals_gt(
    columns="d", value=100, pre=lambda df: df.head(5)
)

print(validation_pre.to_code())
import pointblank as pb

validation = (
    pb.Validate(
        data=your_data,  # Replace your_data with the actual data variable
    )
    .col_vals_gt(columns="d", value=100, pre=None)
)

validation
/tmp/ipykernel_9686/975139424.py:5: UserWarning: Some parts of the validation plan could not be fully serialized to code:
- Step 'col_vals_gt' has a `pre=` preprocessing callable that cannot be serialized; it was dropped from the generated plan.
  print(validation_pre.to_code())

The pre= function is written as pre=None, so the generated step would check every row rather than the first five. Fix up placeholders like this one before using the code.