Skip to content

Commit e593f84

Browse files
committed
Remove AstroidCacheSetupMixin
This class predates efforts to have a central interface to control global state (including caches) and it is no longer needed.
1 parent c3181c8 commit e593f84

File tree

6 files changed

+9
-41
lines changed

6 files changed

+9
-41
lines changed

astroid/interpreter/_import/spec.py

-4
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,3 @@ def _find_spec(args: SpecArgs) -> ModuleSpec:
482482
spec = spec._replace(submodule_search_locations=submodule_path)
483483

484484
return spec
485-
486-
487-
def clear_spec_cache() -> None:
488-
_find_spec.cache_clear()

astroid/manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,11 @@ def clear_cache(self) -> None:
442442
# pylint: disable=import-outside-toplevel
443443
from astroid.brain.helpers import register_all_brains
444444
from astroid.inference_tip import clear_inference_tip_cache
445-
from astroid.interpreter._import.spec import clear_spec_cache
445+
from astroid.interpreter._import.spec import _find_spec
446446
from astroid.interpreter.objectmodel import ObjectModel
447447
from astroid.nodes._base_nodes import LookupMixIn
448448
from astroid.nodes.scoped_nodes import ClassDef
449449

450-
clear_spec_cache()
451450
clear_inference_tip_cache()
452451
_invalidate_cache() # inference context cache
453452

@@ -461,6 +460,7 @@ def clear_cache(self) -> None:
461460
util.is_namespace,
462461
ObjectModel.attributes,
463462
ClassDef._metaclass_lookup_attribute,
463+
_find_spec,
464464
):
465465
lru_cache.cache_clear() # type: ignore[attr-defined]
466466

tests/resources.py

-26
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from pathlib import Path
1010

1111
from astroid import builder
12-
from astroid.manager import AstroidManager
1312
from astroid.nodes.scoped_nodes import Module
1413

1514
DATA_DIR = Path("testdata") / "python3"
@@ -34,28 +33,3 @@ def tearDown(self) -> None:
3433
for key in list(sys.path_importer_cache):
3534
if key.startswith(datadir):
3635
del sys.path_importer_cache[key]
37-
38-
39-
class AstroidCacheSetupMixin:
40-
"""Mixin for handling test isolation issues with the astroid cache.
41-
42-
When clearing the astroid cache, some tests fail due to
43-
cache inconsistencies, where some objects had a different
44-
builtins object referenced.
45-
This saves the builtins module and TransformVisitor and
46-
replaces them after the tests finish.
47-
The builtins module is special, since some of the
48-
transforms for a couple of its objects (str, bytes etc)
49-
are executed only once, so astroid_bootstrapping will be
50-
useless for retrieving the original builtins module.
51-
"""
52-
53-
@classmethod
54-
def setup_class(cls):
55-
cls._builtins = AstroidManager().astroid_cache.get("builtins")
56-
cls._transforms = AstroidManager.brain["_transform"]
57-
58-
@classmethod
59-
def teardown_class(cls):
60-
AstroidManager().astroid_cache["builtins"] = cls._builtins
61-
AstroidManager.brain["_transform"] = cls._transforms

tests/test_manager.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
AttributeInferenceError,
2424
)
2525
from astroid.interpreter._import import util
26-
from astroid.interpreter._import.spec import clear_spec_cache
2726
from astroid.modutils import EXT_LIB_DIRS, module_in_path
2827
from astroid.nodes import Const
2928
from astroid.nodes.scoped_nodes import ClassDef, Module
@@ -37,13 +36,11 @@ def _get_file_from_object(obj) -> str:
3736
return obj.__file__
3837

3938

40-
class AstroidManagerTest(
41-
resources.SysPathSetup, resources.AstroidCacheSetupMixin, unittest.TestCase
42-
):
39+
class AstroidManagerTest(resources.SysPathSetup, unittest.TestCase):
4340
def setUp(self) -> None:
4441
super().setUp()
45-
clear_spec_cache()
4642
self.manager = test_utils.brainless_manager()
43+
self.manager.clear_cache()
4744

4845
def test_ast_from_file(self) -> None:
4946
filepath = unittest.__file__
@@ -393,9 +390,10 @@ def test_denied_modules_raise(self) -> None:
393390
self.manager.ast_from_module_name("math")
394391

395392

396-
class IsolatedAstroidManagerTest(resources.AstroidCacheSetupMixin, unittest.TestCase):
393+
class IsolatedAstroidManagerTest(unittest.TestCase):
397394
def test_no_user_warning(self):
398395
mgr = manager.AstroidManager()
396+
self.addCleanup(mgr.clear_cache)
399397
with warnings.catch_warnings():
400398
warnings.filterwarnings("error", category=UserWarning)
401399
mgr.ast_from_module_name("setuptools")

tests/test_modutils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from astroid import modutils
2323
from astroid.const import PY310_PLUS
2424
from astroid.interpreter._import import spec
25-
from astroid.interpreter._import.spec import clear_spec_cache
2625

2726
from . import resources
2827

@@ -42,7 +41,7 @@ class ModuleFileTest(unittest.TestCase):
4241
package = "mypypa"
4342

4443
def tearDown(self) -> None:
45-
clear_spec_cache()
44+
astroid.MANAGER.clear_cache()
4645
for k in list(sys.path_importer_cache):
4746
if "MyPyPa" in k:
4847
del sys.path_importer_cache[k]

tests/test_regrtest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
HAS_NUMPY = True
2727

2828

29-
class NonRegressionTests(resources.AstroidCacheSetupMixin, unittest.TestCase):
29+
class NonRegressionTests(unittest.TestCase):
3030
def setUp(self) -> None:
3131
sys.path.insert(0, resources.find("data"))
3232
MANAGER.always_load_extensions = True
33+
self.addCleanup(MANAGER.clear_cache)
3334

3435
def tearDown(self) -> None:
3536
MANAGER.always_load_extensions = False

0 commit comments

Comments
 (0)