Anonymous avatar Anonymous committed 6751d9b

pyobjectsdef: added ignore_bad_imports project config

Comments (0)

Files changed (3)

rope/base/default_config.py

     # unit-tests use 4 spaces it is more reliable, too.
     prefs['indent_size'] = 4
 
-    # If `True` modules with syntax errors are considered to be empty.
-    # The default value is `False`; When `False` syntax errors raise
-    # `rope.base.exceptions.ModuleSyntaxError` exception.
-    prefs['ignore_syntax_errors'] = False
-
     # Builtin and c-extension modules that are allowed to be imported
     # and inspected by rope.
     prefs['extension_modules'] = [
         'itertools', 'marshal', 'math', 'mmap', 'nt', 'operator', 'posix',
         'pwd', 'select', 'signal', 'sys', 'thread', 'zlib', 'zipimport']
 
+    # If `True` modules with syntax errors are considered to be empty.
+    # The default value is `False`; When `False` syntax errors raise
+    # `rope.base.exceptions.ModuleSyntaxError` exception.
+    prefs['ignore_syntax_errors'] = False
+
+    # If `True`, rope ignores unresolvable imports.  Otherwise, they
+    # appear in the importing namespace.
+    prefs['ignore_bad_imports'] = False
+
 
 def project_opened(project):
     """This function is called after the project is opened"""

rope/base/pyobjectsdef.py

             alias = import_pair.asname
             first_package = module_name.split('.')[0]
             if alias is not None:
-                self.names[alias] = \
-                    pynames.ImportedModule(self.get_module(), module_name)
+                imported = pynames.ImportedModule(self.get_module(),
+                                                  module_name)
+                if not self._is_ignored_import(imported):
+                    self.names[alias] = imported
             else:
-                self.names[first_package] = \
-                    pynames.ImportedModule(self.get_module(), first_package)
+                imported = pynames.ImportedModule(self.get_module(),
+                                                  first_package)
+                if not self._is_ignored_import(imported):
+                    self.names[first_package] = imported
 
     def _ImportFrom(self, node):
         level = 0
             level = node.level
         imported_module = pynames.ImportedModule(self.get_module(),
                                                  node.module, level)
+        if self._is_ignored_import(imported_module):
+            return
         if len(node.names) == 1 and node.names[0].name == '*':
             if isinstance(self.owner_object, PyModule):
                 self.owner_object.star_imports.append(
                 self.names[imported] = pynames.ImportedName(imported_module,
                                                             imported_name.name)
 
+    def _is_ignored_import(self, imported_module):
+        if not self.pycore.project.prefs.get('ignore_bad_imports', False):
+            return False
+        return not isinstance(imported_module.get_object(), AbstractModule)
+
     def _Global(self, node):
         module = self.get_module()
         for name in node.names:

ropetest/pycoretest.py

         myscript = self.project.root.create_file('myscript')
         self.assertTrue(self.project.pycore.is_python_file(myscript))
 
+    def test_ignore_bad_imports(self):
+        self.project = testutils.sample_project(ignore_bad_imports=True)
+        pymod = self.project.pycore.get_string_module(
+            'import some_nonexistent_module\n')
+        self.assertFalse('some_nonexistent_module' in pymod)
+
+    def test_ignore_bad_imports_for_froms(self):
+        self.project = testutils.sample_project(ignore_bad_imports=True)
+        pymod = self.project.pycore.get_string_module(
+            'from some_nonexistent_module import var\n')
+        self.assertFalse('var' in pymod)
+
 
 def suite():
     result = unittest.TestSuite()
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.