Python warnings - issue DeprecationWarning, filter warnings, and turn them into errors in tests
Answer: Issue warnings by calling the warn() function, for example when a program uses an obsolete module. The warnings filter controls whether warnings are ignored, displayed, or turned into errors. Warnings alert the user of a condition that doesn't warrant raising an exception and terminating the program.
Examples
import warnings
def deprecated_api(message):
warnings.warn(message, DeprecationWarning, stacklevel=2)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
deprecated_api("old") # silenced only here
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
deprecated_api("old")
assert issubclass(w[-1].category, DeprecationWarning)
warnings.filterwarnings("error", category=DeprecationWarning, module="myapp")
python -W error::DeprecationWarning app.py
PYTHONWARNINGS=default python app.py
Filter actions
| Action | Matching warnings | |---|---| | "error" | turned into exceptions | | "ignore" | are never printed | | "always" | are always printed | | "default" | print the first occurrence for each location | | "module" | print the first occurrence for each module | | "once" | print only the first occurrence, regardless of location |
Filters are an ordered list; a warning is matched against each entry in turn until a match is found. filterwarnings() and simplefilter() insert entries at the front.
Details
categorydefaults toUserWarning.stacklevel=2makes the warning refer to the caller of your wrapper function rather than the wrapper itself.- Within the
catch_warnings()context manager you can temporarily mutate the warnings filter. Withrecord=Trueit collects warnings for tests. - By default,
DeprecationWarningis ignored except when triggered directly by code in__main__. Test runners should show all warnings for the code under test. warnings.deprecatedis a decorator (3.13+) that marks a class or function as deprecated for runtime warnings and static type checkers.
See also
Claims
- id: C1 claim: category defaults to UserWarning. source: S1 evidence: "it defaults to UserWarning ."
- id: C2 claim: The error action turns warnings into exceptions. source: S1 evidence: "turn matching warnings into exceptions"
- id: C3 claim: warn may ignore the warning or raise an exception. source: S2 evidence: "Issue a warning, or maybe ignore it or raise an exception."
Sources
- [S1] warnings - Warning control. Python docs. https://docs.python.org/3/library/warnings.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/_py_warnings.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/_py_warnings.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, warnings - Warning control; python/cpython (GitHub), Lib/_py_warnings.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, warnings - Warning control; python/cpython (GitHub), Lib/_py_warnings.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python warnings - issue DeprecationWarning, filter warnings, and turn them into errors in tests." iinventedlight Library, iil:note:em25egjr, version of 2026-11-18. https://iinventedlight.com/library/notes/python-warnings/. License: CC-BY-4.0.
@misc{iil_note_em25egjr,
author = {{iinventedlight}},
title = {{Python warnings - issue DeprecationWarning, filter warnings, and turn them into errors in tests}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-warnings/},
note = {Permanent ID iil:note:em25egjr. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-warnings.table.json (table; CC-BY-4.0)
- python-warnings.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-warnings.md.