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
- Coerce string-like values explicitly: pl.escape_regex(str(s))
- Fix the None source — use required keys or a sensible default like ''
- 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
- Use required dict keys or explicit defaults instead of .get() for values fed to escape_regex
- Decode bytes at the system boundary before regex processing
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
- Series only supports 'vertical' concat strategy
- did not expect type: {qualified_type_name(elems[0])!r} in `c
- merge_sorted is not supported for {qualified_type_name(elems
- input frames must be of a consistent type (all LazyFrame or
- escape_regex function is unsupported for `Expr`, you may wan
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/90d475d98bf5c5be.
Report an issue: GitHub.