1

I have two classes, a base class and a class that inherits from this base class

from abc import ABC

class Base(ABC):
    """
    Base class
    
    Parameters
    ----------
    param1 : int


    Attributes
    ----------
    name : string
        identifier (not necessarily unique).
   
    """
    def __init__(self):
        self.name = "dummy_name"


class MyClass(Base):
    """
    Some other class 
    
    Parameters
    ----------
    param2 : int
   
    """

    def __init__(self):
        pass

and I want to inherit the attributes of the Base class into MyClass with autodocs. But it does not seem to work.

My autodoc config is like that :

extensions = [
    "sphinx.ext.doctest",
    "sphinx.ext.todo",
    "sphinx.ext.viewcode",
    "sphinx.ext.autodoc",
    "sphinx.ext.autosummary",
    "sphinx.ext.mathjax",
    "sphinx.ext.autosectionlabel",
    "sphinxcontrib.video",
    "numpydoc",
    "sphinx_gallery.gen_gallery",
    "myst_parser",
]


autodoc_default_options = {"members": True, "inherited-members": True, "show-inheritance":True}
# generate autosummary even if no references
autosummary_generate = True
autodoc_inherit_docstrings = True

and the class template is like that :

:mod:`{{module}}`.{{objname}}
{{ underline }}==============

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}

       
.. include:: {{module}}.{{objname}}.examples

.. raw:: html

    <div class="clearer"></div>

I tried to add :inherited-members: to the class template but it did not change anything.

PS: what I give here is a toy example, the project I document is much larger and would not fit into a question.

  • Perhaps related to https://stackoverflow.com/q/70054740/407651 – mzjn May 10 '22 at 17:33
  • I don't have such an error message. The only error message I have is `sphinx warning: autosummary: stub file not found for the methods of the class. check your autosummary_generate settings` but the stubs are being generated so I do not think that this is the problem. Maybe I am wrong. –  May 10 '22 at 19:37
  • I don't know. The problem just seemed similar. Btw, if you get an error message, mention it in the question, not just in a comment. – mzjn May 10 '22 at 19:41

1 Answers1

0

In the above example, an instantiated MyClass object cannot have the attribute MyClass.name. For that you have to call super().__init__() first.

If you use the attribute name as a class attribute it should work. Maybe some tweaking of the class template is necessary. The class attribute will be part of the inherited_members attribute in the template (https://www.sphinx-doc.org/en/master/usage/extensions/autosummary.html#inherited_members).

Sergej
  • 25
  • 5