Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let AstroidManager.clear_cache reload brain plugins #1528

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Release date: TBA

Closes #1512

* Allowed ``AstroidManager.clear_cache`` to reload necessary brain plugins.

* Rename ``ModuleSpec`` -> ``module_type`` constructor parameter to match attribute
name and improve typing. Use ``type`` instead.

Expand Down
17 changes: 16 additions & 1 deletion astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import types
import zipimport
from importlib.util import find_spec, module_from_spec
from typing import TYPE_CHECKING, ClassVar, List, Optional

from astroid.exceptions import AstroidBuildingError, AstroidImportError
Expand Down Expand Up @@ -361,6 +362,20 @@ def bootstrap(self):
raw_building._astroid_bootstrapping()

def clear_cache(self):
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
"""Clear the underlying cache. Also bootstraps the builtins module."""
"""Clear the underlying cache. Also bootstraps the builtins module and
re-registers transforms."""
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
# import here because of cyclic imports
# pylint: disable=import-outside-toplevel

from astroid import BRAIN_MODULES_DIRECTORY
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved

self.astroid_cache.clear()
AstroidManager.brain["_transform"] = TransformVisitor()
self.bootstrap()

# Load brain plugins: currently done in astroid.__init__.py
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
for module in BRAIN_MODULES_DIRECTORY.iterdir():
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
if module.suffix == ".py":
module_spec = find_spec(f"astroid.brain.{module.stem}")
module_object = module_from_spec(module_spec)
module_spec.loader.exec_module(module_object)
11 changes: 7 additions & 4 deletions tests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def tearDown(self) -> None:


class AstroidCacheSetupMixin:
"""Mixin for handling the astroid cache problems.
"""Mixin for handling test isolation issues with the astroid cache.

When clearing the astroid cache, some tests fails due to
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 makes sure to add it
back to the astroid_cache after the tests finishes.
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
Expand All @@ -52,8 +52,11 @@ class AstroidCacheSetupMixin:
@classmethod
def setup_class(cls):
cls._builtins = AstroidManager().astroid_cache.get("builtins")
cls._transforms = AstroidManager.brain["_transform"]
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def teardown_class(cls):
if cls._builtins:
AstroidManager().astroid_cache["builtins"] = cls._builtins
if cls._transforms:
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
AstroidManager.brain["_transform"] = cls._transforms
9 changes: 9 additions & 0 deletions tests/unittest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from astroid import manager, test_utils
from astroid.const import IS_JYTHON
from astroid.exceptions import AstroidBuildingError, AstroidImportError
from astroid.nodes import Const

from . import resources

Expand Down Expand Up @@ -315,5 +316,13 @@ def test_borg(self) -> None:
self.assertIs(built, second_built)


class ClearCacheTest(unittest.TestCase, resources.AstroidCacheSetupMixin):
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
def test_brain_plugins_reloaded_after_clearing_cache(self) -> None:
astroid.MANAGER.clear_cache()
format_call = astroid.extract_node("''.format()")
inferred = next(format_call.infer())
self.assertIsInstance(inferred, Const)


if __name__ == "__main__":
unittest.main()