Commits

Georg Brandl committed eff4f4b

Change the way to handle return annotations a bit.

Comments (0)

Files changed (6)

sphinx/addnodes.py

 class desc_addname(nodes.Part, nodes.Inline, nodes.TextElement): pass
 # compatibility alias
 desc_classname = desc_addname
-# return type (C); object type, e.g. -> annotation (Python)
+# return type (C); object type
 class desc_type(nodes.Part, nodes.Inline, nodes.TextElement): pass
+# -> annotation (Python)
+class desc_returns(desc_type):
+    def astext(self):
+        return ' -> ' + nodes.TextElement.astext(self)
 # main name of object
 class desc_name(nodes.Part, nodes.Inline, nodes.TextElement): pass
 # argument list
 
 # make them known to docutils. this is needed, because the HTML writer
 # will choke at some point if these are not added
-nodes._add_node_class_names("""index desc desc_content desc_signature desc_type
+nodes._add_node_class_names("""index desc desc_content desc_signature
+      desc_type desc_returns
       desc_addname desc_name desc_parameterlist desc_parameter desc_optional
       centered versionmodified seealso productionlist production toctree
       pending_xref compact_paragraph highlightlang literal_emphasis

sphinx/directives/desc.py

 py_sig_re = re.compile(
     r'''^ ([\w.]*\.)?            # class name(s)
           (\w+)  \s*             # thing name
-          (?: \((.*)\)           # optional arguments
-          (\s* -> \s* .*)? )? $  # optional return annotation
+          (?: \((.*)\)           # optional: arguments
+           (?:\s* -> \s* (.*))?  #           return annotation
+          )? $                   # and nothing more
           ''', re.VERBOSE)
 
 py_paramlist_re = re.compile(r'([\[\],])')  # split at '[', ']' and ','
         raise ValueError
     classname, name, arglist, retann = m.groups()
 
-    if retann:
-        retann = u' \N{RIGHTWARDS ARROW} ' + retann.strip()[2:]
-
     if env.currclass:
         add_module = False
         if classname and classname.startswith(env.currclass):
             # for callables, add an empty parameter list
             signode += addnodes.desc_parameterlist()
         if retann:
-            signode += addnodes.desc_type(retann, retann)
+            signode += addnodes.desc_returns(retann, retann)
         return fullname, classname
     signode += addnodes.desc_parameterlist()
 
     if len(stack) != 1:
         raise ValueError
     if retann:
-        signode += addnodes.desc_type(retann, retann)
+        signode += addnodes.desc_returns(retann, retann)
     return fullname, classname
 
 

sphinx/ext/autodoc.py

     r'''^ ([\w.]+::)?            # explicit module name
           ([\w.]+\.)?            # module and/or class name(s)
           (\w+)  \s*             # thing name
-          (?: \((.*)\)           # optional arguments
-          (\s* -> \s* .*)? )? $  # optional return annotation
+          (?: \((.*)\)           # optional: arguments
+           (?:\s* -> \s* (.*))?  #           return annotation
+          )? $                   # and nothing more
           ''', re.VERBOSE)
 
 
             args, retann = result
 
         if args is not None:
-            return '%s%s' % (args, retann or '')
+            return '%s%s' % (args, retann and (' -> %s' % retann) or '')
         elif err:
             # re-raise the error for perusal of the handler in generate()
             raise RuntimeError(err)

sphinx/writers/html.py

     def depart_desc_type(self, node):
         pass
 
+    def visit_desc_returns(self, node):
+        self.body.append(' → ')
+    def depart_desc_returns(self, node):
+        pass
+
     def visit_desc_name(self, node):
         self.body.append(self.starttag(node, 'tt', '', CLASS='descname'))
     def depart_desc_name(self, node):

sphinx/writers/latex.py

 class Desc(object):
     def __init__(self, node):
         self.env = LaTeXTranslator.desc_map.get(node['desctype'], 'describe')
-        self.type = self.cls = self.name = self.params = self.annotation = ''
+        self.type = self.cls = self.name = self.params = \
+                    self.annotation = self.returns = ''
         self.count = 0
 
 
             self.descstack[-1].type = self.encode(node.astext().strip())
         raise nodes.SkipNode
 
+    def visit_desc_returns(self, node):
+        d = self.descstack[-1]
+        if d.env == 'describe':
+            d.name += ' $\\rightarrow$ ' + self.encode(node.astext())
+        else:
+            self.descstack[-1].returns = self.encode(node.astext().strip())
+        raise nodes.SkipNode
+
     def visit_desc_name(self, node):
         d = self.descstack[-1]
         if d.env == 'describe':

sphinx/writers/text.py

     def depart_desc_type(self, node):
         pass
 
+    def visit_desc_returns(self, node):
+        self.add_text(' -> ')
+    def depart_desc_returns(self, node):
+        pass
+
     def visit_desc_parameterlist(self, node):
         self.add_text('(')
         self.first_param = 1