Skip to main content

warnings

The warnings module raises non-fatal diagnostics distinct from exceptions. The filter list controls which warnings are shown, ignored, or escalated into errors.

Source-of-record: Lib/warnings.py, Python/_warnings.c, warnings docs.

API​

FunctionBehaviour
warn(message, category=UserWarning, stacklevel=1, source=None)Issue a warning.
warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None, source=None)Without stack lookup.
showwarning(message, category, filename, lineno, file=None, line=None)Hook (override to redirect).
formatwarning(message, category, filename, lineno, line=None)Format for showwarning.

Filter spec​

action:message:category:module:lineno
ActionEffect
"default"Print once per location.
"error"Raise instead of warning.
"ignore"Drop.
"always"Print every time.
"module"Print once per module.
"once"Print once globally.
"all"Alias for "always".

Filter management​

FunctionBehaviour
filterwarnings(action, message='', category=Warning, module='', lineno=0, append=False)Add a filter.
simplefilter(action, category=Warning, lineno=0, append=False)Concise variant.
resetwarnings()Clear all filters and registries.
catch_warnings(*, record=False, action=None, category=Warning, lineno=0, append=False)Context manager that snapshots filters.
warnings.filtersActive filter list.

Categories​

ClassUse
WarningBase.
UserWarningDefault if no category given.
DeprecationWarningDeprecation aimed at code authors.
PendingDeprecationWarningFuture deprecation.
SyntaxWarningDubious syntax that still parses.
RuntimeWarningRuntime feature issues.
FutureWarningBehaviour change in a future release.
ImportWarningIssues during import.
UnicodeWarningEncoding issues.
BytesWarningComparing bytes and str.
ResourceWarningUnclosed resources.
EncodingWarningImplicit default encoding (PEP 597).

-W command-line​

python -W <spec> applies a filter at startup. PYTHONWARNINGS=spec does the same via env.

Gopy status​

AreaState
Filter language and registryComplete.
All categoriesComplete.
catch_warningsComplete.
-W and PYTHONWARNINGSComplete.

Reference​

  • CPython 3.14: warnings.
  • Lib/warnings.py, Python/_warnings.c.
  • module/warnings/, module/_warnings/. gopy port.