Health Checks

pyrunner_lib provides a declarative and fluent API for defining health checks on your data transformations. These checks are designed to be efficient, working lazily on Polars LazyFrame objects to collect only the necessary aggregate statistics without loading the entire dataset into memory.

Health checks are defined using the Check class, which allows you to chain multiple validation methods for a specific column.

Severity Levels

Each check can have an optional severity level:

  • warn (Default): If the check fails, it is recorded in the health report, but the transformation continues and no exception is raised.

  • fail: If any check with fail severity fails, a HealthCheckFailure exception is raised after all checks have been executed, preventing the build from succeeding.

API Reference

Check(column_name: str)

Initializes a check builder for the specified column.


Basic Checks

.no_nulls(severity=None)

Ensures that the column contains no null values.

.non_empty_strings(severity=None)

Ensures that all string values in the column are non-empty after stripping whitespace.

.unique(severity=None)

Ensures that all values in the column are unique. Note: This defaults to warn in many contexts as it can be a common occurrence.


Range & Value Checks

Checks if all values are within the specified inclusive range.

Checks if all values in the column are present in the allowed list.

Ensures that all string values match the provided regular expression pattern.

Allows up to max_pct (0-100) of the values in the column to be null.


Numeric Checks

Provides various numeric comparisons:

  • eq: Equal to

  • gt: Greater than

  • gte: Greater than or equal to

  • lt: Less than

  • lte: Less than or equal to

  • not_eq: Not equal to

  • sum_eq: The sum of the column must equal this value.


Aggregate Checks

Ensures the column has exactly the specified number of distinct values.


Custom Checks

Allows you to provide a custom validation function. The function should have the signature func(lf: pl.LazyFrame, col: str) and should raise a ValueError with a descriptive message if the check fails.

Examples

Health Report

When health checks are run (usually handled by the runner), a health_report.json file is generated in the META_FOLDER. This report contains a summary of passed and failed checks, along with error messages for any failures.

Last updated