Skip to content

Commit

Permalink
Allow clear_cache() to load brain plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Apr 23, 2022
1 parent 615587b commit 8a9f764
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
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
13 changes: 13 additions & 0 deletions 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 @@ -362,5 +363,17 @@ def bootstrap(self):

def clear_cache(self):
"""Clear the underlying cache. Also bootstraps the builtins module."""
# import here because of cyclic imports
# pylint: disable=import-outside-toplevel

from astroid import BRAIN_MODULES_DIRECTORY

self.astroid_cache.clear()
self.bootstrap()

# Load brain plugins: currently done in astroid.__init__.py
for module in BRAIN_MODULES_DIRECTORY.iterdir():
if module.suffix == ".py":
spec = find_spec(f"astroid.brain.{module.stem}")
module_object = module_from_spec(spec)
spec.loader.exec_module(module_object)
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):
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()

0 comments on commit 8a9f764

Please sign in to comment.