documenting module-level constants

480 views
Skip to first unread message

Zbigniew Jędrzejewski-Szmek

unread,
Feb 10, 2013, 10:16:05 PM2/10/13
to sphinx-users
Hi,
I'm trying to document a C extension module containing a number of
constants. I'd like both their names and values to appear in the
sphinx HTML output. I'm using sphinx.autodoc extension, but it ignores
the constants.

The constants are defined as instances of uuid.UUID.
I have:
.. automodule:: systemd.id128
:members:
:undoc-members:

This shows everything I want except the constants.

If I list one of the constants like:

.. autoattribute: SD_MESSAGES_COREDUMP

then it is appears in the output.

Is there a way to avoid listing all the constants and telling
autodoc to the job?

Thanks,
Zbyszek

PS. FWIW, pydoc3.3 systemd.id128 shows:
...
DATA
SD_MESSAGE_COREDUMP = UUID('fc2e22bc-6ee6-47b6-b907-29ab34a250b1')
SD_MESSAGE_FORWARD_SYSLOG_MISSED = ...

Daniel Svensson

unread,
Mar 6, 2016, 5:50:53 PM3/6/16
to sphinx-users
On Monday, February 11, 2013 at 4:16:05 AM UTC+1, Zbigniew Jędrzejewski-Szmek wrote:
Hi,
I'm trying to document a C extension module containing a number of
constants. I'd like both their names and values to appear in the
sphinx HTML output. I'm using sphinx.autodoc extension, but it ignores
the constants.

The constants are defined as instances of uuid.UUID.
I have:
.. automodule:: systemd.id128
   :members:
   :undoc-members:

This shows everything I want except the constants.

I'm in the same boat here. I saw that you went with the autoattribute approach, thus duplicating your code file in the documentation. It should be possible to avoid that by using the Python 'ast' module from within a Sphinx extension to emit something like:

.. attribute:: mymodule.MYCONSTANT
   :annotation: = somevalue

...for each sympol found via the ast module, thus avoiding the duplication. The ast module is quite simple to work with so it wouldn't add much code, and be resilient to change.

There is one problem though, I have no idea what extension code is needed to create the above from say, for example:

.. myautoconst:: mymodule

...and I'm having a hard time to make sense of the documentation. The goal here should be, for me at least, to have the attributes cross-referable via `MYCONSTANT` and the default_role='any'. Any help would be very much appreciated. Either to get this working, or to fix sphinx to actually automatically document the consts.
Reply all
Reply to author
Forward
0 new messages