Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

frankharkins
Copy link
Member

@frankharkins frankharkins commented Sep 18, 2024

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.

.. plot::
   :include-source:
   :nofigs:

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.

.. plot::
   :include-source:
   :nofigs:
   :context: reset

   from qiskit import QuantumCircuit

.. plot::
   :include-source:
   :nofigs:
   :context:

   qc = QuantumCircuit(1)

Advantages:

  • No new tools: we already use .. plot:: in lots of places across the docs
  • Flexibility: Used like this, the directive just checks the code runs, and won't fail if there are meaningless deviations in the output.

Potential problems:

  • The term plot may be confusing to contributors since we're not plotting anything.
  • It's a bit noisy repeating the same options (: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

@frankharkins frankharkins changed the title Use .. plot:: directive to test code examples [Suggestion] Use .. plot:: directive to test code examples Sep 18, 2024
@coveralls
Copy link

coveralls commented Sep 18, 2024

Pull Request Test Coverage Report for Build 11571340391

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 17 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.02%) to 88.669%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 92.73%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 11526575270: -0.02%
Covered Lines: 74890
Relevant Lines: 84460

💛 - Coveralls

@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @enavarro51
  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia
  • @levbishop
  • @mtreinish
  • @nkanazawa1989
  • @t-imamichi

Eric-Arellano
Eric-Arellano previously approved these changes Sep 24, 2024
Copy link
Collaborator

@Eric-Arellano Eric-Arellano left a 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.

qiskit/qpy/__init__.py Outdated Show resolved Hide resolved
qiskit/qpy/__init__.py Show resolved Hide resolved
qiskit/visualization/__init__.py Outdated Show resolved Hide resolved
Copy link
Member

@jakelishman jakelishman left a 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?

Comment on lines 26 to +28
QuantumCircuit:

.. code-block::
.. code-block:: python
Copy link
Member

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?

Copy link
Member Author

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.

qiskit/pulse/schedule.py Show resolved Hide resolved
.. code-block::
.. code-block:: python
Copy link
Member

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.)

Copy link
Member Author

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.

Comment on lines 85 to 92
.. 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()
Copy link
Member

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.

Copy link
Member Author

@frankharkins frankharkins Oct 28, 2024

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/transpiler/passes/scheduling/padding/pad_delay.py Outdated Show resolved Hide resolved
Comment on lines 92 to 97
.. plot::
:nofigs:

# This block is hidden from readers. It's cleanup code.
from pathlib import Path
Path("new_hist.png").unlink()
Copy link
Member

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.

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add doc testing to Python client repositories
5 participants