Skip to content

Commit ef2d7f4

Browse files
authored
Merge pull request #36 from pganssle/example_variants
Add variants example documentation
2 parents 1abe3d1 + 01e6c99 commit ef2d7f4

File tree

7 files changed

+97
-1
lines changed

7 files changed

+97
-1
lines changed

changelog.d/36.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a documentation page demonstrating the use of the sphinx-autodocs-variants extension for displaying function groups.

docs/conf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
# Insert the project root dir as the first element in the PYTHONPATH.
3030
# This lets us ensure that the source package is imported, and that its
3131
# version is used.
32+
sys.path.insert(0, os.path.abspath('.'))
3233
sys.path.insert(0, os.path.join(project_root, 'src'))
34+
sys.path.append(os.path.join(project_root, 'docs/examples'))
3335

3436
import variants
37+
import examples
3538

3639
# -- General configuration ---------------------------------------------
3740

@@ -40,7 +43,7 @@
4043

4144
# Add any Sphinx extension module names here, as strings. They can be
4245
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
43-
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']
46+
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinx_autodoc_variants']
4447

4548
# Add any paths that contain templates here, relative to this directory.
4649
templates_path = ['_templates']

docs/examples/__init__.py

Whitespace-only changes.

docs/examples/example_variants.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import variants
2+
3+
4+
@variants.primary
5+
def primary_func(x, y):
6+
"""
7+
This is the primary function. Its docstring should be shown under the
8+
primary function documentation.
9+
"""
10+
11+
12+
@primary_func.variant('onearg')
13+
def primary_func(x):
14+
"""This is the ``onearg`` variant, it only takes one argument."""
15+
16+
17+
@primary_func.variant('threearg')
18+
def primary_func(x, y, z):
19+
"""This is the ``threearg`` variant, it takes three arguments."""
20+
21+
22+
class VariantMethodsClass(object):
23+
@variants.primary
24+
def primary_method(self, x, y):
25+
"""
26+
This is the primary method, its docstring should be shown under the
27+
primary method documentation.
28+
"""
29+
30+
@primary_method.variant('onearg')
31+
def primary_method(self, x):
32+
"""This is the ``onearg`` variant, it takes one argument."""
33+
34+
def normal_method(self, a, b):
35+
"""This is a normal method, it has no variants"""

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Contents:
1313
readme
1414
installation
1515
usage
16+
Variant Autodoc (Sphinx) <sphinx>
1617

1718
.. toctree::
1819
:maxdepth: 1

docs/requirements-docs.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Sphinx
22
sphinx_rtd_theme>=0.3.1
3+
sphinx-autodoc-variants

docs/sphinx.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
=====================
2+
Documentation example
3+
=====================
4+
5+
Using the `sphinx_autodoc_variants <https://github.com/python-variants/sphinx_autodoc_variants>`_ extension, primary and variant functions can be grouped together as a single function group.
6+
7+
Functions
8+
---------
9+
10+
With the ``sphinx_autodoc_variants`` extension enabled in ``conf.py``, function groups at the module level will automatically be grouped together by the ``automodule`` directive. To explicitly document a function group, use the ``autoprimary_function`` directive:
11+
12+
.. code-block:: rst
13+
14+
.. autoprimary_function:: examples.example_variants.primary_func
15+
:members:
16+
17+
Which generates:
18+
19+
.. autoprimary_function:: examples.example_variants.primary_func
20+
:members:
21+
:noindex:
22+
23+
24+
Methods
25+
-------
26+
For a class containing function group methods, the ``autoclass`` directive works, so:
27+
28+
.. code-block:: rst
29+
30+
.. autoclass:: examples.example_variants.VariantMethodsClass
31+
:members:
32+
33+
Resulting in:
34+
35+
.. autoclass:: examples.example_variants.VariantMethodsClass
36+
:members:
37+
:noindex:
38+
39+
40+
As with functions, individual method groups can be documented using the ``autoprimary_method`` directive:
41+
42+
.. code-block:: rst
43+
44+
.. autoprimary_method:: examples.example_variants.VariantMethodsClass.primary_method
45+
:members:
46+
47+
Which generates:
48+
49+
.. autoprimary_method:: examples.example_variants.VariantMethodsClass.primary_method
50+
:members:
51+
52+
53+
.. .. automodule:: examples.example_variants
54+
.. :members:
55+
.. :undoc-members:

0 commit comments

Comments
 (0)