{"record":{"id":"75c94cf3300b4e63","repo":"python/cpython","slug":"message-must-be-a-string","errorCode":null,"errorMessage":"message must be a string","messagePattern":"message must be a string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_py_warnings.py","lineNumber":269,"sourceCode":"    return _wm._formatwarnmsg_impl(msg)\n\n\ndef filterwarnings(action, message=\"\", category=Warning, module=\"\", lineno=0,\n                   append=False):\n    \"\"\"Insert an entry into the list of warnings filters (at the front).\n\n    'action' -- one of \"error\", \"ignore\", \"always\", \"all\", \"default\", \"module\",\n                or \"once\"\n    'message' -- a regex that the warning message must match\n    'category' -- a class that the warning must be a subclass of\n    'module' -- a regex that the module name must match\n    'lineno' -- an integer line number, 0 matches all warnings\n    'append' -- if true, append to the list of filters\n    \"\"\"\n    if action not in {\"error\", \"ignore\", \"always\", \"all\", \"default\", \"module\", \"once\"}:\n        raise ValueError(f\"invalid action: {action!r}\")\n    if not isinstance(message, str):\n        raise TypeError(\"message must be a string\")\n    if not isinstance(category, type) or not issubclass(category, Warning):\n        raise TypeError(\"category must be a Warning subclass\")\n    if not isinstance(module, str):\n        raise TypeError(\"module must be a string\")\n    if not isinstance(lineno, int):\n        raise TypeError(\"lineno must be an int\")\n    if lineno < 0:\n        raise ValueError(\"lineno must be an int >= 0\")\n\n    if message or module:\n        import re\n\n    if message:\n        message = re.compile(message, re.I)\n    else:\n        message = None\n    if module:\n        module = re.compile(module)","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_py_warnings.py#L251-L287","documentation":"Raised by warnings.filterwarnings when its message argument is not a str. The message is a regex pattern that the library compiles itself (re.compile(message, re.I)), so passing an already-compiled pattern, bytes, or None is rejected up front with TypeError('message must be a string').","triggerScenarios":"warnings.filterwarnings('ignore', message=re.compile('deprecated')) — passing a compiled regex; message=b'...'; message=None explicitly (the default is '' so None passed positionally also trips it).","commonSituations":"Developers 'pre-compiling' the regex for performance; mixing up argument order so category/message land in the wrong slots; building filter tuples programmatically where a compiled pattern or None leaks into the message field.","solutions":["Pass the raw pattern string: message=r'deprecated api'","Unwrap compiled patterns before the call: getattr(message, 'pattern', message)","If you meant 'match everything', pass the empty string '' (default) rather than None"],"exampleFix":"# before\nimport re\nwarnings.filterwarnings('ignore', message=re.compile('deprecated'))  # TypeError\n\n# after\nwarnings.filterwarnings('ignore', message=r'deprecated')","handlingStrategy":"validation","validationCode":"def to_pattern_str(msg):\n    \"\"\"Accept a raw string or a compiled regex, return the pattern string.\"\"\"\n    return getattr(msg, 'pattern', msg)\n\nimport warnings\nwarnings.filterwarnings('ignore', message=to_pattern_str(user_msg))","typeGuard":"def is_pattern_str(msg) -> bool:\n    return isinstance(msg, str)","tryCatchPattern":null,"preventionTips":["Pass regex pattern strings; filterwarnings compiles them for you","Unwrap compiled patterns via getattr(p, 'pattern', p) when values come from mixed sources","Use '' (default) for 'match any message', not None"],"tags":["warnings","validation","regex","filters"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}