Batchseam
Test your Polars expression on the whole fixture and on slices. Batchseam finds the first output that changes when a batch ends and can save the failing input as a pytest regression.
Two trades, two different answers
Try the packaged demo from any directory:
uvx --python 3.12 batchseam demo fx
To use the assertions in an existing Python 3.12 environment, run python -m pip install batchseam. For tests and development from this checkout:
python -m pip install ".[test]"
python -m batchseam demo fx
For an install from the release tag, run uvx --python 3.12 --from git+https://github.com/yyzquwu/batchseam.git@v0.5.0 batchseam demo fx.
Both trades have an amount of 10. Their exchange rates are 1 and 2. The demo callback uses the first rate in each batch. On the whole fixture it converts the second trade to 10. With a cut after the first trade it converts that same trade to 20. The demo exits 1 because the outputs differ.
Pass the same pl.Expr you use in production directly to pytest. This works when the expression encloses an inline lambda, a configured object's method, or a multi-column callback:
import polars as pl
from batchseam import assert_expr_elementwise
def test_trade_conversion():
trades = pl.DataFrame({"amount": [10, 10], "rate": [1.0, 2.0]})
usd = (pl.col("amount") * pl.col("rate")).alias("usd")
assert_expr_elementwise(usd, trades)
Use this assertion for a deterministic expression expected to produce one column and one value per input row. Batchseam evaluates the expression with Polars on the fixture and on each slice, checks lengths, then compares ordered values and dtype. A pass covers the fixture and splits tried. Polars chooses its own internal execution schedule.
To save the failing exchange-rate example, run batchseam demo fx --save-repro fx-repro, then batchseam replay fx-repro. For your own expression, use batchseam check pipeline.py:to_usd trades.parquet --expr --save-repro fx-regression. The target can be a module-level expression or a zero-argument factory that constructs it. Run python -m pytest -q fx-regression after correcting the source. The bundle contains real fixture rows, so choose the input you want to keep.
Find your first check
From this checkout, use a Python 3.12 virtual environment.
python -m pip install ".[test]"
batchseam scan .
Run scan in a Python project to find calls to map_batches with an explicit is_elementwise=True or streamable=True. Each finding shows a source location, the callback, and a suggested next step. The scanner reads Python syntax without importing your project. A finding identifies a claim to test. You supply the fixture and run the check to learn whether the claim holds.
Try the whole workflow from the Batchseam checkout:
cd examples
batchseam scan .
batchseam check expressions.py:keep_positive amounts.csv --series amount --save-repro positive-repro
batchseam replay positive-repro
python -m pytest -q positive-repro
The scan exits 0. The check exits 1 because amounts.csv contains four amounts and the callback returns only three positive ones. Replay and pytest reproduce the failure. When trying your own project, substitute a representative CSV or Parquet file and the actual input column in the command template. Relative commands use the root printed by the scan. Start subsequent examples from the checkout root.
How to use a finding
- Named function. SportsDataverse calls
expr.map_batches(_series_as_date, ..., is_elementwise=True)on an annotated Polars expression. The scan can point to the function in the same file. On a three-row fixture of ISO dates and a null, this callback passed both individual cuts. Upstream date parser - Inline lambda. Extract it into a shared named callback so your pipeline and test execute the same function. Geometry conversion example
- Bound method. Construct the instance with your project's configuration and pass its bound method to the assertion. Interpolation example
- Module-qualified function. For a callback such as
helpers.normalize, import the same module in your test and passhelpers.normalizedirectly to the assertion.
An ambiguous receiver needs a human to confirm its type. Dynamic flags, wrappers and cross-module resolution can leave calls undiscovered. For automation, use batchseam scan . --json. An incomplete scan exits 2 and reports the files that could not be read or parsed alongside findings from the other files.
You can also scan one Python file. A directory scan skips nested environments, dependency and build folders, links, and recognized generated files. Selecting a folder such as build explicitly scans its source. Selecting a virtual environment itself reports an incomplete scan.
Try the Series demos
python -m batchseam demo dates
python -m batchseam demo center
python -m batchseam demo positive
| Series callback | What the fixture shows | Exit |
|---|---|---|
| Parse ISO dates | Dates and nulls stay in the same positions across the tested splits. | 0 |
| Subtract the batch mean | For [10, 30], the first output changes from −10 to 0 when the values are processed separately. |
1 |
| Keep positive values | Three inputs produce only two outputs, before a split is even tried. | 1 |
Test an elementwise callback
import polars as pl
from batchseam import assert_elementwise
def parse_date(values):
return values.str.to_date("%Y-%m-%d")
def test_parse_date():
dates = pl.Series("day", ["2026-01-01", None, "2026-01-03"])
assert_elementwise(parse_date, dates)
Pass the same pure, single-input Series -> Series function you would give to Expr.map_batches. The callback may change dtype, as the date example does. Its results must keep that output dtype when the input is split. Use check_elementwise to get the result dictionary. A passing fixture is evidence for the cuts tried. Polars expression callback contract
batchseam check examples/expressions.py:keep_positive examples/amounts.csv --series amount --save-repro positive-repro
batchseam replay positive-repro --json
python -m pytest -q positive-repro
These commands intentionally fail on the sample callback. The Parquet bundle contains only the selected column and replays the Series check against the current callback. Use at least two rows for a regression test you expect to pass after a fix. A shorter fixture can demonstrate a length violation, but after correction there is no interior cut to test and replay returns exit 2. Existing v0.2 DataFrame bundles still replay.
Run python examples/expressions.py to see the distinction on [-1, 2, 3]: filtering commutes with every single split, but native Polars cannot place the two surviving values in a three-row column.
Find a boundary in a DataFrame transform
python -m batchseam demo sensor --save-repro sensor-repro
The demo forward-fills 100 sensor readings. Only row 41 is null. When a batch starts there, the callback loses the preceding value. The demo exits 1 and saves the failure in a new directory.
| Reduced input | Whole-frame output | Output with a cut after the first row |
|---|---|---|
[40, null] |
[40, 40] |
[40, null] |
python -m batchseam replay sensor-repro
python -m pytest -q sensor-repro
Both commands reproduce the failure. The bundle contains the ordered rows in fixture.parquet, the transform reference and cut offsets in repro.json, and a pytest test. For a transform from your project, the test runs your current code. Keep the bundle and source in the same relative locations when moving a project. Paths across Windows drives use an absolute reference. The original running-total example is still available with python -m batchseam demo.
Check your own transform
Give Batchseam a trusted Python file with a function that accepts and returns an eager Polars DataFrame. Regular Python packages can use package-relative imports. Make your project's other dependencies available in the active environment, just as you would for its tests. The generated test passes pytest's import paths to the replay. Use a deterministic, stateless transform and a fixture small enough to fit in memory. Each test calls your function multiple times.
batchseam check examples/transforms.py:running_total examples/amounts.csv --cuts 2 --save-repro balance-repro
batchseam check examples/transforms.py:cents examples/amounts.csv --json
CSV and Parquet inputs use the same command. The cents example passes because each row converts independently. A global running balance needs state across batches. In a real Polars pipeline, express that calculation with a native cumulative expression on the full lazy frame, or run the callback on the whole input.
from batchseam import assert_batch_invariant
def test_my_transform(frame):
assert_batch_invariant(my_transform, frame, cuts=[2, 5])
Use check_batches when you want the result dictionary. For either API, the default mixes ordinary cuts with boundaries where columns enter or leave null runs. It stops at the first failure or 16 plans. On the sensor fixture, v0.1 passed all 16 default plans and needed 48 attempts with an increased budget to find cut 41. Version 0.2 finds it on attempt five. Checking all 99 single cuts independently confirmed that only cut 41 fails.
--max-plans Nsets the discovery budget.--exhaustivetries every single interior cut in order until one fails. Use it on small fixtures. Combinations of multiple cuts can expose additional failures.--cuts 2,5tests precisely[:2],[2:5], and[5:].--save-repro DIRtries up to 128 reductions and rechecks the saved Parquet data. It can return a larger fixture when further reduction changes the failure. The rows still contain their original values. Choose a new directory and review the rows before sharing it. A successful check creates no bundle.
Comparisons use native Polars dtypes and exact floating-point comparisons. DataFrame checks also compare column names and order. An output offset refers to the transformed frame, which can have a different number of rows than the input. If comparing values requires Python Object equality, the CLI reports an inconclusive run (exit 2). Use a native output dtype, such as a string representation of a UUID, for an ordered value comparison.
| Exit | Meaning |
|---|---|
| 0 | Every tested split matched on this fixture. Other inputs or splits may differ. |
| 1 | The callback broke the Series length contract, a batch changed the output, or a batch raised after the whole-input call succeeded. |
| 2 | The run was inconclusive. This includes fewer than two rows, an invalid command or a failed whole-frame call. |
Native streaming experiment
python examples/streaming.py
Polars requires a streamable map_batches callback to produce the same result on batches as on the full dataset. Its streaming design discussion uses cumulative sums to illustrate the need for cross-batch state. Polars documentation · Polars discussion
The script marks the two example callbacks streamable and lets native Polars choose its own batches. On one Windows run with Polars 2.1.0, eight batches of 100,000 total one-unit rows yielded a final running balance of 12,500 versus 100,000 on the whole frame. The row-local conversion finished at 100 cents in both runs. Batch boundaries depend on the environment. Batchseam's explicit-cut replay is independent of the native schedule.
Run the tests
python -m pip install ".[test]"
python -m pytest -q
Metadata
Release files for batchseam 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| batchseam-0.5.0.tar.gz | 54.4 kB | With provenance | Details for batchseam-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| batchseam-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for batchseam-0.5.0-py3-none-any.whl |
Total release size: 84.0 kB
Release files / batchseam-0.5.0.tar.gz
| Download URL | batchseam-0.5.0.tar.gz |
|---|---|
| Size | 54.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19a7e4f7686d819f343d7ffb450077b7a3471f7daa3436f6035f2a72b05a8635
|
|
BLAKE2b-256 checksum How to use checksums |
4fa3a193604f920d21980768669948e410458468d2a717c6c481f513bc4153b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.
Transparency logRelease files / batchseam-0.5.0-py3-none-any.whl
| Download URL | batchseam-0.5.0-py3-none-any.whl |
|---|---|
| Size | 29.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
738d12fa5ed2c98c7901921096feb91ceefb9fb80a0d5bb35b8a089d8b60f0d5
|
|
BLAKE2b-256 checksum How to use checksums |
50d2d828d0b9f55d4bc8d986418fd81460828543148d470d1b46aafb4af5ba69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.
Transparency logSecurity for batchseam 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for batchseam-0.5.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.