On this page
  1. warnings.catch_warnings
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

warnings.catch_warnings

Up: warnings | Next: warnings.deprecated
Rel: warnings.deprecated, warnings.filterwarnings, warnings.formatwarning, warnings.resetwarnings
Ref: docs.python.org — warnings.catch_warnings
Tags: #public #python3 #stdlib #warnings


Import

import warnings
from warnings import catch_warnings

Signature & Overview

warnings.catch_warnings(*, record=False, module=None, action=None, category=<class 'Warning'>, lineno=0, append=False)

A context manager that copies and restores the warnings filter upon exiting the context.

Usage

import warnings

with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter('always')
    warnings.warn('heads up', UserWarning)
assert len(caught) == 1 and str(caught[0].message) == 'heads up'

Gotchas

REPL Inspect

from warnings import catch_warnings
help(catch_warnings)

Linked from

Related notes