Fix #10027: autodoc_typehints_format does not work with :show-inheritance:

This commit is contained in:
Takeshi KOMIYA 2022-01-01 14:54:08 +09:00
parent 6df45e0ead
commit 3fc98a2b3d
7 changed files with 107 additions and 11 deletions

View File

@ -1674,7 +1674,11 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
self.env.events.emit('autodoc-process-bases',
self.fullname, self.object, self.options, bases)
base_classes = [restify(cls) for cls in bases]
if self.config.autodoc_typehints_format == "short":
base_classes = [restify(cls, "smart") for cls in bases]
else:
base_classes = [restify(cls) for cls in bases]
sourcename = self.get_sourcename()
self.add_line('', sourcename)
self.add_line(' ' + _('Bases: %s') % ', '.join(base_classes), sourcename)
@ -1771,7 +1775,11 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
if self.doc_as_attr and not self.get_variable_comment():
try:
more_content = StringList([_('alias of %s') % restify(self.object)], source='')
if self.config.autodoc_typehints_format == "short":
alias = restify(self.object, "smart")
else:
alias = restify(self.object)
more_content = StringList([_('alias of %s') % alias], source='')
except AttributeError:
pass # Invalid class object is passed.
@ -1844,7 +1852,12 @@ class GenericAliasMixin(DataDocumenterMixinBase):
def update_content(self, more_content: StringList) -> None:
if inspect.isgenericalias(self.object):
more_content.append(_('alias of %s') % restify(self.object), '')
if self.config.autodoc_typehints_format == "short":
alias = restify(self.object, "smart")
else:
alias = restify(self.object)
more_content.append(_('alias of %s') % alias, '')
more_content.append('', '')
super().update_content(more_content)
@ -1862,7 +1875,11 @@ class NewTypeMixin(DataDocumenterMixinBase):
def update_content(self, more_content: StringList) -> None:
if inspect.isNewType(self.object):
supertype = restify(self.object.__supertype__)
if self.config.autodoc_typehints_format == "short":
supertype = restify(self.object.__supertype__, "smart")
else:
supertype = restify(self.object.__supertype__)
more_content.append(_('alias of %s') % supertype, '')
more_content.append('', '')
@ -1899,7 +1916,11 @@ class TypeVarMixin(DataDocumenterMixinBase):
for constraint in self.object.__constraints__:
attrs.append(stringify_typehint(constraint))
if self.object.__bound__:
attrs.append(r"bound=\ " + restify(self.object.__bound__))
if self.config.autodoc_typehints_format == "short":
bound = restify(self.object.__bound__, "smart")
else:
bound = restify(self.object.__bound__)
attrs.append(r"bound=\ " + bound)
if self.object.__covariant__:
attrs.append("covariant=True")
if self.object.__contravariant__:

View File

@ -9,3 +9,6 @@ C = Callable[[int], None] # a generic alias not having a doccomment
class Class:
#: A list of int
T = List[int]
#: A list of Class
L = List[Class]

View File

@ -1,3 +1,4 @@
from datetime import date
from typing import NewType, TypeVar
#: T1
@ -15,7 +16,7 @@ T4 = TypeVar("T4", covariant=True)
T5 = TypeVar("T5", contravariant=True)
#: T6
T6 = NewType("T6", int)
T6 = NewType("T6", date)
#: T7
T7 = TypeVar("T7", bound=int)
@ -26,4 +27,4 @@ class Class:
T1 = TypeVar("T1")
#: T6
T6 = NewType("T6", int)
T6 = NewType("T6", date)

View File

@ -1874,6 +1874,12 @@ def test_autodoc_GenericAlias(app):
'',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
'',
'.. py:attribute:: L',
' :module: target.genericalias',
'',
' A list of Class',
'',
'',
'.. py:attribute:: T',
' :module: target.genericalias',
'',
@ -1898,6 +1904,15 @@ def test_autodoc_GenericAlias(app):
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
'',
'',
'.. py:data:: L',
' :module: target.genericalias',
'',
' A list of Class',
'',
' alias of :py:class:`~typing.List`\\ '
'[:py:class:`target.genericalias.Class`]',
'',
'',
'.. py:data:: T',
' :module: target.genericalias',
'',
@ -1935,7 +1950,7 @@ def test_autodoc_TypeVar(app):
'',
' T6',
'',
' alias of :py:class:`int`',
' alias of :py:class:`datetime.date`',
'',
'',
'.. py:data:: T1',
@ -1975,7 +1990,7 @@ def test_autodoc_TypeVar(app):
'',
' T6',
'',
' alias of :py:class:`int`',
' alias of :py:class:`datetime.date`',
'',
'',
'.. py:data:: T7',

View File

@ -183,7 +183,7 @@ def test_autoattribute_NewType(app):
'',
' T6',
'',
' alias of :py:class:`int`',
' alias of :py:class:`datetime.date`',
'',
]

View File

@ -111,7 +111,7 @@ def test_autodata_NewType(app):
'',
' T6',
'',
' alias of :py:class:`int`',
' alias of :py:class:`datetime.date`',
'',
]

View File

@ -1231,6 +1231,62 @@ def test_autodoc_typehints_format_short(app):
]
@pytest.mark.sphinx('html', testroot='ext-autodoc',
confoverrides={'autodoc_typehints_format': "short"})
def test_autodoc_typehints_format_short_for_class_alias(app):
actual = do_autodoc(app, 'class', 'target.classes.Alias')
assert list(actual) == [
'',
'.. py:attribute:: Alias',
' :module: target.classes',
'',
' alias of :py:class:`~target.classes.Foo`',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc',
confoverrides={'autodoc_typehints_format': "short"})
def test_autodoc_typehints_format_short_for_generic_alias(app):
actual = do_autodoc(app, 'data', 'target.genericalias.L')
if sys.version_info < (3, 7):
assert list(actual) == [
'',
'.. py:data:: L',
' :module: target.genericalias',
' :value: typing.List[target.genericalias.Class]',
'',
' A list of Class',
'',
]
else:
assert list(actual) == [
'',
'.. py:data:: L',
' :module: target.genericalias',
'',
' A list of Class',
'',
' alias of :py:class:`~typing.List`\\ [:py:class:`~target.genericalias.Class`]',
'',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc',
confoverrides={'autodoc_typehints_format': "short"})
def test_autodoc_typehints_format_short_for_newtype_alias(app):
actual = do_autodoc(app, 'data', 'target.typevar.T6')
assert list(actual) == [
'',
'.. py:data:: T6',
' :module: target.typevar',
'',
' T6',
'',
' alias of :py:class:`~datetime.date`',
'',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_default_options(app):
# no settings