Skip to content

Guides

Python functions and Unchecked

The checker cannot read the body of a Python function that Polars runs on the data. So it cannot know the data type that such a function gives.

Unchecked is the data type of a column whose name the checker knows and whose data type it does not:

python.pymypy: no errors
1class Trades(Columns):
2 symbol: pl.String
3 price: pl.Float64
4 quantity: pl.Int64
5
6
7trades: TypedDataFrame[Trades]
8labelled = trades.with_columns(label=pl.col("symbol").map_elements(str.lower))
Resultlabelleddeclaredadded
symbolStringpriceFloat64quantityInt64labelUnchecked
Revealed type
TypedDataFrame[TypedDict({'symbol': String, 'price': Float64, 'quantity': Int64, 'label': Unchecked}, closed=True), None]

Every later operation accepts an Unchecked column. So a program that runs one function in Python keeps every other check: the other columns keep their data types, and a later read of a missing name is still an error.

These members give Unchecked: map_elements, map_batches, pl.map_batches, pl.map_groups, pl.fold, pl.reduce, pl.cum_fold and pl.cum_reduce without return_dtype=, rolling_map, and the map_columns of a frame.

return_dtype= names the data type of the result. The column is typed again:

python.pymypy: no errors
1class Trades(Columns):
2 symbol: pl.String
3 price: pl.Float64
4 quantity: pl.Int64
5
6
7trades: TypedDataFrame[Trades]
8lowered = pl.col("symbol").map_elements(str.lower, return_dtype=pl.String)
9labelled = trades.with_columns(label=lowered)
Resultlabelleddeclaredadded
symbolStringpriceFloat64quantityInt64labelString
Revealed type
TypedDataFrame[TypedDict({'symbol': String, 'price': Float64, 'quantity': Int64, 'label': String}, closed=True), None]

At the level stated or a stricter one, a member without its return_dtype= is an error, and a note names the setting that lets it pass. See Make the checker stricter and Configuration.

A Columns class can declare a column as Unchecked. check_schema then checks that the column is present, and does not check its data type.