Skip to content

Commit 70eb5ef

Browse files
Merge branch 'main' into empty-format-spec
2 parents 848edd7 + 58286a1 commit 70eb5ef

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

astroid/manager.py

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

471471
self.astroid_cache.clear()
472+
self._mod_file_cache.clear()
473+
472474
# NB: not a new TransformVisitor()
473475
AstroidManager.brain["_transform"].transforms = collections.defaultdict(list)
474476

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)