pola-rs/polars · error

didn't expect so many arguments

Error message

didn't expect so many arguments

What it means

The macro's final catch-all panics with "didn't expect so many arguments" (lib.rs:130) when a #[polars_expr] function has three or more parameters. Only 0, 1 (kwargs|context), or 2 (context, kwargs) parameter shapes are supported for the special-call codegen.

Solutions

  1. Reduce the signature to Series parameters plus at most one `kwargs` parameter; pack all extra options into the kwargs struct.
  2. If both context and options are needed, use exactly (context, kwargs) and nothing else.
  3. Precompute or hardcode unneeded parameters instead of passing them through the plugin boundary.

Example fix

// before
fn f(a: &Series, b: &Series, c: i64) -> ...
// after
fn f(a: &Series, kwargs: Wrapped<Kw>) -> ...  // Kw { b: Series-ish, c: i64 }
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Declaring #[polars_expr] on a function with 3+ parameters, e.g. fn f(a: &Series, b: &Series, c: i64) or f(context, kwargs, extra).

Common situations: Trying to pass multiple literal arguments positionally; converting a general Rust helper with many parameters into a plugin expression without consolidating options.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

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

        })
        .collect::<Vec<_>>();

    let fn_name = &ast.sig.ident;

    // Get the tokenstream of the call logic.
    let quote_call = match args.len() {
        0 => quote_call_no_kwargs(&ast, fn_name),
        1 => match args[0].as_str() {
            "kwargs" => quote_call_kwargs(&ast, fn_name),
            "context" => quote_call_context(&ast, fn_name),
            a => panic!("didn't expect argument {a}"),
        },
        2 => match (args[0].as_str(), args[1].as_str()) {
            ("context", "kwargs") => quote_call_context_kwargs(&ast, fn_name),
            ("kwargs", "context") => panic!("'kwargs', 'context' order should be reversed"),
            (a, b) => panic!("didn't expect arguments {a}, {b}"),
        },
        _ => panic!("didn't expect so many arguments"),
    };

    let quote_process_result = quote_process_results();
    let fn_name = get_expression_function_name(fn_name);

    quote!(
        use ::pyo3_polars::export::*;

        // create the outer public function
        #[no_mangle]
        pub unsafe extern "C" fn #fn_name (
            e: *mut polars_ffi::version_0::SeriesExport,
            input_len: usize,
            kwargs_ptr: *const u8,
            kwargs_len: usize,
            return_value: *mut polars_ffi::version_0::SeriesExport,
            context: *mut polars_ffi::version_0::CallerContext
        )  {

View on GitHub (pinned to fe841f959e)