Skip to content

Adds plot_exclude_patterns config to selectively disable plot_directive.#31270

Open
billbrod wants to merge 6 commits intomatplotlib:mainfrom
billbrod:sphinxext-optional
Open

Adds plot_exclude_patterns config to selectively disable plot_directive.#31270
billbrod wants to merge 6 commits intomatplotlib:mainfrom
billbrod:sphinxext-optional

Conversation

@billbrod
Copy link

PR summary

I opened a discussion on discourse asking whether it was possible to temporarily disable the plot_directive, and was told by @timhoffm that "There is currently no clean way of doing this. One could add a config value. It’s not important enough that I’d work on this myself, but I’d accept a PR for it." This is my attempt at that change.

Inspired by myst-nb's nb_execution_excludepatterns config option, this allows users to specify in their sphinx configuration a list of patterns to selectively avoid running some plot directives. The goal is to speed up build time when developing, especially when making changes to project documentation. It does this in a simple way, by checking re.match against the output_base for the created plots for all patterns in the list. If any match, we do not run the code, set result = [(code, [])], and continue. Setting result in this way makes it so the appearance in the built docs is the same as if an error was encountered: code is displayed if so configured, no associated plot.

  • I had originally wanted to check against function_name or something more specific, but it looks like that is often not visible to the sphinx extension, especially when building examples found in docstrings (which is my most common use case).
  • I had considered using re.fullmatch against output_base, but it seems to me that the full output_base might be difficult for a user to predict. Using just re.match may be over-zealous, but given that this is intended to speed up builds during development (not during production), false positives seem more acceptable to me.
  • I figure regex match is the best way to match patterns here. I think glob patterns (like in nb_execution_excludepatterns) are most common for filenames, and the use of re.match means that simple matches (e.g., just "plot") will function as a substring check, while still allowing for more complicated logic.
  • I named this plot_exclude_patterns (with the underscore) rather than plot_excludepatterns (which would match myst-nb's value) to be more consistent with the naming pattern of the other config values.
  • Having this check where it is feels like it might be inefficient to me, but it's not clear how much of the other plot directive code can be skipped without causing other problems. Happy to move it somewhere else if it belongs in a different location!

AI Disclosure

No LLMs were used in this contribution.

PR checklist

  • [N/A] "closes #0000" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New Features and API Changes are noted with a directive and release note
  • Documentation complies with general and docstring guidelines
    • I think the only place to edit is the module-level docstring in plot_directive.py, from grepping for the other config values.

@github-actions
Copy link

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

error will be raised.

plot_exclude_patterns
List of regex patterns to check against to selectively skip plot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My standard question when matching file patterns 😄: Do you really want regular expressions or are wildcard patterns (fnmatch) sufficient/better?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not aware of fnmatch -- that looks sufficient (and easier) to me! I'll update the PR to use that instead.


plot_exclude_patterns
List of regex patterns to check against to selectively skip plot
directives. If any pattern matches the name of the file containing the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is filename enough or do we need a relative path?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh relative path might be better. That increases the chance of false positive matches, but makes it easier to do a simple "disable all plot directives found in the API documentation", which is nice. I'll do that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@melissawm melissawm moved this to Waiting for author in First Time Contributors Mar 11, 2026
@billbrod
Copy link
Author

Pushed changes following @timhoffm suggestions.

Some questions this raised for me:

  • Should there be examples of showing some matches in the user-facing docs somewhere? Given that "matching" works differently for fnmatch and regex (though fnmatch behaves the same as glob, right?), an example or two might make expected behavior more obvious. If so, does this belong right in the docstring of plot_directive.py or is there somewhere else?
  • Related to the above, using fnmatch means that you'll have to use a lot of wildcards. This is fine, but took me a second to realize as I switched from regex. Maybe that will come naturally to users who aren't first thinking in regex, but I wanted to flag it.

@billbrod
Copy link
Author

The failing tests look unrelated to my changes, should I try and fix them?

@timhoffm
Copy link
Member

Yes the failing tests are unrelated.

On "a lot of wildcards": As mentioned above, you may also want to check https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.full_match and https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.match, which may be more convenient than fnmatch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Waiting for author

Development

Successfully merging this pull request may close these issues.

2 participants