pola-rs/polars · error · TypeError

escape_regex function supports only `str` type, got `{qualif

Error message

escape_regex function supports only `str` type, got `{qualified_type_name(s)}`

What it means

pl.escape_regex accepts exactly one str; any other type (int, float, None, bytes, pl.Series, numpy scalar) raises TypeError with the qualified type name. The escaped result feeds the regex engine, which only understands Python strings.

Source

Thrown at py-polars/src/polars/functions/escape_regex.py:27

import polars._reexport as pl


def escape_regex(s: str) -> str:
    r"""
    Escapes string regex meta characters.

    Parameters
    ----------
    s
        The string whose meta characters will be escaped.

    """
    if isinstance(s, pl.Expr):
        msg = "escape_regex function is unsupported for `Expr`, you may want use `Expr.str.escape_regex` instead"
        raise TypeError(msg)
    elif not isinstance(s, str):
        msg = f"escape_regex function supports only `str` type, got `{qualified_type_name(s)}`"
        raise TypeError(msg)

    return plr.escape_regex(s)

View on GitHub (pinned to df599052da)

Solutions

  1. Coerce string-like values explicitly: pl.escape_regex(str(s))
  2. Fix the None source — use required keys or a sensible default like ''
  3. Decode bytes first: pl.escape_regex(b.decode('utf-8'))

Example fix

# before
pl.escape_regex(user.get('needle'))  # None -> TypeError

# after
needle = user.get('needle', '')
pl.escape_regex(str(needle))
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(s, str):
    s = str(s)  # or raise early with context about the bad value
escaped = pl.escape_regex(s)

Type guard

def is_plain_str(s) -> bool:
    return isinstance(s, str)

Prevention

When it happens

Trigger: pl.escape_regex(None) from dict.get('needle') with no default; pl.escape_regex(42); pl.escape_regex(b'a.b') bytes from a network layer; pl.escape_regex(s) where s is a pl.Series.

Common situations: Optional config values that arrive as None; numeric identifiers passed where text was expected; undecoded bytes from file/network input.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/90d475d98bf5c5be. Report an issue: GitHub.