Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include specific special-methods in sphinx

Tags:

I have a bunch of classes which use "special-methods":

class Foo(object):
   "Foo docstring"

   attr1 = "Attribute!" #: first attribute
   attr2 = "Another Attribute!" #: second attribute

   def __init__(self):
       self.x = 12

   def say_hello(self):
       """
       say_hello(self) -> None

       Issue a friendly greeting.
       """
       print "Hello! x is {0}".format(self.x)

   def __contains__(self,other):
       """Implement ``other in self``"""
       return other == self.x

now I would like to generate html documentation for this using Sphinx and autodoc. How do I tell Sphinx to document __contains__? I tried adding

autodoc_default_flags = ['members', 'undoc-members', 'special-members']

to conf.py, but that also included __dict__ which I definitely don't want.

Currently, the relevant portions of the myproject.rst file look like:

.. automodule:: myproject.foomodule
    :members:
    :undoc-members:
    :show-inheritance:

edit adding

.. automodule:: myproject.foomodule
    :members:
    :undoc-members:
    :show-inheritance:

.. automethod:: myproject.foomodule.Foo.__contains__

does add documentation of that method, but in a separate section -- Not as part of the Foo class documentation.

like image 871
mgilson Avatar asked Apr 09 '13 13:04

mgilson


2 Answers

You can add:

:special-members:
:exclude-members: __dict__,__weakref__

To the .rst file in order to show special members, except __dict__ and __weakref__

like image 126
arutaku Avatar answered Sep 28 '22 07:09

arutaku


What worked for me is adding the ".. automethod:: methodName"

directive in the docstring of the class, instead of doing it in the .rst file.

So, you can change "Foo docstring" to

"""
Foo docstring

.. automethod:: __contains__
"""
like image 43
rb3363392 Avatar answered Sep 28 '22 07:09

rb3363392