Skip to content

Commit 8a9f764

Browse files
Allow clear_cache() to load brain plugins
1 parent 615587b commit 8a9f764

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Release date: TBA
1313

1414
Closes #1512
1515

16+
* Allowed ``AstroidManager.clear_cache`` to reload necessary brain plugins.
17+
1618
* Rename ``ModuleSpec`` -> ``module_type`` constructor parameter to match attribute
1719
name and improve typing. Use ``type`` instead.
1820

astroid/manager.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import types
1212
import zipimport
13+
from importlib.util import find_spec, module_from_spec
1314
from typing import TYPE_CHECKING, ClassVar, List, Optional
1415

1516
from astroid.exceptions import AstroidBuildingError, AstroidImportError
@@ -362,5 +363,17 @@ def bootstrap(self):
362363

363364
def clear_cache(self):
364365
"""Clear the underlying cache. Also bootstraps the builtins module."""
366+
# import here because of cyclic imports
367+
# pylint: disable=import-outside-toplevel
368+
369+
from astroid import BRAIN_MODULES_DIRECTORY
370+
365371
self.astroid_cache.clear()
366372
self.bootstrap()
373+
374+
# Load brain plugins: currently done in astroid.__init__.py
375+
for module in BRAIN_MODULES_DIRECTORY.iterdir():
376+
if module.suffix == ".py":
377+
spec = find_spec(f"astroid.brain.{module.stem}")
378+
module_object = module_from_spec(spec)
379+
spec.loader.exec_module(module_object)

tests/unittest_manager.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from astroid import manager, test_utils
1717
from astroid.const import IS_JYTHON
1818
from astroid.exceptions import AstroidBuildingError, AstroidImportError
19+
from astroid.nodes import Const
1920

2021
from . import resources
2122

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

317318

319+
class ClearCacheTest(unittest.TestCase, resources.AstroidCacheSetupMixin):
320+
def test_brain_plugins_reloaded_after_clearing_cache(self) -> None:
321+
astroid.MANAGER.clear_cache()
322+
format_call = astroid.extract_node("''.format()")
323+
inferred = next(format_call.infer())
324+
self.assertIsInstance(inferred, Const)
325+
326+
318327
if __name__ == "__main__":
319328
unittest.main()

0 commit comments

Comments
 (0)