{"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":"exception","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/functions/escape_regex.py#L9-L30","documentation":"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.","triggerScenarios":"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.","commonSituations":"Optional config values that arrive as None; numeric identifiers passed where text was expected; undecoded bytes from file/network input.","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'))"],"exampleFix":"# before\npl.escape_regex(user.get('needle'))  # None -> TypeError\n\n# after\nneedle = user.get('needle', '')\npl.escape_regex(str(needle))","handlingStrategy":"validation","validationCode":"if not isinstance(s, str):\n    s = str(s)  # or raise early with context about the bad value\nescaped = pl.escape_regex(s)","typeGuard":"def is_plain_str(s) -> bool:\n    return isinstance(s, str)","tryCatchPattern":null,"preventionTips":["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"],"tags":["polars","escape-regex","type-check","input-validation"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}