Skip to content

Tutorial

6. Check the data at run time

The checker proves that the code agrees with the declared types. It cannot prove that a file agrees with them. Check the data once, where it enters the program.

check_schema checks at run time that a frame has exactly the declared columns, with their data types. For mypy, it gives the typed frame:

pipeline.pymypy: no errors
1import polars as pl
2
3from astrolabe import Columns, TypedDataFrame, check_schema
4
5
6class Trades(Columns):
7 symbol: pl.String
8 price: pl.Float64
9 quantity: pl.Int64
10
11
12raw = pl.read_parquet("trades.parquet")
13trades = check_schema(raw, TypedDataFrame[Trades])
Resulttradesdeclared
symbolStringpriceFloat64quantityInt64
Revealed type
TypedDataFrame[TypedDict(Trades, {'symbol': String, 'price': Float64, 'quantity': Int64}, closed=True), None]

Write the frame type in the call as an annotation writes it. The result has the type of a variable annotated TypedDataFrame[Trades].

At run time, check_schema raises if a column is missing, if a column has another data type, or if the frame has a column that the type does not declare. The order of the columns does not matter.

Two other functions give a typed frame:

FunctionAt run time
check_schemaChecks the columns and the data types, and casts nothing.
force_schemaKeeps the declared columns in their declared order, and casts each one to its declared data type.
assume_schemaChecks nothing. Use it only for a frame that you know is correct.

See Runtime checks.

Next: 7. Make the checker stricter.