Skip to content

Commit 6635b47

Browse files
authored
Support for Python 3.11's "safe_path" in pyinfo (#13164)
1 parent 6f93fc1 commit 6635b47

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

mypy/pyinfo.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ def getsearchdirs():
3333
# containing pyinfo.py
3434
# - Otherwise, if mypy launched via console script, this is the directory of the script
3535
# - Otherwise, if mypy launched via python -m mypy, this is the current directory
36-
# In all cases, this is safe to drop
36+
# In all these cases, it is desirable to drop the first entry
3737
# Note that mypy adds the cwd to SearchPaths.python_path, so we still find things on the
3838
# cwd consistently (the return value here sets SearchPaths.package_path)
39-
abs_sys_path = (os.path.abspath(p) for p in sys.path[1:])
39+
40+
# Python 3.11 adds a "safe_path" flag wherein Python won't automatically prepend
41+
# anything to sys.path. In this case, the first entry of sys.path is no longer special.
42+
offset = 0 if sys.version_info >= (3, 11) and sys.flags.safe_path else 1
43+
44+
abs_sys_path = (os.path.abspath(p) for p in sys.path[offset:])
4045
return [p for p in abs_sys_path if p not in excludes]
4146

4247

0 commit comments

Comments
 (0)