From 21bd50c6083ce63f1dd5bd028ddc2669edd48ba5 Mon Sep 17 00:00:00 2001 From: David Halter Date: Thu, 27 Dec 2012 15:30:43 +0100 Subject: [PATCH] follow 'from . import variable', fixes parts of davidhalter/jedi-vim#56 --- jedi/imports.py | 34 +++++++++++++++++++++++----------- test/completion/__init__.py | 3 +++ test/completion/goto.py | 3 +++ 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 test/completion/__init__.py diff --git a/jedi/imports.py b/jedi/imports.py index 678ba303..66486a89 100644 --- a/jedi/imports.py +++ b/jedi/imports.py @@ -187,16 +187,20 @@ class ImportPath(parsing.Base): """ Find a module with a path (of the module, like usb.backend.libusb10). """ - def follow_str(ns, string): - debug.dbg('follow_module', ns, string) + def get_relative_path(): + module = self.import_stmt.get_parent_until() + path = os.path.abspath(module.path) + for i in range(self.import_stmt.relative_count): + path = os.path.dirname(path) + return path + + def follow_str(ns_path, string): + debug.dbg('follow_module', ns_path, string) path = None - if ns: - path = ns[1] + if ns_path: + path = ns_path elif self.import_stmt.relative_count: - module = self.import_stmt.get_parent_until() - path = os.path.abspath(module.path) - for i in range(self.import_stmt.relative_count): - path = os.path.dirname(path) + path = get_relative_path() global imports_processed imports_processed += 1 @@ -221,14 +225,22 @@ class ImportPath(parsing.Base): else: sys_path_mod = list(builtin.get_sys_path()) - current_namespace = None + current_namespace = (None, None, None) # now execute those paths rest = [] for i, s in enumerate(self.import_path): try: - current_namespace = follow_str(current_namespace, s) + current_namespace = follow_str(current_namespace[1], s) except ImportError: - if current_namespace: + if self.import_stmt.relative_count \ + and len(self.import_path) == 1: + # follow `from . import some_variable` + try: + current_namespace = follow_str(get_relative_path(), + '__init__') + except ImportError: + pass + if current_namespace[1]: rest = self.import_path[i:] else: raise ModuleNotFound( diff --git a/test/completion/__init__.py b/test/completion/__init__.py new file mode 100644 index 00000000..374dd947 --- /dev/null +++ b/test/completion/__init__.py @@ -0,0 +1,3 @@ +""" needed for some modules to test against packages. """ + +some_variable = 1 diff --git a/test/completion/goto.py b/test/completion/goto.py index 101199ce..451effdf 100644 --- a/test/completion/goto.py +++ b/test/completion/goto.py @@ -118,6 +118,9 @@ from import_tree.pkg.mod1 import a #! ['import os'] from .imports import os +#! ['some_variable=1'] +from . import some_variable + # ----------------- # anonymous classes # -----------------