pola-rs/polars · error

limit should be positive

Error message

limit should be positive

What it means

Config.set_expr_depth_warning sets POLARS_MAX_EXPR_DEPTH, the expression nesting depth tolerated before Polars warns about potential stack overflow. A negative limit is invalid and raises ValueError. Note the check is `limit < 0`, so 0 is technically accepted.

Solutions

  1. Pass a non-negative integer (typically a large value like 100_000 to silence the warning).
  2. To effectively disable the warning, set a very high limit rather than a negative one.
  3. Clamp computed values: max(0, computed_limit).

Example fix

// before
pl.Config.set_expr_depth_warning(-1)
// after
pl.Config.set_expr_depth_warning(100_000)
Defensive patterns

Strategy: validation

Validate before calling

if limit < 0:
    raise ValueError("limit must be non-negative")
pl.Config.set_expr_depth_warning(limit)

Type guard

def is_valid_limit(v) -> bool:
    return isinstance(v, int) and v >= 0

Try / catch

try:
    pl.Config.set_expr_depth_warning(limit)
except ValueError:
    pl.Config.set_expr_depth_warning(10_000)

Prevention

When it happens

Trigger: Calling pl.Config.set_expr_depth_warning(-1) or any negative integer.

Common situations: Sign errors when deriving the limit from a delta (e.g. current_depth - margin); intending to disable the warning by passing a negative number instead of a very large one.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/config.py:1537

        UnstableWarning: `bin_quantiles` is considered unstable. It may be changed at any point without it being considered a breaking change.
        """  # noqa: W505
        if active is None:
            os.environ.pop("POLARS_WARN_UNSTABLE", None)
        else:
            os.environ["POLARS_WARN_UNSTABLE"] = str(int(active))
        plr.config_reload_env_var("POLARS_WARN_UNSTABLE")
        return cls

    @classmethod
    def set_expr_depth_warning(cls, limit: int) -> type[Config]:
        """
        Set the expression depth that Polars will accept without triggering a warning.

        Having too deep expressions (several 1000s) can lead to overflowing the stack and might be worth a refactor.
        """  # noqa: W505
        if limit < 0:
            msg = "limit should be positive"
            raise ValueError(msg)

        os.environ["POLARS_MAX_EXPR_DEPTH"] = str(limit)
        plr.config_reload_env_var("POLARS_MAX_EXPR_DEPTH")
        return cls

    @classmethod
    def set_engine_affinity(cls, engine: EngineType | None = None) -> type[Config]:
        """
        Set which engine to use by default.

        Parameters
        ----------
        engine : {None, 'auto', 'in-memory', 'streaming', 'gpu'}
            The default execution engine Polars will attempt to use
            when calling `.collect()`. However, the query is not
            guaranteed to execute with the specified engine.

            An :class:`Engine` object may also be used to configure the default. Engine

View on GitHub (pinned to fe841f959e)