1

There are a lot of questions here on SO asking how to avoid inheriting the docstring of the parent when building docs with sphinx. I have the opposite problem: I want my class-level docstring to be inherited, but that isn't happening.

I built a toy example to show the problem. The two relevant classes are @dataclasses

# parent.py

from dataclasses import dataclass


@dataclass
class Foo:
    """ A numpy conform docstring.
    
    This is the long description

    Attributes
    ----------
    a_number : int
        Just a boring number
    a_string : str
        A cool string.

    """
    a_number : int
    a_string : str
# child.py

from dataclasses import dataclass

from .parent import Foo


@dataclass
class Bar(Foo):
    a_second_string : str

On top of the sphinx-quickstart files I modified the index.rst to kick-off generating of my classes and I modified the config to include autodoc, autosummary, and numpydoc (also tried napoleon or None, but neither generates the docstring on the child).

.. index.rst

FooBar
======

.. autosummary::
   :toctree: _autosummary

   foobar.parent.Foo
   foobar.child.Bar

# config.py

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('..'))


# -- Project information -----------------------------------------------------

project = 'FooBar'
copyright = '2021, FirefoxMetzger'
author = 'FirefoxMetzger'


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.autosummary',
    # 'numpydoc'
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

For reference, I'm on Sphinx 4.1.2.


I could (theoretically) add the line __doc__ = Foo.__doc__ to Bar, which would manually import the docstring; however in my case Foo and Bar are autogenerated (XML bindings). I would have to modify the generation script, which is only partly possible because it is an upstream project that would have to accept a respective PR. It would be much better (and faster) if I could solve this locally.

Any pointers on what's going on or how to move forward are highly appreciated.

mzjn
  • 48,958
  • 13
  • 128
  • 248
FirefoxMetzger
  • 2,880
  • 1
  • 18
  • 32
  • This has been answered before. Afaik docstrings aren't inherited and setting them programmatically [has a number of complications](https://stackoverflow.com/search?q=%5Bpython-sphinx%5D+inherit+docstring). To change the docstrings programmatically would be to import them with fully qualified names in your `conf.py` and set the docstring there. You don't have any other options here. What is cleaner depends on how you're working with those generated files (you could edit the source before generating with Sphinx), but there's no magic solution Sphinx wise that I'm aware off. – bad_coder Aug 01 '21 at 14:46
  • But you should also examine your reasons for wanting to inherit a docstring, because it's implicitly assumed the derived class shares the properties of the parent class. – bad_coder Aug 01 '21 at 14:48
  • @bad_coder I'm confused. According to [the Sphinx docs](https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_inherit_docstrings), there is an option to toggle inheritance of class docstrings from parent classes and it should do the inheritance by default. (Unfortunately, it doesn't seem to be working in my case) Could you clarify what you mean by `it's implicitly assumed the derived class shares the properties of the parent class`? – FirefoxMetzger Aug 01 '21 at 14:55
  • Ups, I had forgotten about `autodoc_inherit_docstrings = True`. What happens if you run Sphinx with the option set in `conf.py`? (This might be a dataclass specific limitation). – bad_coder Aug 01 '21 at 15:01
  • 1
    @bad_coder Nothing. It is the same as not specifying the option. – FirefoxMetzger Aug 01 '21 at 20:46

0 Answers0