10000 Generalize validation that pyplot commands are documented · matplotlib/matplotlib@2c0f960 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c0f960

Browse files
committed
Generalize validation that pyplot commands are documented
Until now, the test made some exclusions (_NON_PLOT_COMMANDS) and reqired all functions to be documented in a single autosummary block. This change ensures the documentation of the _NON_PLOT_COMMANDS and it allows the commands to be spread across arbitrary many autosummary sections. This is in preparation of regrouping the pyplot commands similar to the Axes documentation. This also pending deprecates `pyplot.get_plot_commands`, which should not be a public function. I'm defensive by using pending, because if `get_plot_commands` is used somewhere, that's most likely some downstream lib and we want to give them time to adapt.
1 parent cada8fb commit 2c0f960

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

lib/matplotlib/pyplot.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,20 +2017,23 @@ def thetagrids(angles=None, labels=None, fmt=None, **kwargs):
20172017
return lines, labels
20182018

20192019

2020-
_NON_PLOT_COMMANDS = {
2021-
'connect', 'disconnect', 'get_current_fig_manager', 'ginput',
2022-
'new_figure_manager', 'waitforbuttonpress'}
2023-
2024-
2020+
@_api.deprecated("3.7", pending=True)
20252021
def get_plot_commands():
20262022
"""
20272023
Get a sorted list of all of the plotting commands.
20282024
"""
2025+
_NON_PLOT_COMMANDS = {
2026+
'connect', 'disconnect', 'get_current_fig_manager', 'ginput',
2027+
'new_figure_manager', 'waitforbuttonpress'}
2028+
return (name for name in _get_pyplot_commands()
2029+
if name not in _NON_PLOT_COMMANDS)
2030+
2031+
2032+
def _get_pyplot_commands():
20292033
# This works by searching for all functions in this module and removing
20302034
# a few hard-coded exclusions, as well as all of the colormap-setting
20312035
# functions, and anything marked as private with a preceding underscore.
2032-
exclude = {'colormaps', 'colors', 'get_plot_commands',
2033-
*_NON_PLOT_COMMANDS, *colormaps}
2036+
exclude = {'colormaps', 'colors', 'get_plot_commands', *colormaps}
20342037
this_module = inspect.getmodule(get_plot_commands)
20352038
return sorted(
20362039
name for name, obj in globals().items()

lib/matplotlib/tests/test_pyplot.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,41 @@ def test_doc_pyplot_summary():
367367
if not pyplot_docs.exists():
368368
pytest.skip("Documentation sources not available")
369369

370-
lines = pyplot_docs.read_text()
371-
m = re.search(r':nosignatures:\n\n(.*?)\n\n', lines, re.DOTALL)
372-
doc_functions = set(line.strip() for line in m.group(1).split('\n'))
373-
plot_commands = set(plt.get_plot_commands())
370+
def extract_documented_functions(lines):
371+
"""
372+
Return a list of all functions that are mentioned in autosummary
373+
blocks contain in *lines*.
374+
375+
An autosummary block looks like this::
376+
377+
.. autosummary::
378+
:toctree: _as_gen
379+
:template: autosummary.rst
380+
:nosignatures:
381+
382+
plot
383+
plot_date
384+
385+
"""
386+
functions = []
387+
in_autosummary = False
388+
for line in lines:
389+
if not in_autosummary:
390+
if line.startswith(".. autosummary::"):
391+
in_autosummary = True
392+
else:
393+
if not line or line.startswith(" :"):
394+
# empty line or autosummary parameter
395+
continue
396+
if not line[0].isspace():
397+
in_autosummary = False
398+
continue
399+
functions.append(line.strip())
400+
return functions
401+
402+
lines = pyplot_docs.read_text().split("\n")
403+
doc_functions = set(extract_documented_functions(lines))
404+
plot_commands = set(plt._get_pyplot_commands())
374405
missing = plot_commands.difference(doc_functions)
375406
if missing:
376407
raise AssertionError(

0 commit comments

Comments
 (0)
0