Skip to content

Commit

Permalink
Fix bug with clear_cache not fully clearing the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
akamat10 committed Sep 21, 2024
1 parent 1368be1 commit a4a4142
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,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
27 changes: 27 additions & 0 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from contextlib import contextmanager
from unittest import mock

import astroid.manager
import pytest

import astroid
Expand Down Expand Up @@ -491,6 +492,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")
module = importfrom_node.do_import_module(importfrom_node.modname)
finally:
sys.path = orig_sys_path

astroid.MANAGER.clear_cache()

# Try import from again after clear cache, this should raise an error
with self.assertRaises(AstroidBuildingError):
importfrom_node = astroid.extract_node("from cache import a")
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 a4a4142

Please sign in to comment.