{"record":{"id":"12b9dc7b228de1ac","repo":"python/cpython","slug":"invalid-action-action-r","errorCode":null,"errorMessage":"invalid action: {action!r}","messagePattern":"invalid action: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_py_warnings.py","lineNumber":267,"sourceCode":"            return fw(msg.message, msg.category,\n                      msg.filename, msg.lineno, msg.line)\n    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","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_py_warnings.py#L249-L285","documentation":"Raised by warnings.filterwarnings when the action string is not one of the recognized filter actions: 'error', 'ignore', 'always', 'all', 'default', 'module', 'once'. The function validates its arguments up front because filter entries are consumed later by the matching engine, which cannot interpret unknown actions. 'all' is an alias of 'always'; there is no 'warn', 'hide', or 'once/module' style shorthand.","triggerScenarios":"warnings.filterwarnings(action='suppress'/'hide'/'warn'/'ignore-once') — any misspelled or invented action string; also actions read from a config file and forwarded verbatim; actions with trailing whitespace or different casing ('Ignore') since the check is case-sensitive.","commonSituations":"Typo'd action names in test-suite bootstrap (pytest.ini-style warning config translated manually); actions loaded from YAML/JSON settings; snippets written for other warning systems (e.g. Ruby's $VERBOSE vocabulary); uppercase or padded values from user input.","solutions":["Use one of the seven valid actions: error, ignore, always, all, default, module, once","Strip/normalize user- or config-supplied actions: action.strip().lower() before calling","Validate actions against the set when parsing config files, and reject early with a clear message"],"exampleFix":"# before\nwarnings.filterwarnings('ignore-once', category=DeprecationWarning)  # ValueError\n\n# after\nwarnings.filterwarnings('once', category=DeprecationWarning)","handlingStrategy":"validation","validationCode":"VALID_ACTIONS = {'error', 'ignore', 'always', 'all', 'default', 'module', 'once'}\n\ndef safe_filterwarnings(action, *args, **kwargs):\n    action = str(action).strip().lower()\n    if action not in VALID_ACTIONS:\n        raise ValueError(f'{action!r} is not a valid action; choose from {sorted(VALID_ACTIONS)}')\n    import warnings\n    return warnings.filterwarnings(action, *args, **kwargs)","typeGuard":"def is_valid_action(action) -> bool:\n    return isinstance(action, str) and action.strip().lower() in {\n        'error', 'ignore', 'always', 'all', 'default', 'module', 'once'}","tryCatchPattern":null,"preventionTips":["Normalize config-sourced actions with .strip().lower() before calling filterwarnings","Validate action names at config-load time so bad values fail loudly and early","Remember 'all' == 'always'; there is no 'hide'/'suppress'/'warn' action"],"tags":["warnings","validation","filters","configuration"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}