Skip to content

Commit

Permalink
sphinx: render the list of arguments in the correct order (#22)
Browse files Browse the repository at this point in the history
* sphinx: render the list of arguments in the correct order

* add missing dtype
  • Loading branch information
njzjz authored Nov 25, 2022
1 parent cc5efaf commit d8856ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion dargs/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
where `_test_argument` returns an :class:`Argument <dargs.Argument>`. A :class:`list` of :class:`Argument <dargs.Argument>` is also accepted.
"""
import sys
from typing import List

from docutils.parsers.rst import Directive
from docutils.parsers.rst.directives import unchanged
Expand Down Expand Up @@ -61,11 +62,13 @@ def run(self):
if not isinstance(arguments, (list, tuple)):
arguments = [arguments]

rsts = []
for argument in arguments:
if not isinstance(argument, (Argument, Variant)):
raise RuntimeError("The function doesn't return Argument")
rst = argument.gen_doc(make_anchor=True, make_link=True, use_sphinx_domain=True)
self.state_machine.insert_input(rst.split("\n"), "%s:%s" %(module_name, attr_name))
rsts.extend(rst.split("\n"))
self.state_machine.insert_input(rsts, "%s:%s" %(module_name, attr_name))
return []


Expand Down Expand Up @@ -157,3 +160,11 @@ def _test_argument() -> Argument:
),
],
)

def _test_arguments() -> List[Argument]:
"""Returns a list of arguments."""
return [
Argument(name="test1", dtype=int, doc="Argument 1"),
Argument(name="test2", dtype=[float, None], doc="Argument 2"),
Argument(name="test3", dtype=list, doc="Argument 3"),
]
7 changes: 6 additions & 1 deletion docs/sphinx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ Then `dargs` directive will be enabled:
:module: dargs.sphinx
:func: _test_argument
where `_test_argument` returns an :class:`Argument <dargs.Argument>`. A :class:`list` of :class:`Argument <dargs.Argument>` is also accepted. The documentation will be rendered as:
where `_test_argument` returns an :class:`Argument <dargs.Argument>`. The documentation will be rendered as:

.. dargs::
:module: dargs.sphinx
:func: _test_argument

A :class:`list` of :class:`Argument <dargs.Argument>` is also accepted.

.. dargs::
:module: dargs.sphinx
:func: _test_arguments

Cross-referencing Arguments
---------------------------
Expand Down

0 comments on commit d8856ff

Please sign in to comment.