{"record":{"id":"90d475d98bf5c5be","repo":"pola-rs/polars","slug":"escape-regex-function-supports-only-str-type-go","errorCode":null,"errorMessage":"escape_regex function supports only `str` type, got `{qualified_type_name(s)}`","messagePattern":"escape_regex function supports only `str` type, got `(.+?)`","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/escape_regex.py","lineNumber":27,"sourceCode":"import polars._reexport as pl\n\n\ndef escape_regex(s: str) -> str:\n    r\"\"\"\n    Escapes string regex meta characters.\n\n    Parameters\n    ----------\n    s\n        The string whose meta characters will be escaped.\n\n    \"\"\"\n    if isinstance(s, pl.Expr):\n        msg = \"escape_regex function is unsupported for `Expr`, you may want use `Expr.str.escape_regex` instead\"\n        raise TypeError(msg)\n    elif not isinstance(s, str):\n        msg = f\"escape_regex function supports only `str` type, got `{qualified_type_name(s)}`\"\n        raise TypeError(msg)\n\n    return plr.escape_regex(s)\n","sourceCodeStart":9,"sourceCodeEnd":30,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/escape_regex.py#L9-L30","documentation":"Type guard in the top-level escape_regex function: it accepts only a plain str input. An Expr input gets a dedicated hint to use Expr.str.escape_regex instead, and any other non-str type raises this generic TypeError.","triggerScenarios":"pl.escape_regex(123), pl.escape_regex(b'pattern'), pl.escape_regex(None), or passing a list of strings instead of a single string.","commonSituations":"A variable expected to hold a string is None due to a failed lookup; passing bytes read from a file; forgetting that the function is not vectorized over collections.","solutions":["Coerce the input with str(value) before calling","Handle None/missing values before calling escape_regex","For multiple strings, loop or use the expression API on a Series/DataFrame column","Add an isinstance(value, str) guard in calling code"],"exampleFix":"// before\nescaped = pl.escape_regex(user_input)  # may be None\n// after\nescaped = pl.escape_regex(user_input) if isinstance(user_input, str) else None","handlingStrategy":"type-guard","validationCode":"if not isinstance(pattern, str):\n    raise TypeError(f'expected str for escape_regex, got {type(pattern).__name__}')","typeGuard":"def is_str_or_none(x) -> bool:\n    return x is None or isinstance(x, str)","tryCatchPattern":"try:\n    escaped = pl.escape_regex(pattern)\nexcept TypeError as e:\n    logger.warning('escape_regex got non-str: %s', e)\n    escaped = None","preventionTips":["Validate user-supplied patterns are str before use","Decode bytes with explicit encoding before regex escaping","Coalesce None/missing values to a default string or skip early"],"tags":["polars","regex","type-error"],"backgroundTag":"type-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}