From 977ed43741423f960710bdb246d21b98d58b2fbd Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 22 Feb 2020 13:03:11 +0100 Subject: [PATCH] Don't duplicate docstrings of pyplot-level cmap setters. Dynamically generating these docstrings takes ~40us, which is negligible compared to the import time. flake8 had to be updated, as older versions incorrectly warned about the lack of blank lines between the one-liners. --- lib/matplotlib/pyplot.py | 240 +++++--------------------------- requirements/testing/flake8.txt | 2 +- tools/boilerplate.py | 16 +-- 3 files changed, 36 insertions(+), 222 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 120a888a4183..af3b1299665c 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -1978,11 +1978,10 @@ def colormaps(): def _setup_pyplot_info_docstrings(): """ - Generate the plotting docstring. + Setup the docstring of `plotting` and of the colormap-setting functions. - These must be done after the entire module is imported, so it is - called from the end of this module, which is generated by - boilerplate.py. + These must be done after the entire module is imported, so it is called + from the end of this module, which is generated by boilerplate.py. """ commands = get_plot_commands() @@ -2018,6 +2017,15 @@ def _setup_pyplot_info_docstrings(): ] plotting.__doc__ = '\n'.join(lines) + for cm_name in colormaps(): + if cm_name in globals(): + globals()[cm_name].__doc__ = f""" + Set the colormap to {cm_name!r}. + + This changes the default colormap as well as the colormap of the current + image if there is one. See ``help(colormaps)`` for more information. + """ + ## Plotting part 1: manually generated functions and wrappers ## @@ -2979,211 +2987,25 @@ def yscale(value, **kwargs): # Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def autumn(): - """ - Set the colormap to "autumn". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("autumn") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def bone(): - """ - Set the colormap to "bone". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("bone") +def autumn(): set_cmap('autumn') +def bone(): set_cmap('bone') +def cool(): set_cmap('cool') +def copper(): set_cmap('copper') +def flag(): set_cmap('flag') +def gray(): set_cmap('gray') +def hot(): set_cmap('hot') +def hsv(): set_cmap('hsv') +def jet(): set_cmap('jet') +def pink(): set_cmap('pink') +def prism(): set_cmap('prism') +def spring(): set_cmap('spring') +def summer(): set_cmap('summer') +def winter(): set_cmap('winter') +def magma(): set_cmap('magma') +def inferno(): set_cmap('inferno') +def plasma(): set_cmap('plasma') +def viridis(): set_cmap('viridis') +def nipy_spectral(): set_cmap('nipy_spectral') -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def cool(): - """ - Set the colormap to "cool". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("cool") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def copper(): - """ - Set the colormap to "copper". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("copper") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def flag(): - """ - Set the colormap to "flag". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("flag") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def gray(): - """ - Set the colormap to "gray". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("gray") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def hot(): - """ - Set the colormap to "hot". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("hot") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def hsv(): - """ - Set the colormap to "hsv". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("hsv") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def jet(): - """ - Set the colormap to "jet". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("jet") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def pink(): - """ - Set the colormap to "pink". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("pink") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def prism(): - """ - Set the colormap to "prism". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("prism") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def spring(): - """ - Set the colormap to "spring". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("spring") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def summer(): - """ - Set the colormap to "summer". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("summer") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def winter(): - """ - Set the colormap to "winter". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("winter") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def magma(): - """ - Set the colormap to "magma". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("magma") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def inferno(): - """ - Set the colormap to "inferno". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("inferno") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def plasma(): - """ - Set the colormap to "plasma". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("plasma") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def viridis(): - """ - Set the colormap to "viridis". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("viridis") - - -# Autogenerated by boilerplate.py. Do not edit as changes will be lost. -def nipy_spectral(): - """ - Set the colormap to "nipy_spectral". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("nipy_spectral") - _setup_pyplot_info_docstrings() diff --git a/requirements/testing/flake8.txt b/requirements/testing/flake8.txt index 9716a81c22c2..b5277c656632 100644 --- a/requirements/testing/flake8.txt +++ b/requirements/testing/flake8.txt @@ -1,5 +1,5 @@ # Extra pip requirements for the GitHub Actions flake8 build -flake8>=3.7 +flake8>=3.8 pydocstyle<4.0 flake8-docstrings diff --git a/tools/boilerplate.py b/tools/boilerplate.py index 749209ba1414..98840d1f09f2 100644 --- a/tools/boilerplate.py +++ b/tools/boilerplate.py @@ -57,17 +57,7 @@ def {name}{signature}: return gcf().{called_name}{call} """ -# Used for colormap functions -CMAP_TEMPLATE = AUTOGEN_MSG + ''' -def {name}(): - """ - Set the colormap to "{name}". - - This changes the default colormap as well as the colormap of the current - image if there is one. See ``help(colormaps)`` for more information. - """ - set_cmap("{name}") -''' +CMAP_TEMPLATE = "def {name}(): set_cmap({name!r})\n" # Colormap functions. class value_formatter: @@ -310,6 +300,8 @@ def boilerplate_gen(): yield generate_function(name, f'Axes.{called_name}', template, sci_command=cmappable.get(name)) + yield AUTOGEN_MSG + yield '\n' cmaps = ( 'autumn', 'bone', @@ -335,7 +327,7 @@ def boilerplate_gen(): for name in cmaps: yield CMAP_TEMPLATE.format(name=name) - yield '\n' + yield '\n\n' yield '_setup_pyplot_info_docstrings()'