diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 21e0a11cf..48f9efb5e 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -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__: diff --git a/tests/roots/test-ext-autodoc/target/genericalias.py b/tests/roots/test-ext-autodoc/target/genericalias.py index 9909efca1..3856e034d 100644 --- a/tests/roots/test-ext-autodoc/target/genericalias.py +++ b/tests/roots/test-ext-autodoc/target/genericalias.py @@ -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] diff --git a/tests/roots/test-ext-autodoc/target/typevar.py b/tests/roots/test-ext-autodoc/target/typevar.py index c330e2d88..ff2d46d19 100644 --- a/tests/roots/test-ext-autodoc/target/typevar.py +++ b/tests/roots/test-ext-autodoc/target/typevar.py @@ -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) diff --git a/tests/test_ext_autodoc.py b/tests/test_ext_autodoc.py index c853fbb03..e701d7778 100644 --- a/tests/test_ext_autodoc.py +++ b/tests/test_ext_autodoc.py @@ -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', diff --git a/tests/test_ext_autodoc_autoattribute.py b/tests/test_ext_autodoc_autoattribute.py index 8fe065d65..826b73dc7 100644 --- a/tests/test_ext_autodoc_autoattribute.py +++ b/tests/test_ext_autodoc_autoattribute.py @@ -183,7 +183,7 @@ def test_autoattribute_NewType(app): '', ' T6', '', - ' alias of :py:class:`int`', + ' alias of :py:class:`datetime.date`', '', ] diff --git a/tests/test_ext_autodoc_autodata.py b/tests/test_ext_autodoc_autodata.py index f983726ad..36a96a9d3 100644 --- a/tests/test_ext_autodoc_autodata.py +++ b/tests/test_ext_autodoc_autodata.py @@ -111,7 +111,7 @@ def test_autodata_NewType(app): '', ' T6', '', - ' alias of :py:class:`int`', + ' alias of :py:class:`datetime.date`', '', ] diff --git a/tests/test_ext_autodoc_configs.py b/tests/test_ext_autodoc_configs.py index b178889ef..e1a9901dc 100644 --- a/tests/test_ext_autodoc_configs.py +++ b/tests/test_ext_autodoc_configs.py @@ -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