Skip to content

Commit

Permalink
Remove AstroidCacheSetupMixin
Browse files Browse the repository at this point in the history
This class predates efforts to have a central interface to control
global state (including caches) and it is no longer needed.
  • Loading branch information
crazybolillo committed May 4, 2024
1 parent 69299fc commit 98a0380
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 41 deletions.
4 changes: 0 additions & 4 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,3 @@ def _find_spec(modpath: tuple, path: tuple) -> ModuleSpec:
spec = spec._replace(submodule_search_locations=submodule_path)

return spec


def clear_spec_cache() -> None:
_find_spec.cache_clear()
4 changes: 2 additions & 2 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,11 @@ def clear_cache(self) -> None:
# 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

Expand All @@ -461,6 +460,7 @@ def clear_cache(self) -> None:
util.is_namespace,
ObjectModel.attributes,
ClassDef._metaclass_lookup_attribute,
_find_spec,
):
lru_cache.cache_clear() # type: ignore[attr-defined]

Expand Down
26 changes: 0 additions & 26 deletions tests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,28 +33,3 @@ def tearDown(self) -> None:
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
10 changes: 4 additions & 6 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,13 +36,11 @@ def _get_file_from_object(obj) -> str:
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__
Expand Down Expand Up @@ -393,9 +390,10 @@ def test_denied_modules_raise(self) -> None:
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")
Expand Down
3 changes: 1 addition & 2 deletions tests/test_modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -42,7 +41,7 @@ class ModuleFileTest(unittest.TestCase):
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]
Expand Down
3 changes: 2 additions & 1 deletion tests/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98a0380

Please sign in to comment.