Added code to correctly parse Python methods/functions containing return annotations.

Thu, 11 Dec 2014 19:51:05 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 11 Dec 2014 19:51:05 +0100
changeset 3966
969a3d41c402
parent 3962
b75057280077
child 3967
383a043d62ec

Added code to correctly parse Python methods/functions containing return annotations.

UI/BrowserModel.py file | annotate | diff | comparison | revisions
Utilities/ClassBrowsers/ClbrBaseClasses.py file | annotate | diff | comparison | revisions
Utilities/ClassBrowsers/pyclbr.py file | annotate | diff | comparison | revisions
Utilities/ModuleParser.py file | annotate | diff | comparison | revisions
--- a/UI/BrowserModel.py	Sat Dec 06 20:19:07 2014 +0100
+++ b/UI/BrowserModel.py	Thu Dec 11 19:51:05 2014 +0100
@@ -1348,6 +1348,9 @@
                 self.icon = UI.PixmapCache.getIcon("method.png")
             self.itemData[0] = "{0}({1})".format(
                 name, ", ".join(self._classObject.parameters))
+            if self._classObject.annotation:
+                self.itemData[0] = "{0} {1}".format(
+                    self.itemData[0], self._classObject.annotation)
             # if no defaults are wanted
             # ....format(name,
             #            ", ".join([e.split('=')[0].strip() \
@@ -1473,6 +1476,9 @@
             self.icon = UI.PixmapCache.getIcon("method.png")
         self.itemData[0] = "{0}({1})".format(
             name, ", ".join(self._functionObject.parameters))
+        if self._functionObject.annotation:
+            self.itemData[0] = "{0} {1}".format(
+                self.itemData[0], self._functionObject.annotation)
         # if no defaults are wanted
         # ....format(name,
         #            ", ".join([e.split('=')[0].strip()
--- a/Utilities/ClassBrowsers/ClbrBaseClasses.py	Sat Dec 06 20:19:07 2014 +0100
+++ b/Utilities/ClassBrowsers/ClbrBaseClasses.py	Thu Dec 11 19:51:05 2014 +0100
@@ -255,7 +255,7 @@
     Class = 2
     
     def __init__(self, module, name, file, lineno, signature='', separator=',',
-                 modifierType=General):
+                 modifierType=General, annotation=""):
         """
         Constructor
         
@@ -266,10 +266,12 @@
         @param signature parameterlist of the method
         @param separator string separating the parameters
         @param modifierType type of the function
+        @param annotation return annotation
         """
         ClbrBase.__init__(self, module, name, file, lineno)
         self.parameters = [e.strip() for e in signature.split(separator)]
         self.modifier = modifierType
+        self.annotation = annotation
 
 
 class Coding(ClbrBase):
--- a/Utilities/ClassBrowsers/pyclbr.py	Sat Dec 06 20:19:07 2014 +0100
+++ b/Utilities/ClassBrowsers/pyclbr.py	Thu Dec 11 19:51:05 2014 +0100
@@ -69,7 +69,9 @@
         (?: [ \t]* \[ (?: plain | html ) \] )?
         [ \t]* \(
         (?P<MethodSignature> (?: [^)] | \)[ \t]*,? )*? )
