Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Sphinx namedtuple documentation

I'm trying to document namedtuple. When I build the docs I get a warning WARNING: duplicate object description and same empty functions after documented ones. For example: enter image description here

How to delete those aliases? I've already tried this solution, writing few functions to conf.py to create empty properties.

Also, I think it worth to mention that after building I get a note to use :noindex: but I don't understand where should I use it? In my docstring, rst file or somewhere else?

Code example:


File = namedtuple("File", ["path", "size", "extension",
                           "adate", "mdate", "links",
                           "user_owner", "group_owner",
                           "inode", "device", "permissions",
                           "depth"])
"""File attributes.

.. py:attribute:: path

    **-** path to the found file

     .. note::
        depending on command-line arguments can be absolute or relative
...
like image 344
Artem Avatar asked Aug 31 '26 11:08

Artem


1 Answers

I ran into a, I think, much better solution than the accepted answer, just wanted to share it:

Just write this in your conf.py:

# -- Post process ------------------------------------------------------------
import collections
def remove_namedtuple_attrib_docstring(app, what, name, obj, skip, options):
    if type(obj) is collections._tuplegetter:
        return True
    return skip


def setup(app):
    app.connect('autodoc-skip-member', remove_namedtuple_attrib_docstring)

This remove all parameters auto documented with this "Alias for field .." from all NamedTuple classes.

Explanation

This uses the autodoc event autodoc-skip-member which triggers an handler each time it comes across any type of member

  • app.connect('autodoc-skip-member', remove_namedtuple_attrib_docstring) attaches the handler remove_namedtuple_attrib_docstring to the event
  • The handler returns true if the member is a tuplegetter and return the default skipping values otherwise
like image 59
NicolasDg Avatar answered Sep 03 '26 01:09

NicolasDg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!