Filtering warnings by message - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Filtering warnings by message (/thread-35299.html) |
Filtering warnings by message - 22_alias - Oct-18-2021 I'm struggling to understand how to use the message argument in warnings.filterwarnings . I am specifically trying to silent a deprecation warning from matplotlib . Here is what I have:import warnings from matplotlib import pyplot fig, ax = pyplot.subplots(1, 1)The following command gives a warning: t = ax.is_first_col() I have been trying to filter by type (DeprecationWarning), or module (matplotlib, matplotlib/..*, matplotlib*) which didn't work# make sure all the warnings show at every instance: warnings.filterwarnings('always') warnings.filterwarnings('ignore', category=DeprecationWarning) t = ax.is_first_col()
warnings.filterwarnings('ignore', module='matplotlib') t = ax.is_first_col()
warnings.filterwarnings('ignore', module='matplotlib*') t = ax.is_first_col()
warnings.filterwarnings('ignore', module='matplotlib/..*') t = ax.is_first_col() Then I have been trying to filter the messages:warnings.filterwarnings('ignore', message='.*MatplotlibDeprecationWarning.*') t = ax.is_first_col()
warnings.filterwarnings('always') warnings.filterwarnings('ignore', message='.*MatplotlibDeprecationWarning.*') warnings.warn('some text MatplotlibDeprecationWarning some other text\n another line')The latter command doesn't result in any warning at all, so the message is indeed filtered according to the regexp, but not the one from matplotlib . The command warnings.filterwarnings('filter') works but it filters everything which is not desirable.I don't understand why the message filter doesn't work. What should I put to filter that specific warning? Note: I am not explicitly using that is_first_col function, this comes from another module that I am using.
RE: Filtering warnings by message - Gribouillis - Oct-18-2021 It turns out that MatplotlibDeprecationWarning is not a subclass of DeprecationWarning >>> import warnings >>> from matplotlib import pyplot, MatplotlibDeprecationWarning >>> warnings.filterwarnings('ignore', category=MatplotlibDeprecationWarning) >>> fig, ax = pyplot.subplots(1, 1) >>> t = ax.is_first_col() >>> RE: Filtering warnings by message - 22_alias - Oct-19-2021 Great, that was unexpected Thanks for the answer. I'm still curious about how to filter by message as this would allow for finer filtering. PS. Checked: >>> import warnings >>> from matplotlib import pyplot, MatplotlibDeprecationWarning >>> warnings.filterwarnings('always') >>> ig, ax = pyplot.subplots(1, 1) Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway. >>> t = ax.is_first_col() <stdin>:1: MatplotlibDeprecationWarning: The is_first_col function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use ax.get_subplotspec().is_first_col() instead. >>> warnings.filterwarnings('ignore', category=MatplotlibDeprecationWarning) >>> t = ax.is_first_col() >>> warnings.filterwarnings('always') >>> t = ax.is_first_col() <stdin>:1: MatplotlibDeprecationWarning: The is_first_col function was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use ax.get_subplotspec().is_first_col() instead. >>> RE: Filtering warnings by message - Gribouillis - Oct-19-2021 The problem is that the MatplotlibDeprecationWarning's message starts with a newline which is not matched by the dot in a regular expression. You can set the DOTALL flag by using (?s) in the regular expression. The following filter workswarnings.filterwarnings('ignore', '(?s).*Matplot',) |