@@ -723,7 +723,7 @@ def default_lib_path(data_dir: str,
723
723
724
724
725
725
@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 ] ]:
727
727
"""Find package directories for given python.
728
728
729
729
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]:
732
732
"""
733
733
734
734
if python_executable is None :
735
- return []
735
+ return ([], [])
736
736
elif python_executable == sys .executable :
737
737
# Use running Python's package dirs
738
- sys_path = pyinfo .getsearchdirs ()
738
+ sys_path , site_packages = pyinfo .getsearchdirs ()
739
739
else :
740
740
# Use subprocess to get the package directory of given Python
741
741
# executable
742
742
try :
743
- sys_path = ast .literal_eval (
743
+ sys_path , site_packages = ast .literal_eval (
744
744
subprocess .check_output ([python_executable , pyinfo .__file__ , 'getsearchdirs' ],
745
745
stderr = subprocess .PIPE ).decode ())
746
746
except OSError as err :
747
747
reason = os .strerror (err .errno )
748
748
raise CompileError (
749
749
[f"mypy: Invalid python executable '{ python_executable } ': { reason } " ]
750
750
) from err
751
- return sys_path
751
+ return sys_path , site_packages
752
752
753
753
754
754
def add_py2_mypypath_entries (mypypath : List [str ]) -> List [str ]:
@@ -837,22 +837,26 @@ def compute_search_paths(sources: List[BuildSource],
837
837
if options .python_version [0 ] == 2 :
838
838
mypypath = add_py2_mypypath_entries (mypypath )
839
839
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 )
848
850
print ("See https://mypy.readthedocs.io/en/stable/running_mypy.html"
849
851
"#how-mypy-handles-imports for more info" , file = sys .stderr )
850
852
sys .exit (1 )
851
853
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
+ )
856
860
857
861
858
862
def load_stdlib_py_versions (custom_typeshed_dir : Optional [str ]) -> StdlibVersions :
0 commit comments