{"record":{"id":"fc62040c9ce5398a","repo":"pytest-dev/pytest","slug":"invalid-regex-e-pattern-r-e","errorCode":null,"errorMessage":"Invalid regex {e.pattern!r}: {e}","messagePattern":"Invalid regex (.+?): (.+?)","errorType":"exception","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/__init__.py","lineNumber":2320,"sourceCode":"        message = re.escape(message)\n    if module and escape:\n        module = re.escape(module) + r\"\\Z\"\n    if lineno_:\n        try:\n            lineno = int(lineno_)\n            if lineno < 0:\n                raise ValueError(\"number is negative\")\n        except ValueError as e:\n            raise UsageError(\n                error_template.format(error=f\"invalid lineno {lineno_!r}: {e}\")\n            ) from None\n    else:\n        lineno = 0\n    try:\n        re.compile(message)\n        re.compile(module)\n    except re.error as e:\n        raise UsageError(\n            error_template.format(error=f\"Invalid regex {e.pattern!r}: {e}\")\n        ) from None\n    return action, message, category, module, lineno\n\n\ndef _resolve_warning_category(category: str) -> type[Warning]:\n    \"\"\"\n    Copied from warnings._getcategory, but changed so it lets exceptions (specially ImportErrors)\n    propagate so we can get access to their tracebacks (#9218).\n    \"\"\"\n    __tracebackhide__ = True\n    if not category:\n        return Warning\n\n    if \".\" not in category:\n        import builtins as m\n\n        klass = category","sourceCodeStart":2302,"sourceCodeEnd":2338,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/config/__init__.py#L2302-L2338","documentation":"Raised by parse_warning_filter() when the message or module field of a warning filter is not a valid regular expression. After parsing the fields, pytest compiles both the message and module patterns with re.compile(); if either fails, a re.error is caught and re-raised as UsageError with the pattern and error.","triggerScenarios":"Providing a warning filter where the message or module segment contains an invalid regex. Example: filterwarnings = [\"ignore:'(::DeprecationWarning\"] — the message pattern has an unbalanced parenthesis. When escape=True (the default for config-provided filters), special chars are escaped, but raw CLI flags (-W) are not escaped.","commonSituations":"Using unescaped special regex characters in a CLI -W flag's message or module field. Malformed regex from a typo. Copying a filter string that was intended for a different regex engine.","solutions":["Fix the regex syntax in the message or module field","For config-provided filters, pytest auto-escapes the message and module (escape=True), so literal special chars are usually safe","For CLI -W flags, escape regex metacharacters manually or simplify to a plain substring"],"exampleFix":"# before (CLI flag, not auto-escaped)\npytest -W \"ignore:(unbalanced:DeprecationWarning\"\n\n# after\npytest -W \"ignore:\\(balanced\\):DeprecationWarning\"","handlingStrategy":"validation","validationCode":"import re\ndef validate_warning_filter_regex(arg: str) -> bool:\n    parts = arg.split(':')\n    # Pad to 5 fields\n    while len(parts) < 5:\n        parts.append('')\n    message, module = parts[1], parts[3]\n    try:\n        re.compile(message)\n        re.compile(module)\n        return True\n    except re.error:\n        return False","typeGuard":null,"tryCatchPattern":"from pytest import UsageError\ntry:\n    parse_warning_filter(arg, escape=True)\nexcept UsageError as e:\n    print(f\"Invalid regex in filter: {e}\")","preventionTips":["For config-provided filters, pytest auto-escapes message/module so literal text is safe","For CLI -W flags, escape regex metacharacters or use simple substrings","Test regex patterns with re.compile before adding to config"],"tags":["pytest","config","warnings","filterwarnings","regex"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}