Skip to content

Commit

Permalink
Merge pull request #6576 from tk0miya/6574_annotation_for_varidic_params
Browse files Browse the repository at this point in the history
Fix autodoc: missing type annotation for variadic and keyword parameters (refs: #6574)
  • Loading branch information
tk0miya committed Aug 2, 2019
2 parents 04fbd51 + b436760 commit a01aabe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Bugs fixed
``__init__()`` and ``__new__()``
* #6574: autodoc: :confval:`autodoc_member_order` does not refer order of
imports when ``'bysource'`` order
* #6574: autodoc: missing type annotation for variadic and keyword parameters
* #6589: autodoc: Formatting issues with autodoc_typehints='none'
* #6498: autosummary: crashed with wrong autosummary_generate setting
* #6507: autosummary: crashes without no autosummary_generate setting
Expand Down
20 changes: 14 additions & 6 deletions sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ def return_annotation(self) -> Any:
return None

def format_args(self, show_annotation: bool = True) -> str:
def format_param_annotation(param: inspect.Parameter) -> str:
if isinstance(param.annotation, str) and param.name in self.annotations:
return self.format_annotation(self.annotations[param.name])
else:
return self.format_annotation(param.annotation)

args = []
last_kind = None
for i, param in enumerate(self.parameters.values()):
Expand All @@ -421,12 +427,8 @@ def format_args(self, show_annotation: bool = True) -> str:
param.KEYWORD_ONLY):
arg.write(param.name)
if show_annotation and param.annotation is not param.empty:
if isinstance(param.annotation, str) and param.name in self.annotations:
arg.write(': ')
arg.write(self.format_annotation(self.annotations[param.name]))
else:
arg.write(': ')
arg.write(self.format_annotation(param.annotation))
arg.write(': ')
arg.write(format_param_annotation(param))
if param.default is not param.empty:
if param.annotation is param.empty or show_annotation is False:
arg.write('=')
Expand All @@ -437,9 +439,15 @@ def format_args(self, show_annotation: bool = True) -> str:
elif param.kind == param.VAR_POSITIONAL:
arg.write('*')
arg.write(param.name)
if show_annotation and param.annotation is not param.empty:
arg.write(': ')
arg.write(format_param_annotation(param))
elif param.kind == param.VAR_KEYWORD:
arg.write('**')
arg.write(param.name)
if show_annotation and param.annotation is not param.empty:
arg.write(': ')
arg.write(format_param_annotation(param))

args.append(arg.getvalue())
last_kind = param.kind
Expand Down
6 changes: 5 additions & 1 deletion tests/test_util_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def meth2(self, arg1, arg2):

def test_Signature_annotations():
from typing_test_data import (f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10,
f11, f12, f13, f14, f15, f16, f17, f18, Node)
f11, f12, f13, f14, f15, f16, f17, f18, f19, Node)

# Class annotations
sig = inspect.Signature(f0).format_args()
Expand Down Expand Up @@ -275,6 +275,10 @@ def test_Signature_annotations():
sig = inspect.Signature(f18).format_args()
assert sig == '(self, arg1: Union[int, Tuple] = 10) -> List[Dict]'

# annotations for variadic and keyword parameters
sig = inspect.Signature(f19).format_args()
assert sig == '(*args: int, **kwargs: str)'

# type hints by string
sig = inspect.Signature(Node.children).format_args()
if (3, 5, 0) <= sys.version_info < (3, 5, 3):
Expand Down
5 changes: 5 additions & 0 deletions tests/typing_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ def f18(self, arg1: Union[int, Tuple] = 10) -> List[Dict]:
pass


def f19(*args: int, **kwargs: str):
pass



class Node:
def __init__(self, parent: Optional['Node']) -> None:
pass
Expand Down

0 comments on commit a01aabe

Please sign in to comment.