Skip to content

Commit f191166

Browse files
akamat10jacobtylerwalls
authored andcommitted
Fix manager.clear_cache() not fully clearing the module cache (#2572)
(cherry picked from commit 58286a1)
1 parent a01a9c9 commit f191166

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ What's New in astroid 3.3.4?
1313
============================
1414
Release date: TBA
1515

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

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

1820
What's New in astroid 3.3.3?
1921
============================

astroid/manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ def clear_cache(self) -> None:
464464
_invalidate_cache() # inference context cache
465465

466466
self.astroid_cache.clear()
467+
self._mod_file_cache.clear()
468+
467469
# NB: not a new TransformVisitor()
468470
AstroidManager.brain["_transform"].transforms = collections.defaultdict(list)
469471

tests/test_manager.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,32 @@ def test_clear_cache_clears_other_lru_caches(self) -> None:
491491
# less equal because the "baseline" might have had multiple calls to bootstrap()
492492
self.assertLessEqual(cleared_cache.currsize, baseline_cache.currsize)
493493

494+
def test_file_cache_after_clear_cache(self) -> None:
495+
"""Test to mimic the behavior of how pylint lints file and
496+
ensure clear cache clears everything stored in the cache.
497+
See https://github.com/pylint-dev/pylint/pull/9932#issuecomment-2364985551
498+
for more information.
499+
"""
500+
orig_sys_path = sys.path[:]
501+
try:
502+
search_path = resources.RESOURCE_PATH
503+
sys.path.insert(0, search_path)
504+
node = astroid.MANAGER.ast_from_file(resources.find("data/cache/a.py"))
505+
self.assertEqual(node.name, "cache.a")
506+
507+
# This import from statement should succeed and update the astroid cache
508+
importfrom_node = astroid.extract_node("from cache import a")
509+
importfrom_node.do_import_module(importfrom_node.modname)
510+
finally:
511+
sys.path = orig_sys_path
512+
513+
astroid.MANAGER.clear_cache()
514+
515+
importfrom_node = astroid.extract_node("from cache import a")
516+
# Try import from again after clear cache, this should raise an error
517+
with self.assertRaises(AstroidBuildingError):
518+
importfrom_node.do_import_module(importfrom_node.modname)
519+
494520
def test_brain_plugins_reloaded_after_clearing_cache(self) -> None:
495521
astroid.MANAGER.clear_cache()
496522
format_call = astroid.extract_node("''.format()")

tests/testdata/python3/data/cache/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
""" Test for clear cache. Doesn't need anything here"""

0 commit comments

Comments
 (0)