{"record":{"id":"bb34ded716b8991c","repo":"python/cpython","slug":"unrecognized-action-r-in-warnings-filters-s","errorCode":null,"errorMessage":"Unrecognized action (%r) in warnings.filters:\n %s","messagePattern":"Unrecognized action \\(%r\\) in warnings\\.filters:\n (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Lib/_py_warnings.py","lineNumber":613,"sourceCode":"        if action == \"once\":\n            registry[key] = 1\n            oncekey = (text, category)\n            if _wm.onceregistry.get(oncekey):\n                return\n            _wm.onceregistry[oncekey] = 1\n        elif action in {\"always\", \"all\"}:\n            pass\n        elif action == \"module\":\n            registry[key] = 1\n            altkey = (text, category, 0)\n            if registry.get(altkey):\n                return\n            registry[altkey] = 1\n        elif action == \"default\":\n            registry[key] = 1\n        else:\n            # Unrecognized actions are errors\n            raise RuntimeError(\n                  \"Unrecognized action (%r) in warnings.filters:\\n %s\" %\n                  (action, item))\n\n    # Prime the linecache for formatting, in case the\n    # \"file\" is actually in a zipfile or something.\n    import linecache\n    linecache.getlines(filename, module_globals)\n\n    # Print message and context\n    msg = _wm.WarningMessage(message, category, filename, lineno,\n                             module=module, source=source)\n    _wm._showwarnmsg(msg)\n\n\nclass WarningMessage(object):\n\n    _WARNING_DETAILS = (\"message\", \"category\", \"filename\", \"lineno\", \"file\",\n                        \"line\", \"source\", \"module\")","sourceCodeStart":595,"sourceCodeEnd":631,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_py_warnings.py#L595-L631","documentation":"Raised while a warning is being dispatched when the filter entry selected for it carries an action the matching engine does not recognize. filterwarnings/simplefilter validate actions at insertion time, so this error is a signature that warnings.filters was mutated directly (insert/append of hand-built tuples) with an invalid action string. It surfaces only when a warning actually matches the corrupt entry, making it late and confusing.","triggerScenarios":"warnings.filters.insert(0, ('suppress', re.compile('.*'), UserWarning, re.compile('.*'), 0)) followed by any UserWarning; copying filter tuples from another interpreter with different accepted actions; save/restore of filters across environments where one side accepted non-standard actions.","commonSituations":"Test harnesses and libraries that reach into warnings.filters as a list instead of using the API; pickled/restored filter state; mixed Python versions where a custom shim wrote filters directly; monkeypatching gone wrong.","solutions":["Replace direct list mutation with warnings.filterwarnings()/simplefilter(), which validate","Audit any code that touches warnings.filters for hand-built tuples","If you must write filters directly, restrict actions to error/ignore/always/all/default/module/once","Wrap suspicious blocks in catch_warnings(record=True) to isolate which filter entry is bad"],"exampleFix":"# before\nwarnings.filters.insert(0, ('suppress', None, UserWarning, None, 0))  # later -> RuntimeError\n\n# after\nwarnings.filterwarnings('ignore', category=UserWarning)","handlingStrategy":"validation","validationCode":"VALID_ACTIONS = {'error', 'ignore', 'always', 'all', 'default', 'module', 'once'}\n\ndef validate_filters() -> None:\n    \"\"\"Call before running code that emits warnings.\"\"\"\n    import warnings\n    for i, item in enumerate(warnings.filters):\n        if item[0] not in VALID_ACTIONS:\n            raise ValueError(f'warnings.filters[{i}] has invalid action {item[0]!r}: {item}')","typeGuard":"def has_valid_filters() -> bool:\n    import warnings\n    valid = {'error', 'ignore', 'always', 'all', 'default', 'module', 'once'}\n    return all(item[0] in valid for item in warnings.filters)","tryCatchPattern":"import warnings\ntry:\n    warnings.warn('probe')\nexcept RuntimeError as e:\n    if 'Unrecognized action' in str(e):\n        warnings.filters[:] = [f for f in warnings.filters\n                               if f[0] in {'error', 'ignore', 'always', 'all', 'default', 'module', 'once'}]\n        warnings.warn('probe')\n    else:\n        raise","preventionTips":["Treat warnings.filters as read-mostly; mutate only through filterwarnings/simplefilter","When saving/restoring filters (e.g. in test harnesses), validate the restored entries","Lint for direct warnings.filters access in code review"],"tags":["warnings","filters","internal-api","configuration"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}