On this page
warnings.warn
Prev: warnings.simplefilter | Up: warnings | Next: warnings.warn_explicit
Rel: warnings.catch_warnings, warnings.deprecated, warnings.filterwarnings, warnings.formatwarning
Ref: docs.python.org — warnings.warn
Tags: #public #python3 #stdlib #warnings
Import
import warnings
from warnings import warn
Signature & Overview
warnings.warn(...)
Issue a warning, or maybe ignore it or raise an exception.
Usage
import warnings
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter('always')
warnings.warn('deprecated param', DeprecationWarning, stacklevel=2)
assert len(caught) == 1
Gotchas
- Always pass
stacklevel=2(or higher for wrappers) so the warning points at the caller's line instead of inside your library.
REPL Inspect
from warnings import warn
help(warn)
Linked from
Related notes
- tuts/python3/stdlib_module/warnings/simplefilter Linked here; links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/warn_explicit Linked here; links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/warnings/catch_warnings Linked here; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/deprecated Linked here; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/filterwarnings Linked here; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/formatwarning Linked here; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/resetwarnings shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/showwarning shared tags: #stdlib, #warnings
- tuts/c/stdlib shared tags: #stdlib