Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sphinx/ext/autodoc/typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def record_typehints(app: Sphinx, objtype: str, name: str, obj: Any,
if callable(obj):
annotations = app.env.temp_data.setdefault('annotations', {})
annotation = annotations.setdefault(name, OrderedDict())
sig = inspect.signature(obj)
sig = inspect.signature(obj, type_aliases=app.config.autodoc_type_aliases)
for param in sig.parameters.values():
if param.annotation is not param.empty:
annotation[param.name] = typing.stringify(param.annotation)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

from typing import TypeAlias


class AliasTarget:
"""Marker class used for type-alias testing."""


AliasName: TypeAlias = AliasTarget


def use_alias(value: AliasName) -> AliasName:
"""Return the provided value unchanged."""

return value
11 changes: 11 additions & 0 deletions tests/roots/test-ext-autodoc-alias-description/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
import sys


sys.path.insert(0, os.path.abspath('.'))


extensions = ['sphinx.ext.autodoc']

# The suffix of source filenames.
source_suffix = '.rst'
2 changes: 2 additions & 0 deletions tests/roots/test-ext-autodoc-alias-description/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.. autofunction:: aliaspkg.alias_description.use_alias
15 changes: 15 additions & 0 deletions tests/test_ext_autodoc_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,21 @@ def test_autodoc_typehints_description_for_invalid_node(app):
restructuredtext.parse(app, text) # raises no error


@pytest.mark.sphinx('text', testroot='ext-autodoc-alias-description')
def test_autodoc_typehints_description_respects_aliases(app):
app.config.autodoc_typehints = 'description'
app.config.autodoc_type_aliases = {
'AliasName': 'aliaspkg.alias_description.AliasName',
}

app.build()
context = (app.outdir / 'index.txt').read_text()

assert 'Parameters:\n **value** (*AliasName*) --' in context
assert 'Return type:\n AliasName' in context
assert 'aliaspkg.alias_description.AliasTarget' not in context


@pytest.mark.skipif(sys.version_info < (3, 7), reason='python 3.7+ is required.')
@pytest.mark.sphinx('text', testroot='ext-autodoc')
def test_autodoc_type_aliases(app):
Expand Down