Python Forum
Filtering warnings by message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtering warnings by message
#1
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()
Error:
<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.
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()
Error:
<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', module='matplotlib')
t = ax.is_first_col()
Error:
<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', module='matplotlib*')
t = ax.is_first_col()
Error:
<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', module='matplotlib/..*')
t = ax.is_first_col()
Error:
<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.
Then I have been trying to filter the messages:
warnings.filterwarnings('ignore', message='.*MatplotlibDeprecationWarning.*')
t = ax.is_first_col()
Error:
<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('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.
Reply
#2
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()
>>>
22_alias likes this post
Reply
#3
Great, that was unexpected Big Grin 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.
>>> 
Reply
#4
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 works
warnings.filterwarnings('ignore', '(?s).*Matplot',)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error UserWarning: Unknown option ssl_ca_certs warnings.warn(str(exc)) Vijayaraj 0 729 Jul-11-2023, 06:01 AM
Last Post: Vijayaraj
  cmake and pip3 install warnings with python 3.10.2 cyrduf 0 1,910 Feb-26-2022, 01:08 PM
Last Post: cyrduf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020