follow 'from . import variable', fixes parts of davidhalter/jedi-vim#56

This commit is contained in:
David Halter
2012-12-27 15:30:43 +01:00
parent 31b335dc08
commit 21bd50c608
3 changed files with 29 additions and 11 deletions

View File

@@ -187,16 +187,20 @@ class ImportPath(parsing.Base):
""" """
Find a module with a path (of the module, like usb.backend.libusb10). Find a module with a path (of the module, like usb.backend.libusb10).
""" """
def follow_str(ns, string): def get_relative_path():
debug.dbg('follow_module', ns, string)
path = None
if ns:
path = ns[1]
elif self.import_stmt.relative_count:
module = self.import_stmt.get_parent_until() module = self.import_stmt.get_parent_until()
path = os.path.abspath(module.path) path = os.path.abspath(module.path)
for i in range(self.import_stmt.relative_count): for i in range(self.import_stmt.relative_count):
path = os.path.dirname(path) 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:
path = ns_path
elif self.import_stmt.relative_count:
path = get_relative_path()
global imports_processed global imports_processed
imports_processed += 1 imports_processed += 1
@@ -221,14 +225,22 @@ class ImportPath(parsing.Base):
else: else:
sys_path_mod = list(builtin.get_sys_path()) sys_path_mod = list(builtin.get_sys_path())
current_namespace = None current_namespace = (None, None, None)
# now execute those paths # now execute those paths
rest = [] rest = []
for i, s in enumerate(self.import_path): for i, s in enumerate(self.import_path):
try: try:
current_namespace = follow_str(current_namespace, s) current_namespace = follow_str(current_namespace[1], s)
except ImportError: 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:] rest = self.import_path[i:]
else: else:
raise ModuleNotFound( raise ModuleNotFound(

View File

@@ -0,0 +1,3 @@
""" needed for some modules to test against packages. """
some_variable = 1

View File

@@ -118,6 +118,9 @@ from import_tree.pkg.mod1 import a
#! ['import os'] #! ['import os']
from .imports import os from .imports import os
#! ['some_variable=1']
from . import some_variable
# ----------------- # -----------------
# anonymous classes # anonymous classes
# ----------------- # -----------------