← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:em25egjr

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.},
}

Markdown

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.

All artifact files (JSON index)

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.