Skip to content

Commit

Permalink
Correct search order for installed packages (python#4878)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
emmatyping authored and gvanrossum committed Apr 9, 2018
1 parent 22e49cc commit 1fd5a30
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,10 @@ def _find_module(self, id: str, lib_path: Tuple[str, ...],
dir_chain = os.sep.join(components[:-1]) # e.g., 'foo/bar'
# TODO (ethanhs): refactor each path search to its own method with lru_cache

third_party_dirs = []
# We have two sets of folders so that we collect *all* stubs folders and
# put them in the front of the search path
third_party_inline_dirs = []
third_party_stubs_dirs = []
# Third-party stub/typed packages
for pkg_dir in _get_site_packages_dirs(python_executable):
stub_name = components[0] + '-stubs'
Expand All @@ -868,11 +871,12 @@ def _find_module(self, id: str, lib_path: Tuple[str, ...],
stub_components = [stub_name] + components[1:]
path = os.path.join(pkg_dir, *stub_components[:-1])
if fscache.isdir(path):
third_party_dirs.append(path)
third_party_stubs_dirs.append(path)
elif fscache.isfile(typed_file):
path = os.path.join(pkg_dir, dir_chain)
third_party_dirs.append(path)
candidate_base_dirs = self.find_lib_path_dirs(dir_chain, lib_path) + third_party_dirs
third_party_inline_dirs.append(path)
candidate_base_dirs = self.find_lib_path_dirs(dir_chain, lib_path) + \
third_party_stubs_dirs + third_party_inline_dirs

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

0 comments on commit 1fd5a30

Please sign in to comment.