Skip to content

Commit

Permalink
Fix manager.clear_cache() not fully clearing the module cache (#2572)…
Browse files Browse the repository at this point in the history
… (#2573)

(cherry picked from commit 58286a1)

Co-authored-by: Akhil Kamat <[email protected]>
  • Loading branch information
jacobtylerwalls and akamat10 authored Sep 22, 2024
1 parent 875651b commit 1915cc3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
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"""

0 comments on commit 1915cc3

Please sign in to comment.