{"record":{"id":"b5f6bf2bcf6466a4","repo":"RustPython/RustPython","slug":"warnings-showwarning-must-be-set-to-a-function-o","errorCode":null,"errorMessage":"warnings.showwarning() must be set to a function or method","messagePattern":"warnings\\.showwarning\\(\\) must be set to a function or method","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_py_warnings.py","lineNumber":227,"sourceCode":"                  f'allocation traceback\\n')\n    return s\n\n\n# Keep a reference to check if the function was replaced\n_showwarning_orig = showwarning\n\n\ndef _showwarnmsg(msg):\n    \"\"\"Hook to write a warning to a file; replace if you like.\"\"\"\n    try:\n        sw = _wm.showwarning\n    except AttributeError:\n        pass\n    else:\n        if sw is not _showwarning_orig:\n            # warnings.showwarning() was replaced\n            if not callable(sw):\n                raise TypeError(\"warnings.showwarning() must be set to a \"\n                                \"function or method\")\n\n            sw(msg.message, msg.category, msg.filename, msg.lineno,\n               msg.file, msg.line)\n            return\n    _wm._showwarnmsg_impl(msg)\n\n\n# Keep a reference to check if the function was replaced\n_formatwarning_orig = formatwarning\n\n\ndef _formatwarnmsg(msg):\n    \"\"\"Function to format a warning the standard way.\"\"\"\n    try:\n        fw = _wm.formatwarning\n    except AttributeError:\n        pass","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_py_warnings.py#L209-L245","documentation":"When a warning is emitted, _py_warnings._showwarnmsg reads warnings.showwarning; if it was replaced and is not callable (None, a string, a number), TypeError is raised at display time. The check applies only when the current showwarning differs from the original implementation, so any non-callable monkeypatch of warnings triggers it lazily on the next warn(), far from the assignment that caused it.","triggerScenarios":"`warnings.showwarning = None` (a common but wrong attempt to silence warnings) followed by any `warnings.warn(...)`; assigning a logging format string or config dict to warnings.showwarning.","commonSituations":"Libraries trying to suppress warnings by clobbering showwarning; test harnesses monkeypatching warnings without restoring; serialized/restored module state that loses the function reference.","solutions":["To silence warnings use the supported filter API: `warnings.filterwarnings('ignore')` - never assign to showwarning","If you must replace it, assign a callable with signature (message, category, filename, lineno, file=None, line=None)","Save and restore the original via warnings.catch_warnings() so state never leaks between tests"],"exampleFix":"# before\nimport warnings\nwarnings.showwarning = None       # later: TypeError at warn() time\nwarnings.warn('deprecated')\n\n# after\nwarnings.filterwarnings('ignore')  # supported way to silence\nwarnings.warn('deprecated')\n\n# or a proper replacement:\nimport logging\ndef _sw(message, category, filename, lineno, file=None, line=None):\n    logging.getLogger('py.warnings').warning('%s:%s: %s: %s', filename, lineno, category.__name__, message)\nwarnings.showwarning = _sw","handlingStrategy":"validation","validationCode":"import warnings\n\ndef showwarning_usable() -> bool:\n    return callable(getattr(warnings, 'showwarning', None))","typeGuard":null,"tryCatchPattern":"try:\n    warnings.warn(msg)\nexcept TypeError:\n    warnings.showwarning = warnings._showwarning_orig  # restore default\n    warnings.warn(msg)","preventionTips":["Use filterwarnings, not assignment, to suppress warnings","When monkeypatching, restore in finally or use warnings.catch_warnings","Assert callable(warnings.showwarning) in test teardown"],"tags":["warnings","showwarning","monkeypatching","typeerror"],"backgroundTag":"warnings-hook-misconfiguration","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}