pola-rs/polars · error

didn't understand polars_expr attribute

Error message

didn't understand polars_expr attribute

What it means

The #[polars_expr] entry macro must generate an output-field function; if none of output_type_fn, output_type_fn_kwargs, or output_dtype was set in the parsed attributes, it panics with "didn't understand polars_expr attribute" (lib.rs:275). This is the public macro's guard against a #[polars_expr] usage that does not declare how to compute the return dtype.

Solutions

  1. Add an output type declaration, e.g. #[polars_expr(output_type=Int64)] or the appropriate dtype alias.
  2. If the dtype depends on inputs, supply #[polars_expr(output_type_func="my_field_fn")] (or output_type_func_with_kwargs) pointing to a function returning the DataType.
  3. Verify you are using #[polars_expr] with its attribute set, not #[polars] attributes, and that the referenced function name is in scope.

Example fix

// before
#[polars_expr]
fn my_expr(x: &Series) -> PolarsResult<Column> { ... }
// after
#[polars_expr(output_type=Int64)]
fn my_expr(x: &Series) -> PolarsResult<Column> { ... }
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Using #[polars_expr(...)] with no output-dtype-producing attribute (no output_type/dtype alias, no output_type_func, no output_type_func_with_kwargs), or with only attributes the field-fn generation path doesn't consume.

Common situations: Omitting output_type on a new expression function; renaming/refactoring that dropped the output_type_func reference; following an example for a different macro (#[polars] vs #[polars_expr]) whose attribute sets differ.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/fe229c1dbdc6ffa2. Report an issue: GitHub.

Appendix: source

Thrown at pyo3-polars/pyo3-polars-derive/src/lib.rs:275

            let out = polars_arrow::ffi::export_field_to_c(&out.to_arrow(polars_core::datatypes::CompatLevel::newest()));
            *return_value = out;
        }
    )
}

#[proc_macro_attribute]
pub fn polars_expr(attr: TokenStream, input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as syn::ItemFn);

    let options = parse_macro_input!(attr as attr::ExprsFunctionOptions);
    let expanded_field_fn = if let Some(fn_name) = options.output_type_fn {
        create_field_function(&ast.sig.ident, &fn_name, false)
    } else if let Some(fn_name) = options.output_type_fn_kwargs {
        create_field_function(&ast.sig.ident, &fn_name, true)
    } else if let Some(dtype) = options.output_dtype {
        create_field_function_from_with_dtype(&ast.sig.ident, dtype)
    } else {
        panic!("didn't understand polars_expr attribute")
    };

    let expanded_expr = create_expression_function(ast);
    let expanded = quote!(
        #expanded_field_fn

        #expanded_expr
    );
    TokenStream::from(expanded)
}

View on GitHub (pinned to fe841f959e)