From c2fd7b3104232bb65de8736658ec09ea742d437b Mon Sep 17 00:00:00 2001 From: Sam Roeca Date: Fri, 29 Nov 2019 21:12:12 -0500 Subject: [PATCH] Fix: upward search omits unnecessary paths In the previous implementation, Jedi's traverse_parents function traversed parent directories to the system root every time. This would inadvertently add every folder to the system root every time. Obviously, this is not the behavior desired for the import system. This commit collects directories in an upward search until we: 1. Hit any directory without an __init__.py, AND 2. Are above self._path. --- jedi/api/project.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/jedi/api/project.py b/jedi/api/project.py index f39ba90f..6ce05270 100644 --- a/jedi/api/project.py +++ b/jedi/api/project.py @@ -110,7 +110,17 @@ class Project(object): suffixed += discover_buildout_paths(inference_state, inference_state.script_path) if add_parent_paths: - traversed = list(traverse_parents(inference_state.script_path)) + # Collect directories in upward search until we: + # 1. Hit any directory without an __init__.py, AND + # 2. Are above self._path. + traversed = [] + no_init = False + for parent_path in traverse_parents(inference_state.script_path): + if not os.path.isfile(os.path.join(parent_path, "__init__.py")): + no_init = True + if no_init and not parent_path.startswith(self._path): + break + traversed.append(parent_path) # AFAIK some libraries have imports like `foo.foo.bar`, which # leads to the conclusion to by default prefer longer paths