From 53e837055fedb735126409c3321ee94583e8634e Mon Sep 17 00:00:00 2001 From: Kirat Singh Date: Sat, 2 Oct 2021 04:09:27 +0000 Subject: [PATCH] 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. --- jedi/inference/imports.py | 6 ++---- test/completion/namespace1/pkg1/pkg2/mod1.py | 1 + test/completion/namespace2/pkg1/pkg2/mod2.py | 1 + test/completion/ns_path.py | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 test/completion/namespace1/pkg1/pkg2/mod1.py create mode 100644 test/completion/namespace2/pkg1/pkg2/mod2.py create mode 100644 test/completion/ns_path.py diff --git a/jedi/inference/imports.py b/jedi/inference/imports.py index 56d6ebb9..2cb8c28b 100644 --- a/jedi/inference/imports.py +++ b/jedi/inference/imports.py @@ -422,11 +422,9 @@ def import_module(inference_state, import_names, parent_module_value, sys_path): # The module might not be a package. return NO_VALUES - for path in paths: - # At the moment we are only using one path. So this is - # not important to be correct. + for i, path in enumerate(paths): if not isinstance(path, list): - path = [path] + path = paths[i:] file_io_or_ns, is_pkg = inference_state.compiled_subprocess.get_module_info( string=import_names[-1], path=path, diff --git a/test/completion/namespace1/pkg1/pkg2/mod1.py b/test/completion/namespace1/pkg1/pkg2/mod1.py new file mode 100644 index 00000000..84d27838 --- /dev/null +++ b/test/completion/namespace1/pkg1/pkg2/mod1.py @@ -0,0 +1 @@ +mod1_name = 'mod1' diff --git a/test/completion/namespace2/pkg1/pkg2/mod2.py b/test/completion/namespace2/pkg1/pkg2/mod2.py new file mode 100644 index 00000000..75dac324 --- /dev/null +++ b/test/completion/namespace2/pkg1/pkg2/mod2.py @@ -0,0 +1 @@ +mod2_name = 'mod2' diff --git a/test/completion/ns_path.py b/test/completion/ns_path.py new file mode 100644 index 00000000..419c1bd7 --- /dev/null +++ b/test/completion/ns_path.py @@ -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