Skip to content

Commit 2a519c0

Browse files
authored
Fix site package on MYPYPATH check (#13223)
This was a regression in 0.971 Fixes #13214
1 parent 4cc3f9a commit 2a519c0

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

mypy/main.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1051,11 +1051,10 @@ def set_strict_flags() -> None:
10511051
# Set target.
10521052
if special_opts.modules + special_opts.packages:
10531053
options.build_type = BuildType.MODULE
1054-
search_dirs = get_search_dirs(options.python_executable)
1055-
search_paths = SearchPaths((os.getcwd(),),
1056-
tuple(mypy_path() + options.mypy_path),
1057-
tuple(search_dirs),
1058-
())
1054+
sys_path, _ = get_search_dirs(options.python_executable)
1055+
search_paths = SearchPaths(
1056+
(os.getcwd(),), tuple(mypy_path() + options.mypy_path), tuple(sys_path), ()
1057+
)
10591058
targets = []
10601059
# TODO: use the same cache that the BuildManager will
10611060
cache = FindModuleCache(search_paths, fscache, options)

mypy/modulefinder.py

+21-17
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ def default_lib_path(data_dir: str,
723723

724724

725725
@functools.lru_cache(maxsize=None)
726-
def get_search_dirs(python_executable: Optional[str]) -> List[str]:
726+
def get_search_dirs(python_executable: Optional[str]) -> Tuple[List[str], List[str]]:
727727
"""Find package directories for given python.
728728
729729
This runs a subprocess call, which generates a list of the directories in sys.path.
@@ -732,23 +732,23 @@ def get_search_dirs(python_executable: Optional[str]) -> List[str]:
732732
"""
733733

734734
if python_executable is None:
735-
return []
735+
return ([], [])
736736
elif python_executable == sys.executable:
737737
# Use running Python's package dirs
738-
sys_path = pyinfo.getsearchdirs()
738+
sys_path, site_packages = pyinfo.getsearchdirs()
739739
else:
740740
# Use subprocess to get the package directory of given Python
741741
# executable
742742
try:
743-
sys_path = ast.literal_eval(
743+
sys_path, site_packages = ast.literal_eval(
744744
subprocess.check_output([python_executable, pyinfo.__file__, 'getsearchdirs'],
745745
stderr=subprocess.PIPE).decode())
746746
except OSError as err:
747747
reason = os.strerror(err.errno)
748748
raise CompileError(
749749
[f"mypy: Invalid python executable '{python_executable}': {reason}"]
750750
) from err
751-
return sys_path
751+
return sys_path, site_packages
752752

753753

754754
def add_py2_mypypath_entries(mypypath: List[str]) -> List[str]:
@@ -837,22 +837,26 @@ def compute_search_paths(sources: List[BuildSource],
837837
if options.python_version[0] == 2:
838838
mypypath = add_py2_mypypath_entries(mypypath)
839839

840-
search_dirs = get_search_dirs(options.python_executable)
841-
for search_dir in search_dirs:
842-
assert search_dir not in lib_path
843-
if (search_dir in mypypath or
844-
any(p.startswith(search_dir + os.path.sep) for p in mypypath) or
845-
(os.path.altsep
846-
and any(p.startswith(search_dir + os.path.altsep) for p in mypypath))):
847-
print(f"{search_dir} is in the MYPYPATH. Please remove it.", file=sys.stderr)
840+
sys_path, site_packages = get_search_dirs(options.python_executable)
841+
# We only use site packages for this check
842+
for site in site_packages:
843+
assert site not in lib_path
844+
if (
845+
site in mypypath
846+
or any(p.startswith(site + os.path.sep) for p in mypypath)
847+
or (os.path.altsep and any(p.startswith(site + os.path.altsep) for p in mypypath))
848+
):
849+
print(f"{site} is in the MYPYPATH. Please remove it.", file=sys.stderr)
848850
print("See https://mypy.readthedocs.io/en/stable/running_mypy.html"
849851
"#how-mypy-handles-imports for more info", file=sys.stderr)
850852
sys.exit(1)
851853

852-
return SearchPaths(python_path=tuple(reversed(python_path)),
853-
mypy_path=tuple(mypypath),
854-
package_path=tuple(search_dirs),
855-
typeshed_path=tuple(lib_path))
854+
return SearchPaths(
855+
python_path=tuple(reversed(python_path)),
856+
mypy_path=tuple(mypypath),
857+
package_path=tuple(sys_path + site_packages),
858+
typeshed_path=tuple(lib_path),
859+
)
856860

857861

858862
def load_stdlib_py_versions(custom_typeshed_dir: Optional[str]) -> StdlibVersions:

mypy/pyinfo.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
possible.
88
"""
99
import os
10+
import site
1011
import sys
1112
import sysconfig
1213

1314
MYPY = False
1415
if MYPY:
15-
from typing import List
16+
from typing import Tuple, List
1617

1718
if __name__ == '__main__':
1819
# HACK: We don't want to pick up mypy.types as the top-level types
@@ -24,7 +25,22 @@
2425
sys.path = old_sys_path
2526

2627

27-
def getsearchdirs():
28+
def getsitepackages():
29+
# type: () -> List[str]
30+
res = []
31+
if hasattr(site, "getsitepackages"):
32+
res.extend(site.getsitepackages())
33+
34+
if hasattr(site, "getusersitepackages") and site.ENABLE_USER_SITE:
35+
res.insert(0, site.getusersitepackages())
36+
else:
37+
from distutils.sysconfig import get_python_lib
38+
39+
res = [get_python_lib()]
40+
return res
41+
42+
43+
def getsyspath():
2844
# type: () -> List[str]
2945
# Do not include things from the standard library
3046
# because those should come from typeshed.
@@ -54,6 +70,14 @@ def getsearchdirs():
5470
return [p for p in abs_sys_path if p not in excludes]
5571

5672

73+
def getsearchdirs():
74+
# type: () -> Tuple[List[str], List[str]]
75+
return (
76+
getsyspath(),
77+
getsitepackages(),
78+
)
79+
80+
5781
if __name__ == '__main__':
5882
if sys.argv[-1] == 'getsearchdirs':
5983
print(repr(getsearchdirs()))

0 commit comments

Comments
 (0)