Search code examples
pythonclasspython-sphinxautodoc

Sphinx can't cope with function assignment inside class definition


I'm using Sphinx together with autodoc and numpydoc extensions to generate a documentation of a pretty complex package. I run into one problem inside that auto-generation:

Let's say I have a package with two modules calculator.py and math.py.

Inside the calculator.py, I have definded a class, which assigns a function from the math.py module as a standard variable:

class Calculation(object):
    def some_calculation(some_variable,calculate=math.multiplication)

In my sphinx documentation, I include this class using the autoclass extension similar to

.. autoclass:: some_package.calculator.Calculation
    :members:

In my documentation, the function now shows up as a pointer, rather than the name:

some_calculation(some_variable,calculate=<function multiplication at 0x2b8882ef7f50>)

Is this something, I can change in the Sphinx configuration, so that my output looks like:

some_calculation(some_variable,calculate=math.multiplication) 

?


Solution

  • Add a docstring to some_calculation where the first line is the signature that you want:

    class Calculation(object):
        def some_calculation(self, some_variable, calculate=math.multiplication):
            """some_calculation(some_variable, calculate=math.multiplication)
    
               More text...
            """
            ...
    

    Reference: http://sphinx-doc.org/ext/autodoc.html#confval-autodoc_docstring_signature