Tutorial
4. Let the body decide the return type
A function that adds a column to an open frame returns the columns of the caller, and the new one. The return type must say both.
Below, add_notional accepts any frame with the columns of Priced: price and quantity. Rest stands for the other columns of the caller. The function adds notional, so its result has the columns of WithNotional and the other columns of the caller.
Let the checker compute the remainder
Section titled “Let the checker compute the remainder”Write InferredRemainder in the place of the remainder, and wrap the returned value in infer(...). The checker computes the remainder from the body:
Revealed type
TypedDataFrame[TypedDict({'price': Float64, 'quantity': Int64, 'notional': Float64, 'symbol': String}, closed=True), None]The returned value must hold each column of WithNotional, with the same data type. At each call, the result keeps the other columns of the caller: the frame of Trades has symbol, and so does result. If a caller passes a notional column, with_columns replaces it, and the checker takes that into account.
Let the checker compute the whole frame
Section titled “Let the checker compute the whole frame”InferredDataFrame lets the checker compute the whole return type. With two return statements, the result is the union of the two frames:
Revealed type
TypedDataFrame[TypedDict({'value': Float64}, closed=True), None] | TypedDataFrame[TypedDict({'quantity': Int64}, closed=True), None]Write infer(...) around the whole value of each return. See Inferred return types for InferredExpr and InferredLazyFrame.