diff --git a/ChangeLog b/ChangeLog index 1ad303a15..6d7911cd1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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? ============================ diff --git a/astroid/manager.py b/astroid/manager.py index e7c2c806f..e5398c45a 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -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) diff --git a/tests/test_manager.py b/tests/test_manager.py index 34ddd06d4..9a7bbdb7e 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -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()") diff --git a/tests/testdata/python3/data/cache/__init__.py b/tests/testdata/python3/data/cache/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/testdata/python3/data/cache/a.py b/tests/testdata/python3/data/cache/a.py new file mode 100644 index 000000000..9ddc377f3 --- /dev/null +++ b/tests/testdata/python3/data/cache/a.py @@ -0,0 +1 @@ +""" Test for clear cache. Doesn't need anything here"""