fix(import): support for nested namespace packages

If multiple directories in sys.path provide a nested namespace
package, then jedi would only visit the first directory which
contained the package.  Fix this by saving the remaining path list in
the ImplicitNamespaceValue and add a test for it.
This commit is contained in:
Kirat Singh
2021-10-02 04:09:27 +00:00
parent 65bc1c117b
commit 53e837055f
4 changed files with 22 additions and 4 deletions

View File

@@ -422,11 +422,9 @@ def import_module(inference_state, import_names, parent_module_value, sys_path):
# The module might not be a package. # The module might not be a package.
return NO_VALUES return NO_VALUES
for path in paths: for i, path in enumerate(paths):
# At the moment we are only using one path. So this is
# not important to be correct.
if not isinstance(path, list): if not isinstance(path, list):
path = [path] path = paths[i:]
file_io_or_ns, is_pkg = inference_state.compiled_subprocess.get_module_info( file_io_or_ns, is_pkg = inference_state.compiled_subprocess.get_module_info(
string=import_names[-1], string=import_names[-1],
path=path, path=path,

View File

@@ -0,0 +1 @@
mod1_name = 'mod1'

View File

@@ -0,0 +1 @@
mod2_name = 'mod2'

View File

@@ -0,0 +1,18 @@
import sys
import os
from os.path import dirname
sys.path.insert(0, os.path.join(dirname(__file__), 'namespace2'))
sys.path.insert(0, os.path.join(dirname(__file__), 'namespace1'))
#? ['mod1']
import pkg1.pkg2.mod1
#? ['mod2']
import pkg1.pkg2.mod2
#? ['mod1_name']
pkg1.pkg2.mod1.mod1_name
#? ['mod2_name']
pkg1.pkg2.mod2.mod2_name