-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Suggestion] Use .. plot::
directive to test code examples
#13179
base: main
Are you sure you want to change the base?
Conversation
.. plot::
directive to test code examples.. plot::
directive to test code examples
Pull Request Test Coverage Report for Build 11571340391Details
💛 - Coveralls |
1f78a56
to
56c8b95
Compare
56c8b95
to
1540feb
Compare
One or more of the following people are relevant to this code:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! I like this approach. Sphinx is already such a learning curve that I think there's value in limiting the number of tools.
It's be useful to put the impact on CI's build from this change by looking at the Azure logs.
Co-authored-by: Eric Arellano <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing all this, and thanks for the care you took in putting in clean-up code. That said, I'm still a bit nervous about merging any docs-build change that causes it to dirty the working directory while it's going, even if it's ephemeral - those stray files can easily get accidentally left behind, or the dev might cancel the docs build partway through, or so on.
Are the side-effect-y blocks something we can just leave as .. code-block::
, or is there a neater way we might trick them into not actually writing to the filesystem?
QuantumCircuit: | ||
|
||
.. code-block:: | ||
.. code-block:: python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a space remove here, but there's still leading indentation - is that what we expect, and is it displaying correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs builds before and after render identically (the conversion script currently ignores the specified language, see Qiskit/documentation#2162). I think I removed the space to keep indentation a multiple of 2.
.. code-block:: | ||
.. code-block:: python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this one stayed as-is?
(Fwiw, python
should be the default language for the Qiskit build, so I think it ought to have been highlighting correctly anyway.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I wasn't sure how to make a pulse qobj and decided to leave it since this class is deprecated.
qiskit/qpy/__init__.py
Outdated
.. plot:: | ||
:nofigs: | ||
:context: | ||
|
||
# This block is hidden from readers. It's cleanup code. | ||
from pathlib import Path | ||
Path("bell.qpy").unlink() | ||
Path("twenty_bells.qpy").unlink() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not super wild about the docs build actually writing out dirty files into the working directory while we're going.
Could we either leave the above QPY stuff as .. code-block
, or alternatively use a prior hidden context
block to override open
to be something that secretly returns io.BytesIO()
objects (potentially wrapped to avoid their context managers clearing them up prematurely). Obviously that's a complete Python crime if it were real code, but if the context objects are localised and don't leak beyond this file, it'll keep the docs build entirely in-memory, not touching the file-system with temporaries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a good point. The bytes object idea is a good one and I think it should work. Otherwise, I think it's fine not to test difficult cases like these.
EDIT: Switched to bytestream in 4370d52
qiskit/visualization/__init__.py
Outdated
.. plot:: | ||
:nofigs: | ||
|
||
# This block is hidden from readers. It's cleanup code. | ||
from pathlib import Path | ||
Path("new_hist.png").unlink() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar thing here - it feels better to me not to have the docs build actually write out to the filesystem as a side effect that we have to tidy up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I think this one is simple enough that we don't need to test it.
Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: Jake Lishman <[email protected]>
Summary
This PR switches code examples to use Matplotlib's
.. plot::
directive. This runs those code examples during documentation builds, failing the build if any code examples raise an exception.Details
We replace code blocks with the plot directive, using the
:nofigs:
option. Any code inside the directive is run at build time. This doesn't change the appearance of the rendered docs.We can also use the
:context:
option to share state between code examples. This is helpful to avoid duplicating imports and for interspersing code examples with prose.Advantages:
.. plot::
in lots of places across the docsPotential problems:
plot
may be confusing to contributors since we're not plotting anything.:include-source:
,:nofig:
) everywhere.Impact on CI:
It's hard to tell since there's a lot of variation in the docs build step (20-30mins), but this doesn't seem to add noticeable time to CI (the two builds so far took 31 and 23 minutes).
Fixes Qiskit/documentation#1893