How to use Sphinx's autodoc to document a class's __init__(self) method?

后端 未结 5 2021
长情又很酷
长情又很酷 2020-12-04 09:49

Sphinx doesn\'t generate docs for __init__(self) by default. I have tried the following:

.. automodule:: mymodule
    :members:

and

5条回答
  •  孤街浪徒
    2020-12-04 10:17

    Over the past years I've written several variants of autodoc-skip-member callbacks for various unrelated Python projects because I wanted methods like __init__(), __enter__() and __exit__() to show up in my API documentation (after all, these "special methods" are part of the API and what better place to document them than inside the special method's docstring).

    Recently I took the best implementation and made it part of one of my Python projects (here's the documentation). The implementation basically comes down to this:

    import types
    
    def setup(app):
        """Enable Sphinx customizations."""
        enable_special_methods(app)
    
    
    def enable_special_methods(app):
        """
        Enable documenting "special methods" using the autodoc_ extension.
    
        :param app: The Sphinx application object.
    
        This function connects the :func:`special_methods_callback()` function to
        ``autodoc-skip-member`` events.
    
        .. _autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html
        """
        app.connect('autodoc-skip-member', special_methods_callback)
    
    
    def special_methods_callback(app, what, name, obj, skip, options):
        """
        Enable documenting "special methods" using the autodoc_ extension.
    
        Refer to :func:`enable_special_methods()` to enable the use of this
        function (you probably don't want to call
        :func:`special_methods_callback()` directly).
    
        This function implements a callback for ``autodoc-skip-member`` events to
        include documented "special methods" (method names with two leading and two
        trailing underscores) in your documentation. The result is similar to the
        use of the ``special-members`` flag with one big difference: Special
        methods are included but other types of members are ignored. This means
        that attributes like ``__weakref__`` will always be ignored (this was my
        main annoyance with the ``special-members`` flag).
    
        The parameters expected by this function are those defined for Sphinx event
        callback functions (i.e. I'm not going to document them here :-).
        """
        if getattr(obj, '__doc__', None) and isinstance(obj, (types.FunctionType, types.MethodType)):
            return False
        else:
            return skip
    

    Yes, there's more documentation than logic :-). The advantage of defining an autodoc-skip-member callback like this over the use of the special-members option (for me) is that the special-members option also enables documentation of properties like __weakref__ (available on all new-style classes, AFAIK) which I consider noise and not useful at all. The callback approach avoids this (because it only works on functions/methods and ignores other attributes).

提交回复
热议问题