New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Building autodocs for derived classes produces invalid warnings #9250
Comments
This is a rather insidious warning because it points the developer to the derived class as the root cause of the problem, but there is no obvious correlation between the code of the derived class and the actual cause of the problem. This is further exacerbated when the derived class is in a different project or library from the base class, because then the developer needs to review the contents of the full inheritance stack. However, even after reviewing the code for the base class (which may or may not be the direct parent class of the one exposing the error) there is no obvious correlation between the warning and the code because the doc strings on the base class(es) are defined and correctly formatted, and Sphinx does not complain when generating the docs for the base class. After much ad-hoc testing I was able to deduce that simply adding a doc string to the constructor on the derived class was sufficient to circumvent the error, but when working on a large number of Python projects and sharing those projects across many developers, problems like this are hard to avoid in practice. My team manages dozens of Python projects, and are trying to build docs for all without having any warnings produced, so we've opted to disable the combined auto content flag to avoid these superfluous warnings. However, this is just a hack/workaround to get us by for now. I hope someone can investigate the root cause of this problem and fix it so that docs for derived classes can be generated without warnings. |
I think what you want is |
Disabling inherited doc strings is not the solution for this bug. The doc strings can and should be inheritable here. The problem is that one of the pre-processing steps involved with the auto content logic is trying to combine doc strings from the class and constructor of the base class as well as the derived class, and not aligning the formatting between them. |
To be clear, this code snippet produces warnings:
Where as this code snippet does not:
Both of these examples are valid Python class definitions, with valid doc strings. Both the inherited docstring functionality AND the auto content combining logic should work in both situations. |
Thank you for your explanation. I misunderstood the problem! |
Internally, the example is expanded to the following code (without napoleon extension):
The second and following lines of the inherited method not having docstring is unindented. This is unexpected behavior and it's a bug. |
…ng is wrongly parsed `sphinx.util.inspect.getdoc()` clean the docstring up if the method is inherited and not having docstring. That causes indentations are removed on processing it.
…fect to classes The docstring of the superclass is not extracted even if the docstring of the subclass is empty and autodoc_inherited_docstrings=True. This adds `sphinx.util.inspect.getclassdoc()` to respect the configuration on getting docstring.
Fix #9250: autodoc: The inherited method not having docstring is wrongly parsed
…fect to classes The docstring of the superclass is not extracted even if the docstring of the subclass is empty and autodoc_inherited_docstrings=True. This adds `sphinx.util.inspect.getclassdoc()` to respect the configuration on getting docstring.
…fect to classes The docstring of the superclass is not extracted even if the docstring of the subclass is empty and autodoc_inherited_docstrings=True. This adds `sphinx.util.inspect.getclassdoc()` to respect the configuration on getting docstring.
Awesome! Thanks for the quick turnaround on this. Hopefully that means it was an easy fix! |
Describe the bug
Generating API docs using the autodoc extensions, when a base class has doc strings in the constructor as well as in the class definition, and the autoclass_content feature is set to "both", produces superfluous warnings when parsing derived classes with overloaded constructors with no doc strings on them.
To Reproduce
Enable the auto content option in Sphinx to combine the class doc string and constructor doc strings together (ie:
autoclass_content = "both"
in the conf.py script)Attempt to generate the docs using the apidoc extension (ie: when using sphinxcontrib.apidoc, just run
sphinx-build docs/ htmldocs/
)Expected behavior
Expected behavior: the API docs for both the base and derived classes should be generated without warnings.
Actual behavior: docs for the base class generate correctly, but docs for the derived class produce the following warning
"docstring of sample.MyDerived: WARNING: Unexpected indentation."
Environment info
The text was updated successfully, but these errors were encountered: