-
-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let AstroidManager.clear_cache
act on other caches
#1521
Changes from all commits
35171c4
0ea952c
f70ef40
0359025
0d4c0d3
37b1d91
5fd833a
c669f8d
b5034cd
e83701c
012f32f
7bbc702
8ee0632
46d8235
b8ac347
a88b481
4427b71
6f98e30
e0797d4
ea74ae4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,9 @@ | |
import sys | ||
import sysconfig | ||
import types | ||
from functools import lru_cache | ||
from pathlib import Path | ||
from typing import Dict, Set | ||
from typing import Set | ||
|
||
from astroid.const import IS_JYTHON, IS_PYPY | ||
from astroid.interpreter._import import spec, util | ||
|
@@ -138,21 +139,19 @@ def _handle_blacklist(blacklist, dirnames, filenames): | |
filenames.remove(norecurs) | ||
|
||
|
||
_NORM_PATH_CACHE: Dict[str, str] = {} | ||
@lru_cache() | ||
def _cache_normalize_path_(path: str) -> str: | ||
jacobtylerwalls marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return _normalize_path(path) | ||
|
||
|
||
def _cache_normalize_path(path: str) -> str: | ||
jacobtylerwalls marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""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. | ||
try: | ||
return _NORM_PATH_CACHE[path] | ||
except KeyError: | ||
if not path: # don't cache result for '' | ||
return _normalize_path(path) | ||
result = _NORM_PATH_CACHE[path] = _normalize_path(path) | ||
return result | ||
if not path: # don't cache result for '' | ||
return _normalize_path(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is not covered, on the other hand are we really ever having something falsey here ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added coverage. We talked a bit about it here. |
||
return _cache_normalize_path_(path) | ||
|
||
|
||
def load_module_from_name(dotted_name: str) -> types.ModuleType: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,7 +179,7 @@ def test_load_packages_without_init(self) -> None: | |
https://github.com/PyCQA/astroid/issues/1327 | ||
""" | ||
tmp_dir = Path(tempfile.gettempdir()) | ||
self.addCleanup(os.chdir, os.curdir) | ||
self.addCleanup(os.chdir, os.getcwd()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to actually find the cwd before changing dir, otherwise this cleanup won't do anything. |
||
os.chdir(tmp_dir) | ||
|
||
self.addCleanup(shutil.rmtree, tmp_dir / "src") | ||
|
@@ -288,6 +288,8 @@ def test_custom_path(self) -> None: | |
self.assertTrue( | ||
modutils.is_standard_module("data.module", (os.path.abspath(datadir),)) | ||
) | ||
# "" will evaluate to cwd | ||
self.assertTrue(modutils.is_standard_module("data.module", ("",))) | ||
|
||
def test_failing_edge_cases(self) -> None: | ||
# using a subpackage/submodule path as std_path argument | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if we could remove the dependency to wrapt following this change, Turns out that there are other uses elsewhere but not that much.