mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
autosummary: improve robustness of the signature compactification
This commit is contained in:
@@ -322,13 +322,29 @@ class Autosummary(Directive):
|
||||
|
||||
def mangle_signature(sig, max_chars=30):
|
||||
"""Reformat a function signature to a more compact form."""
|
||||
sig = re.sub(r"^\((.*)\)$", r"\1", sig) + ", "
|
||||
r = re.compile(r"(?P<name>[a-zA-Z0-9_*]+)(?P<default>=.*?)?, ")
|
||||
items = r.findall(sig)
|
||||
s = re.sub(r"^\((.*)\)$", r"\1", sig).strip()
|
||||
|
||||
args = [name for name, default in items if not default]
|
||||
opts = [name for name, default in items if default]
|
||||
# Strip strings (which can contain things that confuse the code below)
|
||||
s = re.sub(r"\\\\", "", s)
|
||||
s = re.sub(r"\\'", "", s)
|
||||
s = re.sub(r"'[^']*'", "", s)
|
||||
|
||||
# Parse the signature to arguments + options
|
||||
args = []
|
||||
opts = []
|
||||
|
||||
opt_re = re.compile(r"^(.*, |)([a-zA-Z0-9_*]+)=")
|
||||
while s:
|
||||
m = opt_re.search(s)
|
||||
if not m:
|
||||
# The rest are arguments
|
||||
args = s.split(', ')
|
||||
break
|
||||
|
||||
opts.insert(0, m.group(2))
|
||||
s = m.group(1)[:-2]
|
||||
|
||||
# Produce a more compact signature
|
||||
sig = limited_join(", ", args, max_chars=max_chars-2)
|
||||
if opts:
|
||||
if not sig:
|
||||
|
||||
@@ -23,6 +23,14 @@ def test_mangle_signature():
|
||||
:: (a, b[, aaa, bbb, ccc, ...])
|
||||
(a, b, c=(), d=<foo>) :: (a, b[, c, d])
|
||||
(a, b, c='foobar()', d=123) :: (a, b[, c, d])
|
||||
(a, b[, c]) :: (a, b[, c])
|
||||
(a, b[, cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]) :: (a, b[, ...)
|
||||
(a, b='c=d, e=f, g=h', c=3) :: (a[, b, c])
|
||||
(a, b='c=d, \\'e=f,\\' g=h', c=3) :: (a[, b, c])
|
||||
(a, b='c=d, ', e='\\\\' g=h, c=3) :: (a[, b, e, c])
|
||||
(a, b={'c=d, ': 3, '\\\\': 3}) :: (a[, b])
|
||||
(a=1, b=2, c=3) :: ([a, b, c])
|
||||
(a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c])
|
||||
"""
|
||||
|
||||
TEST = [map(lambda x: x.strip(), x.split("::")) for x in TEST.split("\n")
|
||||
|
||||
Reference in New Issue
Block a user