Skip to content

Commit

Permalink
Fix sphinx-doc#10015: autodoc: autodoc_typehints_format='short' does …
Browse files Browse the repository at this point in the history
…not work when autodoc_typehints='description'
  • Loading branch information
tk0miya authored and AA-Turner committed Dec 29, 2021
1 parent 1c18656 commit 744ad82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 7 additions & 2 deletions sphinx/ext/autodoc/typehints.py
Expand Up @@ -23,16 +23,21 @@
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', {})
annotation = annotations.setdefault(name, OrderedDict())
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

Expand Down
6 changes: 3 additions & 3 deletions tests/test_ext_autodoc_configs.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 744ad82

Please sign in to comment.