Test full name for os.path imports. Fixes #873.

This commit is contained in:
Dave Halter
2017-04-05 00:59:50 +02:00
parent fe5eaaf56c
commit 4b841370e4
2 changed files with 18 additions and 4 deletions

View File

@@ -201,14 +201,21 @@ class Importer(object):
path = module_context.py__file__() path = module_context.py__file__()
if path is not None: if path is not None:
import_path = list(import_path) import_path = list(import_path)
p = path
for i in range(level): for i in range(level):
path = os.path.dirname(path) p = os.path.dirname(p)
dir_name = os.path.basename(path) dir_name = os.path.basename(p)
# This is not the proper way to do relative imports. However, since # This is not the proper way to do relative imports. However, since
# Jedi cannot be sure about the entry point, we just calculate an # Jedi cannot be sure about the entry point, we just calculate an
# absolute path here. # absolute path here.
if dir_name: if dir_name:
import_path.insert(0, dir_name) # TODO those sys.modules modifications are getting
# really stupid. this is the 3rd time that we're using
# this. We should probably refactor.
if path.endswith(os.path.sep + 'os.py'):
import_path.insert(0, 'os')
else:
import_path.insert(0, dir_name)
else: else:
_add_error(module_context, import_path[-1]) _add_error(module_context, import_path[-1])
import_path = [] import_path = []
@@ -428,7 +435,7 @@ class Importer(object):
if ('os',) == self.str_import_path and not self.level: if ('os',) == self.str_import_path and not self.level:
# os.path is a hardcoded exception, because it's a # os.path is a hardcoded exception, because it's a
# ``sys.modules`` modification. # ``sys.modules`` modification.
names.append(self._generate_name('path')) names.append(self._generate_name('path', context))
continue continue

View File

@@ -89,3 +89,10 @@ def test_sub_module():
assert [d.full_name for d in defs] == ['jedi.api.classes'] assert [d.full_name for d in defs] == ['jedi.api.classes']
defs = jedi.Script('import jedi.api; jedi.api').goto_definitions() defs = jedi.Script('import jedi.api; jedi.api').goto_definitions()
assert [d.full_name for d in defs] == ['jedi.api'] assert [d.full_name for d in defs] == ['jedi.api']
def test_os_path():
d, = jedi.Script('from os.path import join').completions()
assert d.full_name == 'os.path.join'
d, = jedi.Script('import os.p').completions()
assert d.full_name == 'os.path'