@@ -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
754754def 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
858862def load_stdlib_py_versions (custom_typeshed_dir : Optional [str ]) -> StdlibVersions :
0 commit comments