Skip to content

Commit

Permalink
Handle cwd correctly in pyinfo
Browse files Browse the repository at this point in the history
This fixes a recent regression introduced by the change to
use sys.path

Fixes #12956
  • Loading branch information
hauntsaninja committed Jul 17, 2022
1 parent b13a450 commit db69bf8
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions mypy/pyinfo.py
Expand Up @@ -10,9 +10,6 @@
import sys
import sysconfig

if __name__ == '__main__':
sys.path = sys.path[1:] # we don't want to pick up mypy.types

MYPY = False
if MYPY:
from typing import List
Expand All @@ -29,10 +26,16 @@ def getsearchdirs():
)
stdlib = sysconfig.get_path("stdlib")
stdlib_ext = os.path.join(stdlib, "lib-dynload")
cwd = os.path.abspath(os.getcwd())
excludes = set([cwd, stdlib_zip, stdlib, stdlib_ext])

abs_sys_path = (os.path.abspath(p) for p in sys.path)
excludes = set([stdlib_zip, stdlib, stdlib_ext])

# Drop the first entry of sys.path
# - If pyinfo.py is executed as a script, this is the directory containing pyinfo.py
# - Otherwise, if mypy launched via console script, this is the directory of the script
# - Otherwise, if mypy launched via python -m mypy, this is the current directory
# In all cases, this is safe to drop
# Note that mypy adds the cwd to SearchPaths.python_path, so we still find things on the
# cwd consistently (pyinfo.py sets SearchPaths.package_path)
abs_sys_path = (os.path.abspath(p) for p in sys.path[1:])
return [p for p in abs_sys_path if p not in excludes]


Expand Down

0 comments on commit db69bf8

Please sign in to comment.