mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Support -> return type annotations.
This commit is contained in:
parent
242e343c10
commit
061d9c205f
3
CHANGES
3
CHANGES
@ -57,6 +57,9 @@ New features added
|
|||||||
|
|
||||||
* The new TextBuilder creates plain-text output.
|
* The new TextBuilder creates plain-text output.
|
||||||
|
|
||||||
|
* Python 3-style signatures, giving a return annotation via ``->``,
|
||||||
|
are now supported.
|
||||||
|
|
||||||
* Extensions:
|
* Extensions:
|
||||||
|
|
||||||
- The `autodoc` extension accepts signatures for functions, methods
|
- The `autodoc` extension accepts signatures for functions, methods
|
||||||
|
@ -75,10 +75,12 @@ def desc_index_text(desctype, module, name):
|
|||||||
|
|
||||||
# ------ functions to parse a Python or C signature and create desc_* nodes.
|
# ------ functions to parse a Python or C signature and create desc_* nodes.
|
||||||
|
|
||||||
py_sig_re = re.compile(r'''^([\w.]*\.)? # class names
|
py_sig_re = re.compile(
|
||||||
(\w+) \s* # thing name
|
r'''^ ([\w.]*\.)? # class name(s)
|
||||||
(?: \((.*)\) )? $ # optionally arguments
|
(\w+) \s* # thing name
|
||||||
''', re.VERBOSE)
|
(?: \((.*)\) # optional arguments
|
||||||
|
(\s* -> \s* .*)? )? $ # optional return annotation
|
||||||
|
''', re.VERBOSE)
|
||||||
|
|
||||||
py_paramlist_re = re.compile(r'([\[\],])') # split at '[', ']' and ','
|
py_paramlist_re = re.compile(r'([\[\],])') # split at '[', ']' and ','
|
||||||
|
|
||||||
@ -94,7 +96,7 @@ def parse_py_signature(signode, sig, desctype, module, env):
|
|||||||
m = py_sig_re.match(sig)
|
m = py_sig_re.match(sig)
|
||||||
if m is None:
|
if m is None:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
classname, name, arglist = m.groups()
|
classname, name, arglist, retann = m.groups()
|
||||||
|
|
||||||
if env.currclass:
|
if env.currclass:
|
||||||
add_module = False
|
add_module = False
|
||||||
@ -148,6 +150,9 @@ def parse_py_signature(signode, sig, desctype, module, env):
|
|||||||
stack[-1] += addnodes.desc_parameter(token, token)
|
stack[-1] += addnodes.desc_parameter(token, token)
|
||||||
if len(stack) != 1:
|
if len(stack) != 1:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
if retann:
|
||||||
|
retann = u' \N{RIGHTWARDS ARROW} ' + retann.strip()[2:]
|
||||||
|
signode += addnodes.desc_type(retann, retann)
|
||||||
return fullname, classname
|
return fullname, classname
|
||||||
|
|
||||||
|
|
||||||
|
@ -198,7 +198,7 @@ def generate_rst(what, name, members, options, add_content, document, lineno,
|
|||||||
# first, parse the definition -- auto directives for classes and functions
|
# first, parse the definition -- auto directives for classes and functions
|
||||||
# can contain a signature which is then used instead of an autogenerated one
|
# can contain a signature which is then used instead of an autogenerated one
|
||||||
try:
|
try:
|
||||||
path, base, signature = py_sig_re.match(name).groups()
|
path, base, signature, retann = py_sig_re.match(name).groups()
|
||||||
except:
|
except:
|
||||||
warning = document.reporter.warning(
|
warning = document.reporter.warning(
|
||||||
'invalid signature for auto%s (%r)' % (what, name), line=lineno)
|
'invalid signature for auto%s (%r)' % (what, name), line=lineno)
|
||||||
@ -290,6 +290,8 @@ def generate_rst(what, name, members, options, add_content, document, lineno,
|
|||||||
if signature is not None:
|
if signature is not None:
|
||||||
# signature given explicitly -- the parentheses were stripped by the regex
|
# signature given explicitly -- the parentheses were stripped by the regex
|
||||||
args = '(%s)' % signature
|
args = '(%s)' % signature
|
||||||
|
if retann:
|
||||||
|
args += retann
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
args = format_signature(what, todoc)
|
args = format_signature(what, todoc)
|
||||||
|
@ -979,6 +979,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
(u">", ur"\textgreater{}"),
|
(u">", ur"\textgreater{}"),
|
||||||
(u"^", ur"\textasciicircum{}"),
|
(u"^", ur"\textasciicircum{}"),
|
||||||
(u"\x00", ur"\textbackslash{}"),
|
(u"\x00", ur"\textbackslash{}"),
|
||||||
|
(u"\N{RIGHTWARDS ARROW}", ur"$\rightarrow$"),
|
||||||
]
|
]
|
||||||
|
|
||||||
def encode(self, text):
|
def encode(self, text):
|
||||||
|
Loading…
Reference in New Issue
Block a user