Commits

Anonymous committed 4d46760

Reviewing ImportTools.organize_imports()

  • Participants
  • Parent commits ada445f

Comments (0)

Files changed (4)

File docs/dev/workingon.txt

 Handling Imports When Inlining
 ==============================
 
-- Adding new arguements to `ImportTools.organize_imports`
-- Not using the default organize imports in `move`
+- Review `ImportTools.organize_imports`
+
+* Refactoring `importutils`
+* Sorting imports in inline and move
 
 * Inlining and relative imports
 * Making `inline` classes something like `move` module
 * Adding `handle_imports` option to inline method; 3 recompilation
-* Adding `get_imports(pycore, pydefined)` helper?
-* Removing `ImportTools.get_module_imports()`
-* Sorting imports in inline and move
-
-* Moving `get_imported_resource()` to `ImportInfo` hierarchy
-* Review `ImportTools.organize_imports`
-* Refactoring `importutils`
 
 * What to do with files that cannot be compiled when refactoring?
 * Extracting subexpressions; look at `extracttest` for more info

File rope/refactor/importutils/__init__.py

     def organize_imports(self, pymodule,
                          unused=True, duplicates=True,
                          selfs=True, sort=True):
-        module_imports = self.get_module_imports(pymodule)
-        if unused:
-            module_imports.remove_unused_imports()
-        if duplicates:
-            module_imports.remove_duplicates()
-        source = module_imports.get_changed_source()
-        if source is not None:
-            pymodule = self.pycore.get_string_module(source, pymodule.get_resource())
+        if unused or duplicates:
+            module_imports = self.get_module_imports(pymodule)
+            if unused:
+                module_imports.remove_unused_imports()
+            if duplicates:
+                module_imports.remove_duplicates()
+            source = module_imports.get_changed_source()
+            if source is not None:
+                pymodule = self.pycore.get_string_module(
+                    source, pymodule.get_resource())
         if selfs:
             pymodule = self._remove_self_imports(pymodule)
         if sort:

File rope/refactor/importutils/importinfo.py

                 result.append(name)
         return result
 
+    def get_imported_resource(self, context):
+        """Get the imported resource
+
+        Returns `None` if module was not found.
+        """
+        if self.level == 0:
+            return context.pycore.find_module(
+                self.module_name, current_folder=context.folder)
+        else:
+            return context.pycore.find_relative_module(
+                self.module_name, context.folder, self.level)
+
     def get_imported_module(self, context):
+        """Get the imported `PyModule`
+
+        Raises `rope.base.exceptions.ModuleNotFoundError` if module
+        could not be found.
+        """
         if self.level == 0:
             return context.pycore.get_module(
                 self.module_name, context.folder)
             return context.pycore.get_relative_module(
                 self.module_name, context.folder, self.level)
 
-    def get_imported_resource(self, context):
-        if self.level == 0:
-            return context.pycore.find_module(
-                self.module_name, current_folder=context.folder)
-        else:
-            return context.pycore.find_relative_module(
-                self.module_name, context.folder, self.level)
-
     def get_import_statement(self):
         result = 'from ' + '.' * self.level + self.module_name + ' import '
         for name, alias in self.names_and_aliases:

File rope/refactor/inline.py

             job_set.finished_job()
 
     def _add_imports(self, source, file):
+        if not self.imports:
+            return source
         pymodule = self.pycore.get_string_module(source, file)
         module_import = importutils.get_module_imports(self.pycore, pymodule)
         for import_info in self.imports: