Skip to content

Commit 1f71f85

Browse files
committed
Fix #10015: autodoc: autodoc_typehints_format='short' does not work when autodoc_typehints='description'
1 parent 5da68c3 commit 1f71f85

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

sphinx/ext/autodoc/typehints.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323
def record_typehints(app: Sphinx, objtype: str, name: str, obj: Any,
2424
options: Dict, args: str, retann: str) -> None:
2525
"""Record type hints to env object."""
26+
if app.config.autodoc_typehints_format == 'short':
27+
mode = 'smart'
28+
else:
29+
mode = 'fully-qualified'
30+
2631
try:
2732
if callable(obj):
2833
annotations = app.env.temp_data.setdefault('annotations', {})
2934
annotation = annotations.setdefault(name, OrderedDict())
3035
sig = inspect.signature(obj, type_aliases=app.config.autodoc_type_aliases)
3136
for param in sig.parameters.values():
3237
if param.annotation is not param.empty:
33-
annotation[param.name] = typing.stringify(param.annotation)
38+
annotation[param.name] = typing.stringify(param.annotation, mode)
3439
if sig.return_annotation is not sig.empty:
35-
annotation['return'] = typing.stringify(sig.return_annotation)
40+
annotation['return'] = typing.stringify(sig.return_annotation, mode)
3641
except (TypeError, ValueError):
3742
pass
3843

tests/test_ext_autodoc_configs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ def test_autodoc_typehints_description(app):
835835
' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) --\n'
836836
'\n'
837837
' Return type:\n'
838-
' Tuple[int, int]\n'
838+
' *Tuple*[int, int]\n'
839839
in context)
840840

841841
# Overloads still get displayed in the signature
@@ -887,7 +887,7 @@ def test_autodoc_typehints_description_no_undoc(app):
887887
' another tuple\n'
888888
'\n'
889889
' Return type:\n'
890-
' Tuple[int, int]\n'
890+
' *Tuple*[int, int]\n'
891891
in context)
892892

893893

@@ -978,7 +978,7 @@ def test_autodoc_typehints_both(app):
978978
' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) --\n'
979979
'\n'
980980
' Return type:\n'
981-
' Tuple[int, int]\n'
981+
' *Tuple*[int, int]\n'
982982
in context)
983983

984984
# Overloads still get displayed in the signature

0 commit comments

Comments
 (0)