Skip to content

Commit

Permalink
Fix #8219: autodoc: Parameters for generic base class are not shown
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Sep 21, 2020
1 parent 95bb8fe commit 3528b68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -26,6 +26,8 @@ Bugs fixed
autodoc_default_options
* #8103: autodoc: functools.cached_property is not considered as a property
* #8219: autodoc: A signature of constructor of generic class is incorrect
* #8219: autodoc: Parameters for generic class are not shown when super class is
a generic class and show-inheritance option is given
* #8192: napoleon: description is disappeared when it contains inline literals
* #8169: LaTeX: pxjahyper loaded even when latex_engine is not platex
* #8093: The highlight warning has wrong location in some builders (LaTeX,
Expand Down
33 changes: 26 additions & 7 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -1494,13 +1494,32 @@ def add_directive_header(self, sig: str) -> None:
if not self.doc_as_attr and self.options.show_inheritance:
sourcename = self.get_sourcename()
self.add_line('', sourcename)
if hasattr(self.object, '__bases__') and len(self.object.__bases__):
bases = [':class:`%s`' % b.__name__
if b.__module__ in ('__builtin__', 'builtins')
else ':class:`%s.%s`' % (b.__module__, b.__qualname__)
for b in self.object.__bases__]
self.add_line(' ' + _('Bases: %s') % ', '.join(bases),
sourcename)

def restify(cls: "Type") -> str:
"""Convert class to reST text."""
if cls.__module__ in ('__builtin__', 'builtins'):
return ':class:`%s`' % cls.__name__
elif inspect.isgenericalias(cls):
if cls._name:
text = ':class:`%s.%s`' % (cls.__module__, cls._name)
else:
text = restify(cls.__origin__)

if cls.__args__:
text += r"\ [%s]" % ", ".join(restify(a) for a in cls.__args__)
return text
elif hasattr(cls, '__qualname__'):
return ':class:`%s.%s`' % (cls.__module__, cls.__qualname__)
else:
# not a class (ex. TypeVar)
return ':obj:`%s.%s`' % (cls.__module__, cls.__name__)

if hasattr(self.object, '__orig_bases__') and len(self.object.__orig_bases__):
bases = [restify(cls) for cls in self.object.__orig_bases__]
self.add_line(' ' + _('Bases: %s') % ', '.join(bases), sourcename)
elif hasattr(self.object, '__bases__') and len(self.object.__bases__):
bases = [restify(cls) for cls in self.object.__bases__]
self.add_line(' ' + _('Bases: %s') % ', '.join(bases), sourcename)

def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]:
if encoding is not None:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_ext_autodoc_autoclass.py
Expand Up @@ -16,10 +16,13 @@

@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_generic_class(app):
actual = do_autodoc(app, 'class', 'target.classes.Qux')
options = {"show-inheritance": None}
actual = do_autodoc(app, 'class', 'target.classes.Qux', options)
assert list(actual) == [
'',
'.. py:class:: Qux(x, y)',
' :module: target.classes',
'',
' Bases: :class:`typing.Generic`[:obj:`target.classes.T`]',
'',
]

0 comments on commit 3528b68

Please sign in to comment.