Fix #2372: autosummary: invalid signatures are shown for type annotated functions

This commit is contained in:
Takeshi KOMIYA 2018-02-03 16:53:19 +09:00
parent a19c300653
commit 929379d0ce
3 changed files with 20 additions and 1 deletions

View File

@ -29,6 +29,7 @@ Bugs fixed
* #4525: autosectionlabel does not support parallel build * #4525: autosectionlabel does not support parallel build
* #3953: Do not raise warning when there is a working intersphinx inventory * #3953: Do not raise warning when there is a working intersphinx inventory
* #4487: math: ValueError is raised on parallel build. Thanks to jschueller. * #4487: math: ValueError is raised on parallel build. Thanks to jschueller.
* #2372: autosummary: invalid signatures are shown for type annotated functions
Testing Testing
-------- --------

View File

@ -397,10 +397,20 @@ class Autosummary(Directive):
return [table_spec, table] return [table_spec, table]
def strip_arg_typehint(s):
# type: (unicode) -> unicode
"""Strip a type hint from argument definition."""
return s.split(':')[0].strip()
def mangle_signature(sig, max_chars=30): def mangle_signature(sig, max_chars=30):
# type: (unicode, int) -> unicode # type: (unicode, int) -> unicode
"""Reformat a function signature to a more compact form.""" """Reformat a function signature to a more compact form."""
s = re.sub(r"^\((.*)\)$", r"\1", sig).strip() # Strip return type annotation
s = re.sub(r"\)\s*->\s.*$", ")", sig)
# Remove parenthesis
s = re.sub(r"^\((.*)\)$", r"\1", s).strip()
# Strip strings (which can contain things that confuse the code below) # Strip strings (which can contain things that confuse the code below)
s = re.sub(r"\\\\", "", s) s = re.sub(r"\\\\", "", s)
@ -422,6 +432,13 @@ def mangle_signature(sig, max_chars=30):
opts.insert(0, m.group(2)) opts.insert(0, m.group(2))
s = m.group(1)[:-2] s = m.group(1)[:-2]
# Strip typehints
for i, arg in enumerate(args):
args[i] = strip_arg_typehint(arg)
for i, opt in enumerate(opts):
opts[i] = strip_arg_typehint(opt)
# Produce a more compact signature # Produce a more compact signature
sig = limited_join(", ", args, max_chars=max_chars - 2) sig = limited_join(", ", args, max_chars=max_chars - 2)
if opts: if opts:

View File

@ -47,6 +47,7 @@ def test_mangle_signature():
(a, b={'c=d, ': 3, '\\\\': 3}) :: (a[, b]) (a, b={'c=d, ': 3, '\\\\': 3}) :: (a[, b])
(a=1, b=2, c=3) :: ([a, b, c]) (a=1, b=2, c=3) :: ([a, b, c])
(a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c]) (a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c])
(a: int, b: int) -> str :: (a, b)
""" """
TEST = [[y.strip() for y in x.split("::")] for x in TEST.split("\n") TEST = [[y.strip() for y in x.split("::")] for x in TEST.split("\n")