I noticed a change in behavior in how Sphinx renders class descriptions. Given this code
# my example happens to be a dataclass, but the behavior for
# regular classes is the same
@dataclass
class TestClass:
"""This is a test class for dataclasses.
This is the body of the docstring description.
"""
var_int: int
var_str: str
plus some generic Sphinx settings, I used to get this about two years ago
And am now getting this
Is there a way to tell Sphinx not to add the class variables to the bottom of the class definition? It's particularly annoying that it assumes their values to be None, just because they don't have defaults.
This issue came up during discussions on this post, which also contains a bit more context in comments regarding Sphinx configuration etc.


Members of an object are included by an autodoc directive depending if:
:members:option is used (for members with docstrings).:undoc-members:option is used (for members without docstrings).For example:
In the above
.rstfile the autodoc directive does not have explicit options set, what Sphinx will do is implicitly apply the option flags taken from theautodoc_default_flagsinconf.py.Setting the following in
conf.pywould cause all members of an object (with or without docstrings) to be included by Sphinx in all directives that do not explicitly specify options.The result:
However, this raises a question: What do autodoc and sphinx-napoleon extensions do if members are explicitly specified in the
Attributesdocstring section but also included by the autodoc extension?For example, using the following docstring together with the options specified above in
autodoc_default_options.In this case members will be declared twice, once by each extension, with the corresponding reST declaration being generated as a duplicate. Having a duplicate reST declaration will lead to the usual warning:
Here one difference can be noted: sphinx-napoleon will declare the member in its own docstring section whereas autodoc will render it normally as other members. The visual difference will depend on theme, for example using
classictheme:Finally, if you want to document members using sphinx-napoleon docstring sections and avoid a duplicate reST declaration from autodoc while keeping
autodoc_default_optionsas shown, you can explicitly use:exclude-members:option in that specific directive, for example:Would document the members using only sphinx-napoleon generated reST directives: