Fix autodoc: missing type annotation for variadic and keyword parameters (refs: #6574)

This commit is contained in:
Takeshi KOMIYA
2019-07-13 19:25:38 +09:00
parent 9a552a9cd7
commit 92491c0769
4 changed files with 25 additions and 7 deletions
+1
View File
@@ -44,6 +44,7 @@ Bugs fixed
* #5502: linkcheck: Consider HTTP 503 response as not an error
* #6439: Make generated download links reproducible
* #6486: UnboundLocalError is raised if broken extension installed
* #6574: autodoc: missing type annotation for variadic and keyword parameters
* #6498: autosummary: crashed with wrong autosummary_generate setting
* #6507: autosummary: crashes without no autosummary_generate setting
* #6511: LaTeX: autonumbered list can not be customized in LaTeX
+14 -6
View File
@@ -400,6 +400,12 @@ class Signature:
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()):
@@ -421,12 +427,8 @@ class Signature:
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:
arg.write('=')
@@ -437,9 +439,15 @@ class Signature:
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
+5 -1
View File
@@ -195,7 +195,7 @@ def test_Signature_partialmethod():
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()
@@ -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):
+5
View File
@@ -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