Skip to content

Commit

Permalink
Merge pull request #8589 from tk0miya/8581_deprecate_no_docstrings
Browse files Browse the repository at this point in the history
refactor: Deprecate `no_docstring` argument for Documenter.add_content()
  • Loading branch information
tk0miya committed Dec 27, 2020
2 parents 4fc93ac + 6c645e4 commit 07983a5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
57 changes: 31 additions & 26 deletions sphinx/ext/autodoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,12 @@ def add_directive_header(self, sig: str) -> None:
# etc. don't support a prepended module name
self.add_line(' :module: %s' % self.modname, sourcename)

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
"""Decode and return lines of the docstring(s) for the object."""
def get_doc(self, encoding: str = None, ignore: int = None) -> Optional[List[List[str]]]:
"""Decode and return lines of the docstring(s) for the object.
When it returns None value, autodoc-process-docstring will not be called for this
object.
"""
if encoding is not None:
warnings.warn("The 'encoding' argument to autodoc.%s.get_doc() is deprecated."
% self.__class__.__name__,
Expand Down Expand Up @@ -587,12 +591,10 @@ 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."""
# Suspended temporarily (see https://github.com/sphinx-doc/sphinx/pull/8533)
#
# if no_docstring:
# warnings.warn("The 'no_docstring' argument to %s.add_content() is deprecated."
# % self.__class__.__name__,
# RemovedInSphinx50Warning, stacklevel=2)
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()
Expand All @@ -612,13 +614,17 @@ def add_content(self, more_content: Optional[StringList], no_docstring: bool = F
# add content from docstrings
if not no_docstring:
docstrings = self.get_doc()
if not docstrings:
# append at least a dummy docstring, so that the event
# autodoc-process-docstring is fired and can add some
# content if desired
docstrings.append([])
for i, line in enumerate(self.process_doc(docstrings)):
self.add_line(line, sourcename, i)
if docstrings is None:
# Do not call autodoc-process-docstring on get_doc() returns None.
pass
else:
if not docstrings:
# append at least a dummy docstring, so that the event
# autodoc-process-docstring is fired and can add some
# content if desired
docstrings.append([])
for i, line in enumerate(self.process_doc(docstrings)):
self.add_line(line, sourcename, i)

# add additional content (e.g. from document), if present
if more_content:
Expand Down Expand Up @@ -1213,7 +1219,7 @@ def _find_signature(self, encoding: str = None) -> Tuple[str, str]:

return result

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
def get_doc(self, encoding: str = None, ignore: int = None) -> Optional[List[List[str]]]:
if encoding is not None:
warnings.warn("The 'encoding' argument to autodoc.%s.get_doc() is deprecated."
% self.__class__.__name__,
Expand Down Expand Up @@ -1611,14 +1617,14 @@ def convert(m: ClassAttribute) -> ObjectMember:
else:
return False, [convert(m) for m in members.values() if m.class_ == self.object]

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
def get_doc(self, encoding: str = None, ignore: int = None) -> Optional[List[List[str]]]:
if encoding is not None:
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 []
return None

lines = getattr(self, '_new_docstrings', None)
if lines is not None:
Expand Down Expand Up @@ -1667,9 +1673,8 @@ def add_content(self, more_content: Optional[StringList], no_docstring: bool = F
) -> None:
if self.doc_as_attr:
more_content = StringList([_('alias of %s') % restify(self.object)], source='')
super().add_content(more_content, no_docstring=True)
else:
super().add_content(more_content)

super().add_content(more_content)

def document_members(self, all_members: bool = False) -> None:
if self.doc_as_attr:
Expand Down Expand Up @@ -1774,7 +1779,7 @@ def should_suppress_directive_header(self) -> bool:
return (isinstance(self.object, TypeVar) or
super().should_suppress_directive_header())

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
def get_doc(self, encoding: str = None, ignore: int = None) -> Optional[List[List[str]]]:
if ignore is not None:
warnings.warn("The 'ignore' argument to autodoc.%s.get_doc() is deprecated."
% self.__class__.__name__,
Expand Down Expand Up @@ -1838,7 +1843,7 @@ def should_suppress_value_header(self) -> bool:
return (self.object is UNINITIALIZED_ATTR or
super().should_suppress_value_header())

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
def get_doc(self, encoding: str = None, ignore: int = None) -> Optional[List[List[str]]]:
if self.object is UNINITIALIZED_ATTR:
return []
else:
Expand Down Expand Up @@ -2102,7 +2107,7 @@ def should_suppress_value_header(self) -> bool:
return (inspect.isattributedescriptor(self.object) or
super().should_suppress_directive_header())

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
def get_doc(self, encoding: str = None, ignore: int = None) -> Optional[List[List[str]]]:
if not inspect.isattributedescriptor(self.object):
# the docstring of non datadescriptor is very probably the wrong thing
# to display
Expand Down Expand Up @@ -2141,7 +2146,7 @@ def should_suppress_directive_header(self) -> bool:
else:
return super().should_suppress_directive_header()

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
def get_doc(self, encoding: str = None, ignore: int = None) -> Optional[List[List[str]]]:
if self.object is SLOTSATTR:
try:
__slots__ = inspect.getslots(self.parent)
Expand Down Expand Up @@ -2395,7 +2400,7 @@ def get_attribute_comment(self, parent: Any, attrname: str) -> Optional[List[str

return None

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
def get_doc(self, encoding: str = None, ignore: int = None) -> Optional[List[List[str]]]:
# Check the attribute has a docstring-comment
comment = self.get_attribute_comment(self.parent, self.objpath[-1])
if comment:
Expand Down
3 changes: 3 additions & 0 deletions tests/roots/test-ext-autodoc/target/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ def __init__(self, x, y):
class Quux(List[Union[int, float]]):
"""A subclass of List[Union[int, float]]"""
pass


Alias = Foo
18 changes: 18 additions & 0 deletions tests/test_ext_autodoc_autoclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,21 @@ def test_show_inheritance_for_subclass_of_generic_type(app):
' A subclass of List[Union[int, float]]',
'',
]


def test_class_alias(app):
def autodoc_process_docstring(*args):
"""A handler always raises an error.
This confirms this handler is never called for class aliases.
"""
raise

app.connect('autodoc-process-docstring', autodoc_process_docstring)
actual = do_autodoc(app, 'class', 'target.classes.Alias')
assert list(actual) == [
'',
'.. py:attribute:: Alias',
' :module: target.classes',
'',
' alias of :class:`target.classes.Foo`',
]

0 comments on commit 07983a5

Please sign in to comment.