Anonymous avatar Anonymous committed 754430a

Type inferencing a_var = AClass()

Comments (0)

Files changed (4)

 * Having multiple clipboards @ 2
 
 
+* reST support inside pydocs @ 1
+
+
+* reST highlighting @ 2
+
+
+* reST outlines @ 3
+
+
+* reSt codeassists @ 3
+
+
 * Adding tool bar @ 1
 
 
   Writing ASTs
 
 
+* Rename class @ 3
+
+
 * We know the type of ``var = AClass()`` @ 2
 
 
-* Rename class @ 3
-
-
 Remaining Stories
 =================
 * Change program goals and description; principles.html

docs/workingon.txt

-Refactoring
-===========
+We know the type of ``var = AClass()`` @ 2
+==========================================
+
+- Classes defined in a bigger scope
+
+* Assigns in the class bodies
+* Equality of two objects of the same type?
 
 * Only looking in the holding scope range
 * Using LineOrientedSourceTools in GUI; requires it to use Lines
 * PyObject equality check; Value objects?
 * PyName equality check; Value objects?
 
+* Optimizing holding scope search ?
 * Elimination of source_code.split('\n')
-* The same pattern for module names; ~ing or ~ or ~er
 * Refactor `_ScopeVisitor.visitFrom` and visitImport
 * A TreeViewer is an EnhancedList
 * Folders and change_observers?
   * Specifing the type of each story; UI or Core
   * Dividing week time; 5/7 for core and 2/7 for UI
 
-* reST support for text files; It can have outlines, codeassists and ...
-* reST support inside pydocs
+* The same pattern for module names; ~ing or ~ or ~er
 
     def visitGetattr(self, node):
         compiler.walk(node.expr, self)
         self.name_list.append(node.attrname)
+
+    @staticmethod
+    def get_attribute_list(node):
+        finder = _AttributeListFinder()
+        compiler.walk(node, finder)
+        return finder.name_list        
         
     @staticmethod
     def get_attribute(node, scope):
         pyobject = PyFunction(self.pycore, node, self.owner_object)
         self.names[node.name] = PyName(pyobject, True)
 
-    def visitAssName(self, node):
-        if node.name in self.names:
-            self.names[node.name].update_object(lineno=node.lineno)
-        else:
-            self.names[node.name] = PyName(lineno=node.lineno)
+    def _search_in_dictionary_for_attribute_list(self, names, attribute_list):
+        pyobject = names.get(attribute_list[0], None)
+        if pyobject != None and len(attribute_list) > 1:
+            for name in attribute_list[1:]:
+                if name in pyobject.get_attributes():
+                    pyobject = pyobject.get_attributes()[name].object
+                else:
+                    pyobject = None
+                    break
+        return pyobject
+        
+    def visitAssign(self, node):
+        type_ = None
+        if isinstance(node.expr, compiler.ast.CallFunc):
+            function_name = _AttributeListFinder.get_attribute_list(node.expr.node)
+            function_object = self._search_in_dictionary_for_attribute_list(self.names, function_name)
+            if type_ is None and self.owner_object.parent is not None:
+                function_object = self._search_in_dictionary_for_attribute_list(self.owner_object.parent.\
+                                                                                get_scope().get_names(), 
+                                                                                function_name)
+            if function_object is not None:
+                if function_object.get_type() == PyObject.get_base_type('Type'):
+                    type_ = function_object.get_object()
+        object_ = PyObject(type_=type_)
+        for ass_name in node.nodes:
+            if ass_name.name in self.names:
+                self.names[ass_name.name].update_object(object_=object_, lineno=node.lineno)
+            else:
+                self.names[ass_name.name] = PyName(object_=object_, lineno=node.lineno)
+
 
     def visitImport(self, node):
         for import_pair in node.names:

ropetest/pycoretest.py

         an_attr = a_class.get_attributes()['an_attr']
         self.assertEquals((None, 3), an_attr.get_definition_location())
 
+    def test_simple_type_inferencing(self):
+        pycore = self.project.get_pycore()
+        scope = pycore.get_string_scope('class Sample(object):\n    pass\na_var = Sample()\n')
+        sample_class = scope.get_names()['Sample'].get_object()
+        a_var = scope.get_names()['a_var']
+        self.assertEquals(sample_class, a_var.get_type())
+        
+    def test_simple_type_inferencing_classes_defined_in_holding_scope(self):
+        pycore = self.project.get_pycore()
+        scope = pycore.get_string_scope('class Sample(object):\n    pass\n' +
+                                        'def a_func():\n    a_var = Sample()\n')
+        sample_class = scope.get_names()['Sample'].get_object()
+        a_var = scope.get_names()['a_func'].get_object().get_scope().get_names()['a_var']
+        self.assertEquals(sample_class, a_var.get_type())
+        
 
 class PyCoreInProjectsTest(unittest.TestCase):
 
         runner.wait_process()
         self.assertEquals('run', self.get_output_file_content(file_path))
 
-
 def suite():
     result = unittest.TestSuite()
     result.addTests(unittest.makeSuite(PyCoreTest))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.