Remove AstroidCacheSetupMixin
This class predates efforts to have a central interface to control
global state (including caches) and it is no longer needed.
diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py
index e1c5ed0..77351c2 100644
--- a/astroid/interpreter/_import/spec.py
+++ b/astroid/interpreter/_import/spec.py
@@ -474,7 +474,3 @@
spec = spec._replace(submodule_search_locations=submodule_path)
return spec
-
-
-def clear_spec_cache() -> None:
- _find_spec.cache_clear()
diff --git a/astroid/manager.py b/astroid/manager.py
index 195ac66..fc30bf9 100644
--- a/astroid/manager.py
+++ b/astroid/manager.py
@@ -442,12 +442,11 @@
# pylint: disable=import-outside-toplevel
from astroid.brain.helpers import register_all_brains
from astroid.inference_tip import clear_inference_tip_cache
- from astroid.interpreter._import.spec import clear_spec_cache
+ from astroid.interpreter._import.spec import _find_spec
from astroid.interpreter.objectmodel import ObjectModel
from astroid.nodes._base_nodes import LookupMixIn
from astroid.nodes.scoped_nodes import ClassDef
- clear_spec_cache()
clear_inference_tip_cache()
_invalidate_cache() # inference context cache
@@ -461,6 +460,7 @@
util.is_namespace,
ObjectModel.attributes,
ClassDef._metaclass_lookup_attribute,
+ _find_spec,
):
lru_cache.cache_clear() # type: ignore[attr-defined]
diff --git a/tests/resources.py b/tests/resources.py
index 455dc6f..853fd79 100644
--- a/tests/resources.py
+++ b/tests/resources.py
@@ -9,7 +9,6 @@
from pathlib import Path
from astroid import builder
-from astroid.manager import AstroidManager
from astroid.nodes.scoped_nodes import Module
DATA_DIR = Path("testdata") / "python3"
@@ -34,28 +33,3 @@
for key in list(sys.path_importer_cache):
if key.startswith(datadir):
del sys.path_importer_cache[key]
-
-
-class AstroidCacheSetupMixin:
- """Mixin for handling test isolation issues with the astroid cache.
-
- When clearing the astroid cache, some tests fail due to
- cache inconsistencies, where some objects had a different
- builtins object referenced.
- This saves the builtins module and TransformVisitor and
- replaces them after the tests finish.
- The builtins module is special, since some of the
- transforms for a couple of its objects (str, bytes etc)
- are executed only once, so astroid_bootstrapping will be
- useless for retrieving the original builtins module.
- """
-
- @classmethod
- def setup_class(cls):
- cls._builtins = AstroidManager().astroid_cache.get("builtins")
- cls._transforms = AstroidManager.brain["_transform"]
-
- @classmethod
- def teardown_class(cls):
- AstroidManager().astroid_cache["builtins"] = cls._builtins
- AstroidManager.brain["_transform"] = cls._transforms
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 160fa94..c91ec0a 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -23,7 +23,6 @@
AttributeInferenceError,
)
from astroid.interpreter._import import util
-from astroid.interpreter._import.spec import clear_spec_cache
from astroid.modutils import EXT_LIB_DIRS, module_in_path
from astroid.nodes import Const
from astroid.nodes.scoped_nodes import ClassDef, Module
@@ -37,13 +36,11 @@
return obj.__file__
-class AstroidManagerTest(
- resources.SysPathSetup, resources.AstroidCacheSetupMixin, unittest.TestCase
-):
+class AstroidManagerTest(resources.SysPathSetup, unittest.TestCase):
def setUp(self) -> None:
super().setUp()
- clear_spec_cache()
self.manager = test_utils.brainless_manager()
+ self.manager.clear_cache()
def test_ast_from_file(self) -> None:
filepath = unittest.__file__
@@ -393,9 +390,10 @@
self.manager.ast_from_module_name("math")
-class IsolatedAstroidManagerTest(resources.AstroidCacheSetupMixin, unittest.TestCase):
+class IsolatedAstroidManagerTest(unittest.TestCase):
def test_no_user_warning(self):
mgr = manager.AstroidManager()
+ self.addCleanup(mgr.clear_cache)
with warnings.catch_warnings():
warnings.filterwarnings("error", category=UserWarning)
mgr.ast_from_module_name("setuptools")
diff --git a/tests/test_modutils.py b/tests/test_modutils.py
index be7095e..7921757 100644
--- a/tests/test_modutils.py
+++ b/tests/test_modutils.py
@@ -22,7 +22,6 @@
from astroid import modutils
from astroid.const import PY310_PLUS
from astroid.interpreter._import import spec
-from astroid.interpreter._import.spec import clear_spec_cache
from . import resources
@@ -42,7 +41,7 @@
package = "mypypa"
def tearDown(self) -> None:
- clear_spec_cache()
+ astroid.MANAGER.clear_cache()
for k in list(sys.path_importer_cache):
if "MyPyPa" in k:
del sys.path_importer_cache[k]
diff --git a/tests/test_regrtest.py b/tests/test_regrtest.py
index 67ccca6..45f241f 100644
--- a/tests/test_regrtest.py
+++ b/tests/test_regrtest.py
@@ -26,10 +26,11 @@
HAS_NUMPY = True
-class NonRegressionTests(resources.AstroidCacheSetupMixin, unittest.TestCase):
+class NonRegressionTests(unittest.TestCase):
def setUp(self) -> None:
sys.path.insert(0, resources.find("data"))
MANAGER.always_load_extensions = True
+ self.addCleanup(MANAGER.clear_cache)
def tearDown(self) -> None:
MANAGER.always_load_extensions = False