Commits

Anonymous committed ef97876

Support -> return type annotations.

  • Participants
  • Parent commits 55f7974

Comments (0)

Files changed (4)

 
 * The new TextBuilder creates plain-text output.
 
+* Python 3-style signatures, giving a return annotation via ``->``,
+  are now supported.
+
 * Extensions:
     
   - The `autodoc` extension accepts signatures for functions, methods

sphinx/directives/desc.py

 
 # ------ functions to parse a Python or C signature and create desc_* nodes.
 
-py_sig_re = re.compile(r'''^([\w.]*\.)?        # class names
-                           (\w+)  \s*          # thing name
-                           (?: \((.*)\) )? $   # optionally arguments
-                        ''', re.VERBOSE)
+py_sig_re = re.compile(
+    r'''^ ([\w.]*\.)?            # class name(s)
+          (\w+)  \s*             # thing name
+          (?: \((.*)\)           # optional arguments
+          (\s* -> \s* .*)? )? $  # optional return annotation
+          ''', re.VERBOSE)
 
 py_paramlist_re = re.compile(r'([\[\],])')  # split at '[', ']' and ','
 
     m = py_sig_re.match(sig)
     if m is None:
         raise ValueError
-    classname, name, arglist = m.groups()
+    classname, name, arglist, retann = m.groups()
 
     if env.currclass:
         add_module = False
             stack[-1] += addnodes.desc_parameter(token, token)
     if len(stack) != 1:
         raise ValueError
+    if retann:
+        retann = u' \N{RIGHTWARDS ARROW} ' + retann.strip()[2:]
+        signode += addnodes.desc_type(retann, retann)
     return fullname, classname
 
 

sphinx/ext/autodoc.py

     # first, parse the definition -- auto directives for classes and functions
     # can contain a signature which is then used instead of an autogenerated one
     try:
-        path, base, signature = py_sig_re.match(name).groups()
+        path, base, signature, retann = py_sig_re.match(name).groups()
     except:
         warning = document.reporter.warning(
             'invalid signature for auto%s (%r)' % (what, name), line=lineno)
     if signature is not None:
         # signature given explicitly -- the parentheses were stripped by the regex
         args = '(%s)' % signature
+        if retann:
+            args += retann
     else:
         try:
             args = format_signature(what, todoc)

sphinx/latexwriter.py

         (u">", ur"\textgreater{}"),
         (u"^", ur"\textasciicircum{}"),
         (u"\x00", ur"\textbackslash{}"),
+        (u"\N{RIGHTWARDS ARROW}", ur"$\rightarrow$"),
     ]
 
     def encode(self, text):