Use a different sys path for import completions and import type inference

Fix tests of the #1451 pull request
This commit is contained in:
Dave Halter
2019-12-01 00:10:11 +01:00
parent 1ba83414a5
commit 86071dda54
4 changed files with 19 additions and 8 deletions

View File

@@ -94,7 +94,8 @@ class Project(object):
return sys_path return sys_path
@inference_state_as_method_param_cache() @inference_state_as_method_param_cache()
def _get_sys_path(self, inference_state, environment=None, add_parent_paths=True): def _get_sys_path(self, inference_state, environment=None,
add_parent_paths=True, add_init_paths=False):
""" """
Keep this method private for all users of jedi. However internally this Keep this method private for all users of jedi. However internally this
one is used like a public method. one is used like a public method.
@@ -117,7 +118,8 @@ class Project(object):
for parent_path in traverse_parents(inference_state.script_path): for parent_path in traverse_parents(inference_state.script_path):
if not parent_path.startswith(self._path): if not parent_path.startswith(self._path):
break break
if os.path.isfile(os.path.join(parent_path, "__init__.py")): if not add_init_paths \
and os.path.isfile(os.path.join(parent_path, "__init__.py")):
continue continue
traversed.append(parent_path) traversed.append(parent_path)

View File

@@ -272,12 +272,15 @@ class Importer(object):
for name in self.import_path for name in self.import_path
) )
def _sys_path_with_modifications(self): def _sys_path_with_modifications(self, is_completion):
if self._fixed_sys_path is not None: if self._fixed_sys_path is not None:
return self._fixed_sys_path return self._fixed_sys_path
sys_path_mod = ( sys_path_mod = (
self._inference_state.get_sys_path() # For import completions we don't want to see init paths, but for
# inference we want to show the user as much as possible.
# See GH #1446.
self._inference_state.get_sys_path(add_init_paths=not is_completion)
+ sys_path.check_sys_path_modifications(self._module_context) + sys_path.check_sys_path_modifications(self._module_context)
) )
@@ -297,7 +300,7 @@ class Importer(object):
force_unicode(i.value if isinstance(i, tree.Name) else i) force_unicode(i.value if isinstance(i, tree.Name) else i)
for i in self.import_path for i in self.import_path
) )
sys_path = self._sys_path_with_modifications() sys_path = self._sys_path_with_modifications(is_completion=False)
value_set = [None] value_set = [None]
for i, name in enumerate(self.import_path): for i, name in enumerate(self.import_path):
@@ -326,7 +329,7 @@ class Importer(object):
for name in self._inference_state.compiled_subprocess.get_builtin_module_names()] for name in self._inference_state.compiled_subprocess.get_builtin_module_names()]
if search_path is None: if search_path is None:
search_path = self._sys_path_with_modifications() search_path = self._sys_path_with_modifications(is_completion=True)
for name in iter_module_names(self._inference_state, search_path): for name in iter_module_names(self._inference_state, search_path):
if in_module is None: if in_module is None:
@@ -355,7 +358,7 @@ class Importer(object):
extname = modname[len('flask_'):] extname = modname[len('flask_'):]
names.append(ImportName(self._module_context, extname)) names.append(ImportName(self._module_context, extname))
# Now the old style: ``flaskext.foo`` # Now the old style: ``flaskext.foo``
for dir in self._sys_path_with_modifications(): for dir in self._sys_path_with_modifications(is_completion=True):
flaskext = os.path.join(dir, 'flaskext') flaskext = os.path.join(dir, 'flaskext')
if os.path.isdir(flaskext): if os.path.isdir(flaskext):
names += self._get_module_names([flaskext]) names += self._get_module_names([flaskext])

View File

@@ -20,8 +20,12 @@ def builtin_test():
#? ['sqlite3'] #? ['sqlite3']
import sqlite3 import sqlite3
#? ['classes'] # classes is a local module that has an __init__.py and can therefore not be
# found. test can be found.
#? []
import classes import classes
#? ['test']
import test
#? ['timedelta'] #? ['timedelta']
from datetime import timedel from datetime import timedel

View File

@@ -115,6 +115,8 @@ def test_find_module_not_package_zipped(Script, inference_state, environment):
def test_import_not_in_sys_path(Script): def test_import_not_in_sys_path(Script):
""" """
non-direct imports (not in sys.path) non-direct imports (not in sys.path)
This is in the end just a fallback.
""" """
a = Script(path='module.py', line=5).goto_definitions() a = Script(path='module.py', line=5).goto_definitions()
assert a[0].name == 'int' assert a[0].name == 'int'