Skip to content
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

[Backport maintenance/3.3.x] Fix manager.clear_cache() not fully clearing the module cache (#2572) #2573

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ What's New in astroid 3.3.4?
============================
Release date: TBA

* Fix bug with ``manager.clear_cache()`` not fully clearing cache

Refs https://github.com/pylint-dev/pylint/pull/9932#issuecomment-2364985551

What's New in astroid 3.3.3?
============================
Expand Down
2 changes: 2 additions & 0 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ def clear_cache(self) -> None:
_invalidate_cache() # inference context cache

self.astroid_cache.clear()
self._mod_file_cache.clear()

# NB: not a new TransformVisitor()
AstroidManager.brain["_transform"].transforms = collections.defaultdict(list)

Expand Down
26 changes: 26 additions & 0 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,32 @@ def test_clear_cache_clears_other_lru_caches(self) -> None:
# less equal because the "baseline" might have had multiple calls to bootstrap()
self.assertLessEqual(cleared_cache.currsize, baseline_cache.currsize)

def test_file_cache_after_clear_cache(self) -> None:
"""Test to mimic the behavior of how pylint lints file and
ensure clear cache clears everything stored in the cache.
See https://github.com/pylint-dev/pylint/pull/9932#issuecomment-2364985551
for more information.
"""
orig_sys_path = sys.path[:]
try:
search_path = resources.RESOURCE_PATH
sys.path.insert(0, search_path)
node = astroid.MANAGER.ast_from_file(resources.find("data/cache/a.py"))
self.assertEqual(node.name, "cache.a")

# This import from statement should succeed and update the astroid cache
importfrom_node = astroid.extract_node("from cache import a")
importfrom_node.do_import_module(importfrom_node.modname)
finally:
sys.path = orig_sys_path

astroid.MANAGER.clear_cache()

importfrom_node = astroid.extract_node("from cache import a")
# Try import from again after clear cache, this should raise an error
with self.assertRaises(AstroidBuildingError):
importfrom_node.do_import_module(importfrom_node.modname)

def test_brain_plugins_reloaded_after_clearing_cache(self) -> None:
astroid.MANAGER.clear_cache()
format_call = astroid.extract_node("''.format()")
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/testdata/python3/data/cache/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""" Test for clear cache. Doesn't need anything here"""
Loading