René's URL Explorer Experiment


Title: plot_directive is confused by include directives, part 2 (context option) · Issue #20523 · matplotlib/matplotlib · GitHub

Open Graph Title: plot_directive is confused by include directives, part 2 (context option) · Issue #20523 · matplotlib/matplotlib

X Title: plot_directive is confused by include directives, part 2 (context option) · Issue #20523 · matplotlib/matplotlib

Description: Bug summary Whenever a file b.rst is included in a.rst via the RST include directive, Sphinx rebuilds the a.html page; but in plot_directive, a.rst is not considered 'out of date' (as judged by the out_of_date() function), because the mo...

Open Graph Description: Bug summary Whenever a file b.rst is included in a.rst via the RST include directive, Sphinx rebuilds the a.html page; but in plot_directive, a.rst is not considered 'out of date' (as judged by the...

X Description: Bug summary Whenever a file b.rst is included in a.rst via the RST include directive, Sphinx rebuilds the a.html page; but in plot_directive, a.rst is not considered 'out of date' (as judge...

Opengraph URL: https://github.com/matplotlib/matplotlib/issues/20523

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"plot_directive is confused by include directives, part 2 (context option)","articleBody":"### Bug summary\r\n\r\nWhenever a file `b.rst` is included in `a.rst` via the RST `include` directive, Sphinx rebuilds the `a.html` page; but in plot_directive, `a.rst` is not considered 'out of date' (as judged by the `out_of_date()` function), because the modification time of `a.rst` was not changed.\r\n\r\nThis discrepancy is part of the reason why #17860 exists; and while working on it (at PR #20374) I discovered also that figures with the `:context:` option set will get confused. A specific example is shown below. It's quite hard to encounter in real life but it is directly relevant to matplotlib's tests as there is a very similar construct in plots 6-9 of matplotlib's `test_sphinxext.py`.\r\n\r\n### Code for reproduction\r\n\r\n**conf.py**\r\n```python\r\nextensions = ['matplotlib.sphinxext.plot_directive']\r\nexclude_patterns = ['_build']\r\n```\r\n\r\n**index.rst**\r\n```rst\r\nIndex\r\n=====\r\n\r\n.. toctree::\r\n   \r\n   a\r\n   b\r\n```\r\n\r\n**a.rst**\r\n```rst\r\nFile A\r\n======\r\n\r\nIt's important that the first plot produces an image, and also sets a variable\r\nvia ``:context:``.\r\n\r\n\r\n.. plot::\r\n   :context:\r\n\r\n   plt.plot(range(2))\r\n   a = 1  \r\n\r\nThe second plot must not use ``:context:``. It doesn't necessarily have to\r\nproduce an image. The important thing is that it must close the figure from the\r\nprevious plot, so that the third plot doesn't actually produce an image (if\r\nfigures aren't closed, then the third plot will reuse the same image from the\r\nfirst plot).\r\n\r\n.. plot::\r\n\r\n   plt.plot(range(3))\r\n\r\nThe third plot must try to use a variable previously saved in `:context:`` and\r\nmust not produce an image.\r\n\r\n\r\n.. plot::\r\n   :context:\r\n\r\n   assert a == 1\r\n\r\nLastly we include another file.\r\n\r\n.. include:: b.rst\r\n```\r\n\r\n**b.rst**\r\n```rst\r\nFile B\r\n======\r\n\r\nThis can be anything.\r\n```\r\n\r\n\r\n### Steps to reproduce\r\n\r\n1. Put the four files above in a directory and `cd` into it.\r\n2. Build the docs the first time using `sphinx-build -b html . ./_build/html`.\r\n3. Modify `b.rst` in any way.\r\n4. Build the docs again.\r\n\r\n### Actual outcome\r\n\r\nThe third plot in `a.rst` throws an error.\r\n\r\n```\r\n/Users/yongrenjie/test/rst/a.rst:21: WARNING: Exception occurred in plotting a-3\r\n from /Users/yongrenjie/test/rst/a.rst:\r\nTraceback (most recent call last):\r\n  File \"/Users/yongrenjie/progs/matplotlib/lib/matplotlib/sphinxext/plot_directive.py\", line 497, in _run_code\r\n    exec(code, ns)\r\n  File \"\u003cstring\u003e\", line 1, in \u003cmodule\u003e\r\nNameError: name 'a' is not defined\r\n```\r\n\r\nThe reason for this, as suggested above, is because of the `out_of_date()` function. When `sphinx-build` is invoked again, Sphinx decides that both `a.rst` and `b.rst` must be recompiled. Now:\r\n\r\n - Plot 1 is not considered out of date, because the image file already exists and `a.rst` was not modified. So the code is never run and `a` is never saved to the context.\r\n - Plot 2 is there to ensure that figures are closed prior to Plot 3, so that Plot 3 never generates an image file.\r\n - Plot 3 is considered out of date, because there is no image file that corresponds to it. Thus it is run again, and doesn't see `a` in the context, hence the warning.\r\n\r\n### Expected outcome\r\n\r\nThere shouldn't be any errors.\r\n\r\nOne easy way to accomplish this is to make sure that Sphinx re-runs all code snippets which are context-dependent, whenever a file is recompiled. That is, if a plot directive has :context: on, then the code should always be considered out of date regardless of the file modification times.\r\n\r\nThis will lead to some excessive regeneration of plots whenever included files are modified. For example, in the above code, whenever `b.rst` is modified, Plots 1 and 3 will always be re-created, even if `a.rst` is untouched. But IMO this is more sensible behaviour than the current bug. It would also be in line with what happens if *any* part of `a.rst` is modified, including the text outside the plot directives: all the plots in `a.rst` would be re-created.\r\n\r\nThis doesn't change the case where neither `a.rst` nor `b.rst` are modified, because in that case Sphinx will never attempt to recompile either file and plot_directive will never be called.","author":{"url":"https://github.com/yongrenjie","@type":"Person","name":"yongrenjie"},"datePublished":"2021-06-26T19:48:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/20523/matplotlib/issues/20523"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:640156dd-be73-cd4e-14c4-e3f28a7c0542
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idED62:24214A:1C3B446:26763D5:6A5400AB
html-safe-noncee2f9334e20f02d952fb537e07f28b39965197c28761c32d0af3e53c61a232f26
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDYyOjI0MjE0QToxQzNCNDQ2OjI2NzYzRDU6NkE1NDAwQUIiLCJ2aXNpdG9yX2lkIjoiNDE1MjY0NDIxOTE3MjAyODU4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac5e8abae87e04b75b9119e1c7f6c04f2c880e11bf471584f2f204dff45c929fc2
hovercard-subject-tagissue:930782905
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/20523/issue_layout
twitter:imagehttps://opengraph.githubassets.com/7a30448fbb85522fa3d2a6b20995455edb500ea165fc28d09848f948bec543e4/matplotlib/matplotlib/issues/20523
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/7a30448fbb85522fa3d2a6b20995455edb500ea165fc28d09848f948bec543e4/matplotlib/matplotlib/issues/20523
og:image:altBug summary Whenever a file b.rst is included in a.rst via the RST include directive, Sphinx rebuilds the a.html page; but in plot_directive, a.rst is not considered 'out of date' (as judged by the...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameyongrenjie
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
go-importgithub.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git
octolytics-dimension-user_id215947
octolytics-dimension-user_loginmatplotlib
octolytics-dimension-repository_id1385122
octolytics-dimension-repository_nwomatplotlib/matplotlib
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1385122
octolytics-dimension-repository_network_root_nwomatplotlib/matplotlib
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release07a982c1d40157c619b364352b704c3ce66bb332
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/matplotlib/matplotlib/issues/20523#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F20523
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F20523
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=matplotlib%2Fmatplotlib
Reloadhttps://github.com/matplotlib/matplotlib/issues/20523
Reloadhttps://github.com/matplotlib/matplotlib/issues/20523
Reloadhttps://github.com/matplotlib/matplotlib/issues/20523
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/20523
matplotlib https://github.com/matplotlib
matplotlibhttps://github.com/matplotlib/matplotlib
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/20523
Notifications https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Fork 8.4k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Star 23k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Code https://github.com/matplotlib/matplotlib
Issues 1.1k https://github.com/matplotlib/matplotlib/issues
Pull requests 408 https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality 0 https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
Code https://github.com/matplotlib/matplotlib
Issues https://github.com/matplotlib/matplotlib/issues
Pull requests https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
#20374https://github.com/matplotlib/matplotlib/pull/20374
plot_directive is confused by include directives, part 2 (context option)https://github.com/matplotlib/matplotlib/issues/20523#top
#20374https://github.com/matplotlib/matplotlib/pull/20374
topic: sphinx extensionhttps://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22topic%3A%20sphinx%20extension%22
v3.5.0https://github.com/matplotlib/matplotlib/milestone/59
https://github.com/yongrenjie
yongrenjiehttps://github.com/yongrenjie
on Jun 26, 2021https://github.com/matplotlib/matplotlib/issues/20523#issue-930782905
#17860https://github.com/matplotlib/matplotlib/issues/17860
#20374https://github.com/matplotlib/matplotlib/pull/20374
topic: sphinx extensionhttps://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22topic%3A%20sphinx%20extension%22
v3.5.0https://github.com/matplotlib/matplotlib/milestone/59
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.