summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeichi Takahashi <hello@keichi.dev>2021-12-15 22:32:23 +0900
committerGitHub <noreply@github.com>2021-12-15 14:32:23 +0100
commit2ee20ccdf62450db611acc4a1a7e42f407ce8a14 (patch)
treef2a6620ba74c95b960b98b7d79dd0f8465a328f0
parent5efb7e2ad30f7f0ee6c25f1407c555991730ae44 (diff)
downloadastroid-git-2ee20ccdf62450db611acc4a1a7e42f407ce8a14.tar.gz
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 <pierre.sassoulas@gmail.com> Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
-rw-r--r--ChangeLog10
-rw-r--r--astroid/modutils.py19
2 files changed, 19 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index 3b026ba9..af8dad12 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -50,6 +50,16 @@ Release date: 2021-11-21
Closes #1239
+* Resolve symlinks in the import path
+ Fixes inference error when the import path includes symlinks (e.g. Python
+ installed on macOS via Homebrew).
+
+ Closes #823
+ Closes PyCQA/pylint#3499
+ Closes PyCQA/pylint#4302
+ Closes PyCQA/pylint#4798
+ Closes PyCQA/pylint#5081
+
What's New in astroid 2.8.5?
============================
diff --git a/astroid/modutils.py b/astroid/modutils.py
index 141b18a6..b2a67b23 100644
--- a/astroid/modutils.py
+++ b/astroid/modutils.py
@@ -157,12 +157,12 @@ class NoSourceFile(Exception):
"""
-def _normalize_path(path):
- return os.path.normcase(os.path.abspath(path))
-
-
-def _canonicalize_path(path):
- return os.path.realpath(os.path.expanduser(path))
+def _normalize_path(path: str) -> str:
+ """Resolve symlinks in path and convert to absolute path.
+ Note that environment variables and ~ in the path need to be expanded in
+ advance.
+ """
+ return os.path.normcase(os.path.realpath(path))
def _path_from_filename(filename, is_jython=IS_JYTHON):
@@ -189,8 +189,8 @@ def _handle_blacklist(blacklist, dirnames, filenames):
_NORM_PATH_CACHE = {}
-def _cache_normalize_path(path):
- """abspath with caching"""
+def _cache_normalize_path(path: str) -> str:
+ """Normalize path with caching."""
# _module_file calls abspath on every path in sys.path every time it's
# called; on a larger codebase this easily adds up to half a second just
# assembling path components. This cache alleviates that.
@@ -302,9 +302,8 @@ def _get_relative_base_path(filename, path_to_check):
def modpath_from_file_with_callback(filename, path=None, is_package_cb=None):
filename = os.path.expanduser(_path_from_filename(filename))
for pathname in itertools.chain(
- path or [], map(_canonicalize_path, sys.path), sys.path
+ path or [], map(_cache_normalize_path, sys.path), sys.path
):
- pathname = _cache_normalize_path(pathname)
if not pathname:
continue
modpath = _get_relative_base_path(filename, pathname)