Fix #10015: autodoc: autodoc_typehints_format='short' does not work when autodoc_typehints='description'

This commit is contained in:
Takeshi KOMIYA
2021-12-27 01:58:42 +09:00
parent 5da68c3de3
commit 1f71f85fd6
2 changed files with 10 additions and 5 deletions

View File

@@ -23,6 +23,11 @@ from sphinx.util import inspect, typing
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

View File

@@ -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