diff --git a/sphinx/ext/autodoc/typehints.py b/sphinx/ext/autodoc/typehints.py index f4b4dd35e57..5102e6706b0 100644 --- a/sphinx/ext/autodoc/typehints.py +++ b/sphinx/ext/autodoc/typehints.py @@ -23,6 +23,11 @@ def record_typehints(app: Sphinx, objtype: str, name: str, obj: Any, options: Dict, args: str, retann: str) -> None: """Record type hints to env object.""" + if app.config.autodoc_typehints_format == 'short': + mode = 'smart' + else: + mode = 'fully-qualified' + try: if callable(obj): annotations = app.env.temp_data.setdefault('annotations', {}) @@ -30,9 +35,9 @@ def record_typehints(app: Sphinx, objtype: str, name: str, obj: Any, 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) + annotation[param.name] = typing.stringify(param.annotation, mode) if sig.return_annotation is not sig.empty: - annotation['return'] = typing.stringify(sig.return_annotation) + annotation['return'] = typing.stringify(sig.return_annotation, mode) except (TypeError, ValueError): pass diff --git a/tests/test_ext_autodoc_configs.py b/tests/test_ext_autodoc_configs.py index b178889ef13..6a4961ff6c8 100644 --- a/tests/test_ext_autodoc_configs.py +++ b/tests/test_ext_autodoc_configs.py @@ -835,7 +835,7 @@ def test_autodoc_typehints_description(app): ' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) --\n' '\n' ' Return type:\n' - ' Tuple[int, int]\n' + ' *Tuple*[int, int]\n' in context) # Overloads still get displayed in the signature @@ -887,7 +887,7 @@ def test_autodoc_typehints_description_no_undoc(app): ' another tuple\n' '\n' ' Return type:\n' - ' Tuple[int, int]\n' + ' *Tuple*[int, int]\n' in context) @@ -978,7 +978,7 @@ def test_autodoc_typehints_both(app): ' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) --\n' '\n' ' Return type:\n' - ' Tuple[int, int]\n' + ' *Tuple*[int, int]\n' in context) # Overloads still get displayed in the signature