Sphinx Autodoc skip member from docstring

12,116

Solution 1

You can now (as of version 0.6) use :exclude-members: to exclude specific members from the documentation:

The directives supporting member documentation also have a exclude-members option that can be used to exclude single member names from documentation, if all members are to be documented.

New in version 0.6.

Source: http://www.sphinx-doc.org/en/stable/ext/autodoc.html

In your specific case, you would add :exclude-members: log into your .rst file.

Solution 2

There doesn't appear to be any easy way to do this.

As a workaround, you can try something like this in your RST file:

.. autoclass:: StatusUpdateAdapter
   :members: methodA, methodB

But that requires listing out all the methods you do want to document by hand, which could be quite laborious. It also probably doesn't interact well with :inherited-members:, if you're using that.

Another option is to put a docstring on every method you want documented, but no docstring on the log() method, then (if necessary) use :no-undoc-members:. This is obviously no good if you plan on documenting your internal interfaces or not documenting your public interfaces.

Finally, Autodoc skips anything whose name begins with an underscore unless configured otherwise (:private-members:), so if you use an underscore-prefixed name, the method will not appear. Underscore-prefixing indicates a private interface under PEP 8, which may or may not match up with your intentions. This could also create backwards compatibility headaches in an established codebase.

Solution 3

I'm not sure how to do that with a docstring, but you can declare the function/method 'protected' with a prepended underscore. Sphinx will not pull that function/method in.

def _log(self, *args, **kwargs):
     pass

Solution 4

You can use :meta private: so that Sphinx considers the method as private and it will be hidden if you configure Sphinx to hide private methods.

Share:
12,116

Related videos on Youtube

Colton Leekly-Winslow
Author by

Colton Leekly-Winslow

Updated on June 14, 2022

Comments

  • Colton Leekly-Winslow
    Colton Leekly-Winslow about 2 years

    I am documenting a class with Sphinx and simple want to skip one of the classes members:

    class StatusUpdateAdapter(logging.LoggerAdapter):
        """
        """
        def __init__(self, status_update_func, logger, extra={}):
            """
            """
            pass
    
        def log(self, *args, **kwargs):
            pass
    

    How can I cause sphinx NOT to document the log member? I would like to do this in the StatusUpdateAdapter or log docstring's if possible.

    • GergelyPolonkai
      GergelyPolonkai about 9 years
      I also need this. I already tried to include :exclude-members: in the docstring with no luck. Have you found a solution since then?
    • Colton Leekly-Winslow
      Colton Leekly-Winslow about 9 years
      @GergelyPolonkai Nope, no luck on this one yet
  • E. T.
    E. T. about 5 years
    This is not really answering the question, is it? Is there a way to do this via docstring? Hardcoding a wide variety of internal members on a bunch of different classes into the sphinx config isn't really practical for bigger projects
  • Joao Coelho
    Joao Coelho almost 5 years
    Didn't answer the exact question being asked, but answered the question I had :)
  • S3DEV
    S3DEV about 3 years
    However, this leaves this method undocumented in the source code. Which might (likely will) reap the wrath of future maintainers.
  • gerrit
    gerrit over 2 years
    That doesn't seem to work. The method still ends up in the sphinx docs.