-        \) [ \t]* :
+        \) [ \t]*
+        (?P<MethodReturnAnnotation> (?: -> [ \t]* [^:]+ )? )
+        [ \t]* :
     )
 
 |   (?P<Class>
@@ -152,7 +154,7 @@
     Class to represent a Python function.
     """
     def __init__(self, module, name, file, lineno, signature='', separator=',',
-                 modifierType=ClbrBaseClasses.Function.General):
+                 modifierType=ClbrBaseClasses.Function.General, annotation=""):
         """
         Constructor
         
@@ -163,9 +165,11 @@
         @param signature parameterlist of the method
         @param separator string separating the parameters
         @param modifierType type of the function
+        @param annotation return annotation
         """
         ClbrBaseClasses.Function.__init__(self, module, name, file, lineno,
-                                          signature, separator, modifierType)
+                                          signature, separator, modifierType,
+                                          annotation)
         VisibilityMixin.__init__(self)
 
 
@@ -297,6 +301,9 @@
             meth_sig = m.group("MethodSignature")
             meth_sig = meth_sig.replace('\\\n', '')
             meth_sig = _commentsub('', meth_sig)
+            meth_ret = m.group("MethodReturnAnnotation")
+            meth_ret = meth_ret.replace('\\\n', '')
+            meth_ret = _commentsub('', meth_ret)
             lineno = lineno + src.count('\n', last_lineno_pos, start)
             last_lineno_pos = start
             if modifierType and modifierIndent == thisindent:
@@ -336,12 +343,14 @@
                 if cur_class:
                     # it's a method/nested def
                     f = Function(None, meth_name,
-                                 file, lineno, meth_sig, modifierType=modifier)
+                                 file, lineno, meth_sig, annotation=meth_ret,
+                                 modifierType=modifier)
                     cur_class._addmethod(meth_name, f)
             else:
                 # it's a function
                 f = Function(module, meth_name,
-                             file, lineno, meth_sig, modifierType=modifier)
+                             file, lineno, meth_sig, annotation=meth_ret,
+                             modifierType=modifier)
                 if meth_name in dict_counts:
                     dict_counts[meth_name] += 1
                     meth_name = "{0}_{1:d}".format(
--- a/Utilities/ModuleParser.py	Sat Dec 06 20:19:07 2014 +0100
+++ b/Utilities/ModuleParser.py	Thu Dec 11 19:51:05 2014 +0100
@@ -139,7 +139,9 @@
         (?: [ \t]* \[ (?: plain | html ) \] )?
         [ \t]* \(
         (?P<MethodSignature> (?: [^)] | \)[ \t]*,? )*? )
-        \) [ \t]* :
+        \) [ \t]*
+        (?P<MethodReturnAnnotation> (?: -> [ \t]* [^:]+ )? )
+        [ \t]* :
     )
 
 |   (?P<Class>
@@ -538,6 +540,8 @@
                 meth_name = m.group("MethodName")
                 meth_sig = m.group("MethodSignature")
                 meth_sig = meth_sig.replace('\\\n', '')
+                meth_ret = m.group("MethodReturnAnnotation")
+                meth_ret = meth_ret.replace('\\\n', '')
                 if m.group("MethodPyQtSignature") is not None:
                     meth_pyqtSig = m.group("MethodPyQtSignature")\
                                     .replace('\\\n', '')\
@@ -595,7 +599,8 @@
                             # it's a class method
                             f = Function(
                                 None, meth_name, None, lineno,
-                                meth_sig, meth_pyqtSig, modifierType=modifier)
+                                meth_sig, meth_pyqtSig, modifierType=modifier,
+                                annotation=meth_ret)
                             self.__py_setVisibility(f)
                             cur_class.addMethod(meth_name, f)
                             break
@@ -603,13 +608,15 @@
                         # it's a nested function of a module function
                         f = Function(
                             self.name, meth_name, self.file, lineno,
-                            meth_sig, meth_pyqtSig, modifierType=modifier)
+                            meth_sig, meth_pyqtSig, modifierType=modifier,
+                            annotation=meth_ret)
                         self.__py_setVisibility(f)
                         self.addFunction(meth_name, f)
                 else:
                     # it's a module function
                     f = Function(self.name, meth_name, self.file, lineno,
-                                 meth_sig, meth_pyqtSig, modifierType=modifier)
+                                 meth_sig, meth_pyqtSig, modifierType=modifier,
+                                 annotation=meth_ret)
                     self.__py_setVisibility(f)
                     self.addFunction(meth_name, f)
                 if not classstack:
@@ -1354,7 +1361,7 @@
     Class = 2
     
     def __init__(self, module, name, file, lineno, signature='',
-                 pyqtSignature=None, modifierType=General):
+                 pyqtSignature=None, modifierType=General, annotation=""):
         """
         Constructor
         
@@ -1365,6 +1372,7 @@
         @param signature the functions call signature (string)
         @param pyqtSignature the functions PyQt signature (string)
         @param modifierType type of the function
+        @param annotation return annotation
         """
         self.module = module
         self.name = name
@@ -1376,6 +1384,7 @@
         self.description = ""
         self.pyqtSignature = pyqtSignature
         self.modifier = modifierType
+        self.annotation = annotation
         self.setPublic()
     
     def addDescription(self, description):

eric ide

mercurial