Python | Warnings | Temporarily Suppressing Warnings














































Python | Warnings | Temporarily Suppressing Warnings



If you are writing a code that is under test and it will raise a warning for eg. Deprecated function but do not want to see the warning, then it is possible to suppress the warning using the catch_warnings context manager.
While within the context manager all warnings will simply be ignored.
This allows you to use known deprecated code without having to see the warning while not suppressing the warning for other code that might not be aware of its use of deprecated code.
Note: This can only be guaranteed In a single-threaded application. If two or more threads use the catch_warnings context manager at the same time, the behavior is undefined.

SAMPLE CODE:
import warnings def fxn(): warnings.warn("deprecated", DeprecationWarning) with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() print("DeprecationWarning is suppressed Temporarily.")

OUTPUT:

        




Comments