numpy.testing.suppress_warnings#
- classnumpy.testing.suppress_warnings(forwarding_rule='always')[source]#
Context manager and decorator doing much the same as
warnings.catch_warnings.However, it also provides a filter mechanism to work aroundhttps://bugs.python.org/issue4180.
This bug causes Python before 3.4 to not reliably show warnings againafter they have been ignored once (even within catch_warnings). Itmeans that no “ignore” filter can be used easily, since followingtests might need to see the warning. Additionally it allows easierspecificity for testing warnings and can be nested.
- Parameters:
- forwarding_rulestr, optional
One of “always”, “once”, “module”, or “location”. Analogous tothe usual warnings module filter mode, it is useful to reducenoise mostly on the outmost level. Unsuppressed and unrecordedwarnings will be forwarded based on this rule. Defaults to “always”.“location” is equivalent to the warnings “default”, match by exactlocation the warning warning originated from.
Notes
Filters added inside the context manager will be discarded againwhen leaving it. Upon entering all filters defined outside acontext will be applied automatically.
When a recording filter is added, matching warnings are stored in the
logattribute as well as in the list returned byrecord.If filters are added and the
modulekeyword is given, thewarning registry of this module will additionally be cleared whenapplying it, entering the context, or exiting it. This could causewarnings to appear a second time after leaving the context if theywere configured to be printed once (default) and were alreadyprinted before the context was entered.Nesting this context manager will work as expected when theforwarding rule is “always” (default). Unfiltered and unrecordedwarnings will be passed out and be matched by the outer level.On the outmost level they will be printed (or caught by anotherwarnings context). The forwarding rule argument can modify thisbehaviour.
Like
catch_warningsthis context manager is not threadsafe.Examples
With a context manager:
withnp.testing.suppress_warnings()assup: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@supdefsome_function():# do something which causes a warning in np.ma.corepass
Methods