From 333e7a447edfcb3092032ac801116e1eec193e44 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 21 Nov 2020 01:30:17 +0900 Subject: [PATCH] Fix #8452: autodoc_type_aliases doesn't work with autodoc_typehints --- CHANGES | 2 ++ sphinx/ext/autodoc/typehints.py | 2 +- tests/test_ext_autodoc_configs.py | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index d9f1e06b2..f44b28e19 100644 --- a/CHANGES +++ b/CHANGES @@ -47,6 +47,8 @@ Bugs fixed type annotated variables * #8443: autodoc: autoattribute directive can't create document for PEP-526 based uninitalized variables +* #8452: autodoc: autodoc_type_aliases doesn't work when autodoc_typehints is + set to "description" * #8419: html search: Do not load ``language_data.js`` in non-search pages * #8454: graphviz: The layout option for graph and digraph directives don't work diff --git a/sphinx/ext/autodoc/typehints.py b/sphinx/ext/autodoc/typehints.py index 70cbc3ba1..e6451b52c 100644 --- a/sphinx/ext/autodoc/typehints.py +++ b/sphinx/ext/autodoc/typehints.py @@ -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) diff --git a/tests/test_ext_autodoc_configs.py b/tests/test_ext_autodoc_configs.py index 4dff7c3db..ca7968bba 100644 --- a/tests/test_ext_autodoc_configs.py +++ b/tests/test_ext_autodoc_configs.py @@ -777,6 +777,28 @@ def test_autodoc_type_aliases(app): ] +@pytest.mark.skipif(sys.version_info < (3, 7), reason='python 3.7+ is required.') +@pytest.mark.sphinx('text', testroot='ext-autodoc', + srcdir='autodoc_typehints_description_and_type_aliases', + confoverrides={'autodoc_typehints': "description", + 'autodoc_type_aliases': {'myint': 'myint'}}) +def test_autodoc_typehints_description_and_type_aliases(app): + (app.srcdir / 'annotations.rst').write_text('.. autofunction:: target.annotations.sum') + app.build() + context = (app.outdir / 'annotations.txt').read_text() + assert ('target.annotations.sum(x, y)\n' + '\n' + ' docstring\n' + '\n' + ' Parameters:\n' + ' * **x** (*myint*) --\n' + '\n' + ' * **y** (*myint*) --\n' + '\n' + ' Return type:\n' + ' myint\n' == context) + + @pytest.mark.sphinx('html', testroot='ext-autodoc') def test_autodoc_default_options(app): # no settings