Skip to content

Commit

Permalink
refactor: Deprecate no_docstring argument for Documenter.add_content()
Browse files Browse the repository at this point in the history
This deprecates `no_docstring` argument for Documenter.add_content().
After this change, please use Documenter.get_doc() to control (suppress)
the content of the python object.
  • Loading branch information
tk0miya committed Dec 12, 2020
1 parent 8823d1c commit 3638a9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -14,6 +14,8 @@ Deprecated
----------

* The ``follow_wrapped`` argument of ``sphinx.util.inspect.signature()``
* The ``no_docstring`` argument of
``sphinx.ext.autodoc.Documenter.add_content()``
* ``sphinx.ext.autodoc.Documenter.get_object_members()``
* ``sphinx.ext.autodoc.DataDeclarationDocumenter``
* ``sphinx.ext.autodoc.GenericAliasDocumenter``
Expand Down
6 changes: 6 additions & 0 deletions doc/extdev/deprecated.rst
Expand Up @@ -31,6 +31,12 @@ The following is a list of deprecated interfaces.
- 5.0
- N/A

* - The ``no_docstring`` argument of
``sphinx.ext.autodoc.Documenter.add_content()``
- 3.4
- 5.0
- ``sphinx.ext.autodoc.Documenter.get_doc()``

* - ``sphinx.ext.autodoc.Documenter.get_object_members()``
- 3.4
- 6.0
Expand Down
34 changes: 20 additions & 14 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -584,6 +584,11 @@ def get_sourcename(self) -> str:
def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
) -> None:
"""Add content from docstrings, attribute documentation and user."""
if no_docstring:
warnings.warn("The 'no_docstring' argument to %s.add_content() is deprecated."
% self.__class__.__name__,
RemovedInSphinx50Warning, stacklevel=2)

# set sourcename and add content from attribute documentation
sourcename = self.get_sourcename()
if self.analyzer:
Expand Down Expand Up @@ -1595,6 +1600,10 @@ def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
warnings.warn("The 'encoding' argument to autodoc.%s.get_doc() is deprecated."
% self.__class__.__name__,
RemovedInSphinx40Warning, stacklevel=2)
if self.doc_as_attr:
# Don't show the docstring of the class when it is an alias.
return []

lines = getattr(self, '_new_docstrings', None)
if lines is not None:
return lines
Expand Down Expand Up @@ -1641,10 +1650,9 @@ def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
) -> None:
if self.doc_as_attr:
content = StringList([_('alias of %s') % restify(self.object)], source='')
super().add_content(content, no_docstring=True)
else:
super().add_content(more_content)
more_content = StringList([_('alias of %s') % restify(self.object)], source='')

super().add_content(more_content)

This comment has been minimized.

Copy link
@jorisvandenbossche

jorisvandenbossche Dec 23, 2020

Contributor

One consequence of this change is that now the 'autodoc-process-docstring' event is called for the case of doc_as_attr, while before this was not called.

I am running into a problem with this with numpydoc, which then adds content for a class dostring to this py:attribute directive (because of doc_as_attr= True, sphinx is using an attribute docstring instead of class docstring). And then later sphinx cannot parse this generated directive, because it cannot find members for an attribute ..

Given that for the case of doc_as_attr= True a dummy docstring gets generated in a non-class directive, it's maybe best to continue not passing this to the 'autodoc-process-docstring' event?

This comment has been minimized.

Copy link
@tk0miya

tk0miya Dec 23, 2020

Author Member

Thank you for your comment. I did not notice that change. And it's not intended.

This comment has been minimized.

Copy link
@altendky

altendky Dec 28, 2020

Contributor

Just for cross linkage: #8602 (comment)


def document_members(self, all_members: bool = False) -> None:
if self.doc_as_attr:
Expand Down Expand Up @@ -1811,13 +1819,11 @@ def should_suppress_value_header(self) -> bool:
return (self.object == UNINITIALIZED_ATTR or
super().should_suppress_value_header())

def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
) -> None:
def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
if self.object is UNINITIALIZED_ATTR:
# suppress docstring of the value
super().add_content(more_content, no_docstring=True) # type: ignore
return []
else:
super().add_content(more_content, no_docstring=no_docstring) # type: ignore
return super().get_doc(encoding, ignore) # type: ignore


class DataDocumenter(GenericAliasMixin, NewTypeMixin, TypeVarMixin,
Expand Down Expand Up @@ -2220,6 +2226,11 @@ def add_directive_header(self, sig: str) -> None:
pass

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
if not self._datadescriptor:
# if it's not a data descriptor, its docstring is very probably the
# wrong thing to display
return []

try:
# Disable `autodoc_inherit_docstring` temporarily to avoid to obtain
# a docstring from the value which descriptor returns unexpectedly.
Expand All @@ -2232,11 +2243,6 @@ def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:

def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
) -> None:
if not self._datadescriptor:
# if it's not a data descriptor, its docstring is very probably the
# wrong thing to display
no_docstring = True

if more_content is None:
more_content = StringList()
self.update_content(more_content)
Expand Down

0 comments on commit 3638a9e

Please sign in to comment.