class documentation

class suppress_warnings: (source)

View In Hierarchy

Context manager and decorator doing much the same as warnings.catch_warnings.

However, it also provides a filter mechanism to work around https://bugs.python.org/issue4180.

This bug causes Python before 3.4 to not reliably show warnings again after they have been ignored once (even within catch_warnings). It means that no "ignore" filter can be used easily, since following tests might need to see the warning. Additionally it allows easier specificity for testing warnings and can be nested.

Notes

Filters added inside the context manager will be discarded again when leaving it. Upon entering all filters defined outside a context will be applied automatically.

When a recording filter is added, matching warnings are stored in the log attribute as well as in the list returned by record.

If filters are added and the module keyword is given, the warning registry of this module will additionally be cleared when applying it, entering the context, or exiting it. This could cause warnings to appear a second time after leaving the context if they were configured to be printed once (default) and were already printed before the context was entered.

Nesting this context manager will work as expected when the forwarding rule is "always" (default). Unfiltered and unrecorded warnings will be passed out and be matched by the outer level. On the outmost level they will be printed (or caught by another warnings context). The forwarding rule argument can modify this behaviour.

Like catch_warnings this context manager is not threadsafe.

Examples

With a context manager:

with np.testing.suppress_warnings() as sup:
    sup.filter(DeprecationWarning, "Some text")
    sup.filter(module=np.ma.core)
    log = sup.record(FutureWarning, "Does this occur?")
    command_giving_warnings()
    # The FutureWarning was given once, the filtered warnings were
    # ignored. All other warnings abide outside settings (may be
    # printed/error)
    assert_(len(log) == 1)
    assert_(len(sup.log) == 1)  # also stored in log attribute

Or as a decorator:

sup = np.testing.suppress_warnings()
sup.filter(module=np.ma.core)  # module must match exactly
@sup
def some_function():
    # do something which causes a warning in np.ma.core
    pass
Parameters
forwarding_ruleOne of "always", "once", "module", or "location". Analogous to the usual warnings module filter mode, it is useful to reduce noise mostly on the outmost level. Unsuppressed and unrecorded warnings will be forwarded based on this rule. Defaults to "always". "location" is equivalent to the warnings "default", match by exact location the warning warning originated from.
Method __call__ Function decorator to apply certain suppressions to a whole function.
Method __enter__ Undocumented
Method __exit__ Undocumented
Method __init__ Undocumented
Method filter Add a new suppressing filter or apply it if the state is entered.
Method record Append a new recording filter or apply it if the state is entered.
Instance Variable log Undocumented
Method _clear_registries Undocumented
Method _filter Undocumented
Method _showwarning Undocumented
Instance Variable _entered Undocumented
Instance Variable _filters Undocumented
Instance Variable _forwarded Undocumented
Instance Variable _forwarding_rule Undocumented
Instance Variable _orig_show Undocumented
Instance Variable _suppressions Undocumented
Instance Variable _tmp_modules Undocumented
Instance Variable _tmp_suppressions Undocumented
def __call__(self, func): (source)

Function decorator to apply certain suppressions to a whole function.

def __enter__(self): (source)

Undocumented

def __exit__(self, *exc_info): (source)

Undocumented

def __init__(self, forwarding_rule='always'): (source)

Undocumented

def filter(self, category=Warning, message='', module=None): (source)

Add a new suppressing filter or apply it if the state is entered.

Notes

When added within a context, filters are only added inside the context and will be forgotten when the context is exited.

Parameters
category:class, optionalWarning class to filter
message:string, optionalRegular expression matching the warning message.
module:module, optionalModule to filter for. Note that the module (and its file) must match exactly and cannot be a submodule. This may make it unreliable for external modules.
def record(self, category=Warning, message='', module=None): (source)

Append a new recording filter or apply it if the state is entered.

All warnings matching will be appended to the log attribute.

Notes

When added within a context, filters are only added inside the context and will be forgotten when the context is exited.

Parameters
category:class, optionalWarning class to filter
message:string, optionalRegular expression matching the warning message.
module:module, optionalModule to filter for. Note that the module (and its file) must match exactly and cannot be a submodule. This may make it unreliable for external modules.
Returns
listlog - A list which will be filled with all matched warnings.

Undocumented

def _clear_registries(self): (source)

Undocumented

def _filter(self, category=Warning, message='', module=None, record=False): (source)

Undocumented

def _showwarning(self, message, category, filename, lineno, *args, use_warnmsg=None, **kwargs): (source)

Undocumented

_entered: bool = (source)

Undocumented

_filters = (source)

Undocumented

_forwarded = (source)

Undocumented

_forwarding_rule = (source)

Undocumented

_orig_show = (source)

Undocumented

_suppressions: list = (source)

Undocumented

_tmp_modules = (source)

Undocumented

_tmp_suppressions: list = (source)

Undocumented