Skip to content

Commit 2132036

Browse files
authored
Handle cwd correctly in pyinfo (#13161) (#13162)
This fixes a recent regression introduced by the change to use sys.path Fixes #12956
1 parent ee10d29 commit 2132036

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Diff for: mypy/pyinfo.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
import sys
1111
import sysconfig
1212

13-
if __name__ == '__main__':
14-
sys.path = sys.path[1:] # we don't want to pick up mypy.types
15-
1613
MYPY = False
1714
if MYPY:
1815
from typing import List
@@ -29,10 +26,17 @@ def getsearchdirs():
2926
)
3027
stdlib = sysconfig.get_path("stdlib")
3128
stdlib_ext = os.path.join(stdlib, "lib-dynload")
32-
cwd = os.path.abspath(os.getcwd())
33-
excludes = set([cwd, stdlib_zip, stdlib, stdlib_ext])
34-
35-
abs_sys_path = (os.path.abspath(p) for p in sys.path)
29+
excludes = set([stdlib_zip, stdlib, stdlib_ext])
30+
31+
# Drop the first entry of sys.path
32+
# - If pyinfo.py is executed as a script (in a subprocess), this is the directory
33+
# containing pyinfo.py
34+
# - Otherwise, if mypy launched via console script, this is the directory of the script
35+
# - Otherwise, if mypy launched via python -m mypy, this is the current directory
36+
# In all cases, this is safe to drop
37+
# Note that mypy adds the cwd to SearchPaths.python_path, so we still find things on the
38+
# cwd consistently (the return value here sets SearchPaths.package_path)
39+
abs_sys_path = (os.path.abspath(p) for p in sys.path[1:])
3640
return [p for p in abs_sys_path if p not in excludes]
3741

3842

0 commit comments

Comments
 (0)