Python Glossary Русский

all Python 3warningsfunction

warnings.warn_explicit()

issuing warnings

A low-level warn() where the file, line and module of the warning are given explicitly — for warnings about configs, templates and other non-Python files.

Syntax

warnings.warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None, source=None)

warn() works out the location from the call stack, while warn_explicit takes filename and lineno from its arguments — so a config loader can point at a line in settings.ini rather than at its own source. Without registry every warning counts as the first: 'default' and 'module' behave like 'always', and repeats in a loop are not collapsed. Pass the same registry dict each time to suppress repeats.

Examples

import warnings
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter('default')
    for _ in range(2):
        warnings.warn_explicit('ключ устарел', UserWarning, 'settings.ini', 12)
print(len(caught), caught[0].filename, caught[0].lineno)  # → 2 settings.ini 12
import warnings
registry = {}
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter('default')
    for _ in range(2):
        warnings.warn_explicit('ключ устарел', UserWarning, 'settings.ini', 12,
                               registry=registry)
print(len(caught))  # → 1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy