From a4a4142d26bcfa6bcd9cc735fca27b8143494b2c Mon Sep 17 00:00:00 2001 From: akamat10 Date: Sat, 21 Sep 2024 19:20:57 -0400 Subject: [PATCH 1/8] Fix bug with clear_cache not fully clearing the cache --- astroid/manager.py | 2 ++ tests/test_manager.py | 27 +++++++++++++++++++ tests/testdata/python3/data/cache/__init__.py | 0 tests/testdata/python3/data/cache/a.py | 1 + 4 files changed, 30 insertions(+) create mode 100644 tests/testdata/python3/data/cache/__init__.py create mode 100644 tests/testdata/python3/data/cache/a.py diff --git a/astroid/manager.py b/astroid/manager.py index 7ed4525b8c..3ed9ca39c4 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -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) diff --git a/tests/test_manager.py b/tests/test_manager.py index 34ddd06d4a..d5f0b82d76 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -12,6 +12,7 @@ from contextlib import contextmanager from unittest import mock +import astroid.manager import pytest import astroid @@ -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()") diff --git a/tests/testdata/python3/data/cache/__init__.py b/tests/testdata/python3/data/cache/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/testdata/python3/data/cache/a.py b/tests/testdata/python3/data/cache/a.py new file mode 100644 index 0000000000..62cfbc1191 --- /dev/null +++ b/tests/testdata/python3/data/cache/a.py @@ -0,0 +1 @@ +""" Test for clear cache. Doesn't need anything here" From e4169301776660d36106193945ff93af32d67a7b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 21 Sep 2024 23:37:03 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_manager.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_manager.py b/tests/test_manager.py index d5f0b82d76..b4e564351d 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -12,10 +12,10 @@ from contextlib import contextmanager from unittest import mock -import astroid.manager import pytest import astroid +import astroid.manager from astroid import manager, test_utils from astroid.const import IS_JYTHON, IS_PYPY, PY312_PLUS from astroid.exceptions import ( @@ -493,7 +493,7 @@ def test_clear_cache_clears_other_lru_caches(self) -> None: 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 + """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. @@ -501,9 +501,9 @@ def test_file_cache_after_clear_cache(self) -> None: orig_sys_path = sys.path[:] try: search_path = resources.RESOURCE_PATH - sys.path.insert(0,search_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") + 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") From c439550380c0580156b5b78cc7adc4122265b7d9 Mon Sep 17 00:00:00 2001 From: akamat10 Date: Sat, 21 Sep 2024 19:36:27 -0400 Subject: [PATCH 3/8] Update ChangeLog --- ChangeLog | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ChangeLog b/ChangeLog index 0651f5aa15..bef73fb39c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,9 @@ Release date: TBA Closes pylint-dev/pylint#9811 +* 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? ============================ From 8aa48873737fcbfc888951619a7cacec136d2b8c Mon Sep 17 00:00:00 2001 From: akamat10 Date: Sat, 21 Sep 2024 19:38:51 -0400 Subject: [PATCH 4/8] Fix linting issue --- tests/test_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_manager.py b/tests/test_manager.py index b4e564351d..2c47119ca8 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -507,7 +507,7 @@ def test_file_cache_after_clear_cache(self) -> None: # 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) + importfrom_node.do_import_module(importfrom_node.modname) finally: sys.path = orig_sys_path From 1b2825009ad18c677a3cafb29e77de73a4b93426 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 21 Sep 2024 23:40:38 +0000 Subject: [PATCH 5/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index bef73fb39c..9e9a78bc3c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,7 +18,7 @@ Release date: TBA Closes pylint-dev/pylint#9811 * 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? From f6a874aab725cb67530e489108b8299ef8cc5f8f Mon Sep 17 00:00:00 2001 From: akamat10 Date: Sat, 21 Sep 2024 19:47:01 -0400 Subject: [PATCH 6/8] fix linting issue, typo in a.py --- tests/test_manager.py | 1 - tests/testdata/python3/data/cache/a.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_manager.py b/tests/test_manager.py index 2c47119ca8..b19d2ad25a 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -15,7 +15,6 @@ import pytest import astroid -import astroid.manager from astroid import manager, test_utils from astroid.const import IS_JYTHON, IS_PYPY, PY312_PLUS from astroid.exceptions import ( diff --git a/tests/testdata/python3/data/cache/a.py b/tests/testdata/python3/data/cache/a.py index 62cfbc1191..9ddc377f39 100644 --- a/tests/testdata/python3/data/cache/a.py +++ b/tests/testdata/python3/data/cache/a.py @@ -1 +1 @@ -""" Test for clear cache. Doesn't need anything here" +""" Test for clear cache. Doesn't need anything here""" From d20716b82f21813c4a2591e02b77b73952489600 Mon Sep 17 00:00:00 2001 From: akamat10 Date: Sat, 21 Sep 2024 19:49:16 -0400 Subject: [PATCH 7/8] Remove duplicate fragment in Changelog --- ChangeLog | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9e9a78bc3c..6d7911cd1d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,10 +13,6 @@ What's New in astroid 3.3.4? ============================ Release date: TBA -* Fix inference regression with property setters - - Closes pylint-dev/pylint#9811 - * Fix bug with ``manager.clear_cache()`` not fully clearing cache Refs https://github.com/pylint-dev/pylint/pull/9932#issuecomment-2364985551 From ba5c1a129d336b62d197a77a19117240f2f4c99e Mon Sep 17 00:00:00 2001 From: akamat10 Date: Sat, 21 Sep 2024 20:07:45 -0400 Subject: [PATCH 8/8] Update test based on feedback --- tests/test_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_manager.py b/tests/test_manager.py index b19d2ad25a..9a7bbdb7ef 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -512,9 +512,9 @@ def test_file_cache_after_clear_cache(self) -> None: 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 = 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: