On this page
warnings
Prev: venv | Up: stdlib_module | Next: wave
Rel: sys, sysconfig, builtins, dataclasses, contextlib
Ref: docs.python.org — warnings
Tags: #public #python3 #stdlib
Import
import warnings
Overview
Emits, filters, and tests non-fatal runtime warnings (warn, filterwarnings, catch_warnings)—the standard mechanism for signaling deprecated APIs (DeprecationWarning, FutureWarning) or questionable usage without crashing the caller.
Quick Start
import warnings
def old_helper() -> int:
warnings.warn("old_helper() is deprecated; use new_helper()", DeprecationWarning, stacklevel=2)
return 42
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
assert old_helper() == 42
assert len(caught) == 1 and caught[0].category is DeprecationWarning
Implementation Walkthrough
1. Pointing Warnings at the Caller (stacklevel=2)
Always pass stacklevel=2 (or higher for nested helpers) so the warning points to the caller's line rather than the warnings.warn() line inside your library:
import warnings
def deprecated_parse(text: str) -> str:
warnings.warn(
"deprecated_parse() will be removed in 2.0",
DeprecationWarning,
stacklevel=2,
)
return text.strip()
2. Turning Warnings into Errors in CI / Tests
Run tests with python -W error (or filterwarnings = ["error"] in pyproject.toml) to catch deprecations before they become breaking upgrades.
Gotchas & Boundaries
DeprecationWarningis silenced by default outside__main__: UseFutureWarningfor end-user-facing deprecations, or test withpython -W default/-W error.- Always set
stacklevel=2: Without it, users see your internal library filename instead of their own call site. warnings.catch_warnings()is NOT thread-safe because it mutates global warning filters; use it in single-threaded tests only.
Common Functions
- warnings.filterwarnings() —
filterwarnings(action, message='', category=<class 'Warning'>, module='', lineno=0, append=False)— Insert an entry into the list of warnings filters (at the front). - warnings.formatwarning() —
formatwarning(message, category, filename, lineno, line=None)— Function to format a warning the standard way. - warnings.resetwarnings() —
resetwarnings()— Clear the list of warning filters, so that no filters are active. - warnings.showwarning() —
showwarning(message, category, filename, lineno, file=None, line=None)— Hook to write a warning to a file; replace if you like. - warnings.simplefilter() —
simplefilter(action, category=<class 'Warning'>, lineno=0, append=False)— Insert a simple entry into the list of warnings filters (at the front). - warnings.warn() —
warn(...)— Issue a warning, or maybe ignore it or raise an exception. - warnings.warn_explicit() —
warn_explicit(...)— Issue a warning, or maybe ignore it or raise an exception.
Classes & Objects
- warnings.catch_warnings — A context manager that copies and restores the warnings filter upon exiting the context.
- warnings.deprecated — Indicate that a class, function or overload is deprecated.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/abc
- tuts/python3/stdlib_module/antigravity
- tuts/python3/stdlib_module/ast
- tuts/python3/stdlib_module/atexit
- tuts/python3/stdlib_module/builtins
- tuts/python3/stdlib_module/code
- tuts/python3/stdlib_module/codeop
- tuts/python3/stdlib_module/compileall
- tuts/python3/stdlib_module/contextlib
- tuts/python3/stdlib_module/dataclasses
- tuts/python3/stdlib_module/dis
- tuts/python3/stdlib_module/ensurepip
- tuts/python3/stdlib_module/gc
- tuts/python3/stdlib_module/importlib
- tuts/python3/stdlib_module/inspect
- tuts/python3/stdlib_module/keyword
- tuts/python3/stdlib_module/linecache
- tuts/python3/stdlib_module/modulefinder
- tuts/python3/stdlib_module/opcode
- tuts/python3/stdlib_module/pickletools
- tuts/python3/stdlib_module/pkgutil
- tuts/python3/stdlib_module/py_compile
- tuts/python3/stdlib_module/pyclbr
- tuts/python3/stdlib_module/pyexpat
- tuts/python3/stdlib_module/runpy
- tuts/python3/stdlib_module/site
- tuts/python3/stdlib_module/sre_compile
- tuts/python3/stdlib_module/sre_constants
- tuts/python3/stdlib_module/sre_parse
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/symtable
- tuts/python3/stdlib_module/sys
- tuts/python3/stdlib_module/sysconfig
- tuts/python3/stdlib_module/tabnanny
- tuts/python3/stdlib_module/this
- tuts/python3/stdlib_module/token
- tuts/python3/stdlib_module/tokenize
- tuts/python3/stdlib_module/traceback
- tuts/python3/stdlib_module/venv
- tuts/python3/stdlib_module/warnings/catch_warnings
- tuts/python3/stdlib_module/warnings/deprecated
- tuts/python3/stdlib_module/warnings/filterwarnings
- tuts/python3/stdlib_module/warnings/formatwarning
- tuts/python3/stdlib_module/warnings/resetwarnings
- tuts/python3/stdlib_module/warnings/showwarning
- tuts/python3/stdlib_module/warnings/simplefilter
- tuts/python3/stdlib_module/warnings/warn
- tuts/python3/stdlib_module/warnings/warn_explicit
- tuts/python3/stdlib_module/wave
- tuts/python3/stdlib_module/zipapp
- tuts/python3/stdlib_module/zipimport
- tuts/python3/tutorials/intermediate_python_and_software_engineering/error_handling_and_context_managers
Related notes
- tuts/python3/stdlib_module/builtins Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/contextlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sys Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sysconfig Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/venv Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/warnings/catch_warnings Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/warnings/deprecated Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/warnings/filterwarnings Linked here; links to this page; shared tags: #stdlib