Skip to content

Commit

Permalink
Add inline classes to example docs (#598)
Browse files Browse the repository at this point in the history
We need to support these better in the new Qiskit docs, so it's helpful
to see how qiskit-sphinx-theme handles things. See
Qiskit/documentation#199 for more context.
Eric-Arellano authored Mar 1, 2024
1 parent 9fd9765 commit 418231e
Showing 7 changed files with 107 additions and 0 deletions.
51 changes: 51 additions & 0 deletions example_docs/api_example/inline_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from __future__ import annotations


class SimpleInlineClass:
"""This is a simple class that does not have
any methods or attributes.
It only has the class description and constructor.
Many classes in Qiskit docs are simple like this.
"""

def __init__(self, arg1: str) -> None:
self.arg1 = arg1


class InlineClassWithMethods:
"""This class is more involved.
Note how the methods and attributes are rendered and
indented when this class is inlined on the docs page.
"""

CLASS_ATTRIBUTE: str = "An important part of any API."

@property
def interest_rate(self):
pass

def method1(self) -> int:
"""A simple method."""
return 0

def method2(
self, arg1: int | float, arg2: list[InlineClassWithMethods], description: str
) -> tuple[int, InlineClassWithMethods]:
"""A method with a lot of args!
This method will use a Hamiltonian to reach quantum advantage. Hamilton: great play & the
secret to quantum computing. What a polymath.
Args:
arg1: All numbers accepted.
arg2: A list of other instances, although these will be discarded.
description: If your description is too boring or too cryptic, this program
will crash your computer.
"""
return [0, self]


class CustomException(Exception):
"""See how exceptions render too."""
1 change: 1 addition & 0 deletions example_docs/docs/index.rst
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ A site to test out styling for qiskit-sphinx-theme.
:maxdepth: 2

Autodoc <sphinx_guide/autodoc>
Inline classes <sphinx_guide/inline_classes>

.. toctree::
:caption: Special elements
49 changes: 49 additions & 0 deletions example_docs/docs/sphinx_guide/inline_classes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
==============
Inline classes
==============

This is a page to test out how we render classes and functions
included inline in the page. This is common with module pages.


Important APIs
==============

Every time you use this program, you'll want to create an instance of
:class:`api_example.inline_classes.SimpleInlineClass`. It has a simple interface:

.. autoclass:: api_example.inline_classes.SimpleInlineClass

It can be useful to use free functions rather than the class:

.. autofunction:: api_example.my_function
:noindex:

Sometimes, you even need to use a really complex class!

.. autoclass:: api_example.inline_classes.InlineClassWithMethods
:no-members:
:show-inheritance:

.. rubric:: Attributes

.. autoattribute:: CLASS_ATTRIBUTE
.. autoattribute:: interest_rate

.. rubric:: Methods

.. automethod:: method1
.. automethod:: method2


Warning: exceptions
-------------------

The above APIs might raise an exception! Be careful!

.. autoexception:: api_example.inline_classes.CustomException

Other prose
===========

Blah blah blah.
6 changes: 6 additions & 0 deletions tests/js/tests.js
Original file line number Diff line number Diff line change
@@ -85,6 +85,12 @@ test.describe("api docs", () => {
const content = page.locator("div.article-container");
await expect(content).toHaveScreenshot();
});

test("inline classes", async ({ page }) => {
await page.goto("sphinx_guide/inline_classes.html");
const content = page.locator("div.article-container");
await expect(content).toHaveScreenshot();
});
});

test("tables align with qiskit.", async ({ page }) => {
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 418231e

Please sign in to comment.