8000 Generalize validation that pyplot commands are documented · matplotlib/matplotlib@009bfab · GitHub
[go: up one dir, main page]

Skip to content

Commit 009bfab

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 009bfab

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``matplotlib.pyplot.get_plot_commands``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
... is pending to be deprecated. This is considered internal and no end-user
5+
should need it.

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import difflib
2-
import re
32

43
import numpy as np
54
import subprocess
@@ -367,10 +366,41 @@ def test_doc_pyplot_summary():
367366
if not pyplot_docs.exists():
368367
pytest.skip("Documentation sources not available")
369368

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

0 commit comments

Comments
 (0)
0