Skip to content

Commit 2ee20cc

Browse files
keichiPierre-SassoulasDanielNoord
authored
Resolve symlinks in the import path (#1253)
* Resolve symlinks in the import path * Drop expanduser() from _normalize_path() and add note Co-authored-by: Pierre Sassoulas <[email protected]> Co-authored-by: Daniël van Noord <[email protected]>
1 parent 5efb7e2 commit 2ee20cc

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ Release date: 2021-11-21
5050

5151
Closes #1239
5252

53+
* Resolve symlinks in the import path
54+
Fixes inference error when the import path includes symlinks (e.g. Python
55+
installed on macOS via Homebrew).
56+
57+
Closes #823
58+
Closes PyCQA/pylint#3499
59+
Closes PyCQA/pylint#4302
60+
Closes PyCQA/pylint#4798
61+
Closes PyCQA/pylint#5081
62+
5363

5464
What's New in astroid 2.8.5?
5565
============================

astroid/modutils.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ class NoSourceFile(Exception):
157157
"""
158158

159159

160-
def _normalize_path(path):
161-
return os.path.normcase(os.path.abspath(path))
162-
163-
164-
def _canonicalize_path(path):
165-
return os.path.realpath(os.path.expanduser(path))
160+
def _normalize_path(path: str) -> str:
161+
"""Resolve symlinks in path and convert to absolute path.
162+
Note that environment variables and ~ in the path need to be expanded in
163+
advance.
164+
"""
165+
return os.path.normcase(os.path.realpath(path))
166166

167167

168168
def _path_from_filename(filename, is_jython=IS_JYTHON):
@@ -189,8 +189,8 @@ def _handle_blacklist(blacklist, dirnames, filenames):
189189
_NORM_PATH_CACHE = {}
190190

191191

192-
def _cache_normalize_path(path):
193-
"""abspath with caching"""
192+
def _cache_normalize_path(path: str) -> str:
193+
"""Normalize path with caching."""
194194
# _module_file calls abspath on every path in sys.path every time it's
195195
# called; on a larger codebase this easily adds up to half a second just
196196
# assembling path components. This cache alleviates that.
@@ -302,9 +302,8 @@ def _get_relative_base_path(filename, path_to_check):
302302
def modpath_from_file_with_callback(filename, path=None, is_package_cb=None):
303303
filename = os.path.expanduser(_path_from_filename(filename))
304304
for pathname in itertools.chain(
305-
path or [], map(_canonicalize_path, sys.path), sys.path
305+
path or [], map(_cache_normalize_path, sys.path), sys.path
306306
):
307-
pathname = _cache_normalize_path(pathname)
308307
if not pathname:
309308
continue
310309
modpath = _get_relative_base_path(filename, pathname)

0 commit comments

Comments
 (0)