Skip to content

Commit feea767

Browse files
committed
Correct search order for installed packages
Due to the method of looking for installed packages, it was possible for the search to be unstable: an installed inline package would be chosen before an installed stub package. This corrects that by storing a seperate list for each, and putting the stub packages first. Fixes python#4876
1 parent 80aee58 commit feea767

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

mypy/build.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,10 @@ def _find_module(self, id: str, lib_path: Tuple[str, ...],
858858
dir_chain = os.sep.join(components[:-1]) # e.g., 'foo/bar'
859859
# TODO (ethanhs): refactor each path search to its own method with lru_cache
860860

861-
third_party_dirs = []
861+
# We have two sets of folders so that we collect *all* stubs folders and
862+
# put them in the front of the search path
863+
third_party_inline_dirs = []
864+
third_party_stubs_dirs = []
862865
# Third-party stub/typed packages
863866
for pkg_dir in _get_site_packages_dirs(python_executable):
864867
stub_name = components[0] + '-stubs'
@@ -868,11 +871,12 @@ def _find_module(self, id: str, lib_path: Tuple[str, ...],
868871
stub_components = [stub_name] + components[1:]
869872
path = os.path.join(pkg_dir, *stub_components[:-1])
870873
if fscache.isdir(path):
871-
third_party_dirs.append(path)
874+
third_party_stubs_dirs.append(path)
872875
elif fscache.isfile(typed_file):
873876
path = os.path.join(pkg_dir, dir_chain)
874-
third_party_dirs.append(path)
875-
candidate_base_dirs = self.find_lib_path_dirs(dir_chain, lib_path) + third_party_dirs
877+
third_party_inline_dirs.append(path)
878+
candidate_base_dirs = self.find_lib_path_dirs(dir_chain, lib_path) + \
879+
third_party_stubs_dirs + third_party_inline_dirs
876880

877881
# If we're looking for a module like 'foo.bar.baz', then candidate_base_dirs now
878882
# contains just the subdirectories 'foo/bar' that actually exist under the

0 commit comments

Comments
 (0)