From 9b130e01c11da0f8e57b58ad328835f3561abc28 Mon Sep 17 00:00:00 2001 From: hannah Date: Thu, 12 Oct 2023 18:06:37 -0400 Subject: [PATCH] sphinx directive warning filter --- lib/matplotlib/sphinxext/plot_directive.py | 38 +++++++++++++------ lib/matplotlib/tests/tinypages/some_plots.rst | 8 ++++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/sphinxext/plot_directive.py b/lib/matplotlib/sphinxext/plot_directive.py index 65b25fb913a5..dfbc5d1804e7 100644 --- a/lib/matplotlib/sphinxext/plot_directive.py +++ b/lib/matplotlib/sphinxext/plot_directive.py @@ -78,6 +78,12 @@ figure. This overwrites the caption given in the content, when the plot is generated from a file. +``:filter-warning:`` : str + When specified, will ignore warnings that match the input string, which is + a warnings filter `message regex `_ string. + +.. _msg: https://docs.python.org/3/library/warnings.html#the-warnings-filter + Additionally, this directive supports all the options of the `image directive `_, except for ``:target:`` (since plot will add its own target). These include @@ -177,6 +183,7 @@ import sys import textwrap import traceback +import warnings from docutils.parsers.rst import directives, Directive from docutils.parsers.rst.directives.images import Image @@ -221,6 +228,10 @@ def _option_format(arg): return directives.choice(arg, ('python', 'doctest')) +def _option_string(arg): + return arg if arg.strip() else False + + def mark_plot_labels(app, document): """ To make plots referenceable, we need to move the reference from the @@ -271,7 +282,8 @@ class PlotDirective(Directive): 'context': _option_context, 'nofigs': directives.flag, 'caption': directives.unchanged, - } + 'filter-warning': _option_string, + } def run(self): """Run the plot directive.""" @@ -854,16 +866,20 @@ def run(arguments, content, options, state_machine, state, lineno): # make figures try: - results = render_figures(code=code, - code_path=source_file_name, - output_dir=build_dir, - output_base=output_base, - context=keep_context, - function_name=function_name, - config=config, - context_reset=context_opt == 'reset', - close_figs=context_opt == 'close-figs', - code_includes=source_file_includes) + with warnings.catch_warnings(record=True) as w: + if msg := options.get('filter-warning', False): + warnings.filterwarnings('ignore', message=msg) + + results = render_figures(code=code, + code_path=source_file_name, + output_dir=build_dir, + output_base=output_base, + context=keep_context, + function_name=function_name, + config=config, + context_reset=context_opt == 'reset', + close_figs=context_opt == 'close-figs', + code_includes=source_file_includes) errors = [] except PlotError as err: reporter = state.memo.reporter diff --git a/lib/matplotlib/tests/tinypages/some_plots.rst b/lib/matplotlib/tests/tinypages/some_plots.rst index dd1f79892b0e..3bebbb231e0a 100644 --- a/lib/matplotlib/tests/tinypages/some_plots.rst +++ b/lib/matplotlib/tests/tinypages/some_plots.rst @@ -174,3 +174,11 @@ Plot 21 is generated via an include directive: Plot 22 uses a different specific function in a file with plot commands: .. plot:: range6.py range10 + +Plot 23 filters a missing glyph warning + +.. plot:: + :filter-warning: Glyph + + plt.text(.5, .5, "Hello 🙃 World!") + \ No newline at end of file