Pregunta

I have a class factory that generates a class with a function bound to another class. When a class inherits from the generated class it gains the appropriate function. Now I want to document the generated class' function else one would have to inspect the class factory to see what the function does or the class factory's documentation must describe all of the generated class' functions.

Class factory:

def greeter(name):
    """Generate a greeter for the given name."""
    class Greeter(object):
        def greet(self):
            print('Hello {}.'.format(name))
        greet.__doc__ = "Greet someone by the name of {!r}".format(name)
    return Greeter

It's usage:

class GreetArthur(greeter('Arthur')):
    pass

GreetArthur().greet()  # Prints 'Hello Arthur.'

Now when this gets documented by Sphinx it is clear that GreetArthur inherits from Greeter, but Greeter is never documented because it's generated. From the documentation it is thus not clear that GreetArthur has a function greet.

greeter(name)
    Generate a greeter for the given name.

class GreetArthur
    Bases: Greeter

Desired result:

greeter(name)
    Generate a greeter for the given name.

class GreetArthur
    Bases: Greeter

    greet()
        Greet someone by the name of 'Arthur'.
¿Fue útil?

Solución

Looks like the :inherited-members: does the trick. It works both for :automodule: and :autoclass:.

.. automodule:: greet
    :members:
    :inherited-members:

or

.. autoclass:: greet.GreetArthur
    :members:
    :inherited-members:

This will result in:

class greet.GreetArthur
    greet()
        Greet someone by the name of 'Arthur'.

Otros consejos

According to the sphynx autodoc documentation an empty :members: option for autoclass looks like it should do the trick. If not then you may need to expose the Greeter implementation at the module level instead of hiding it inside a factory closure so that sphynx can see it when importing your module to look for docstrings.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top