Skip to content

Commit

Permalink
Resolve symlinks in the import path (#1253)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Dec 15, 2021
1 parent 5efb7e2 commit 2ee20cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
============================
Expand Down
19 changes: 9 additions & 10 deletions astroid/modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2ee20cc

Please sign in to comment.