From a2921b3224f068c34f1dbb564aab6ce794b7eab3 Mon Sep 17 00:00:00 2001 From: Jamie Scott Date: Mon, 25 Mar 2024 13:24:13 +0000 Subject: [PATCH] Add deny list for modules to be skipped on AST generation (#2399) Work towards pylint-dev/pylint#9442 --- ChangeLog | 6 ++++++ astroid/manager.py | 4 ++++ astroid/test_utils.py | 1 + tests/test_manager.py | 7 +++++++ 4 files changed, 18 insertions(+) diff --git a/ChangeLog b/ChangeLog index ac5df56f4f..c6119847bc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,12 @@ Release date: TBA Closes #1015 Refs pylint-dev/pylint#4696 +* Adds ``module_denylist`` to ``AstroidManager`` for modules to be skipped during AST + generation. Modules in this list will cause an ``AstroidImportError`` to be raised + when an AST for them is requested. + + Refs pylint-dev/pylint#9442 + What's New in astroid 3.1.1? ============================ diff --git a/astroid/manager.py b/astroid/manager.py index c499fe5598..4ef3e8288f 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -59,6 +59,7 @@ class AstroidManager: "optimize_ast": False, "max_inferable_values": 100, "extension_package_whitelist": set(), + "module_denylist": set(), "_transform": TransformVisitor(), } @@ -70,6 +71,7 @@ def __init__(self) -> None: self.extension_package_whitelist = AstroidManager.brain[ "extension_package_whitelist" ] + self.module_denylist = AstroidManager.brain["module_denylist"] self._transform = AstroidManager.brain["_transform"] @property @@ -200,6 +202,8 @@ def ast_from_module_name( # noqa: C901 # importing a module with the same name as the file that is importing # we want to fallback on the import system to make sure we get the correct # module. + if modname in self.module_denylist: + raise AstroidImportError(f"Skipping ignored module {modname!r}") if modname in self.astroid_cache and use_cache: return self.astroid_cache[modname] if modname == "__main__": diff --git a/astroid/test_utils.py b/astroid/test_utils.py index 1119cd093f..afddb1a4ff 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -74,4 +74,5 @@ def brainless_manager(): m._mod_file_cache = {} m._transform = transforms.TransformVisitor() m.extension_package_whitelist = set() + m.module_denylist = set() return m diff --git a/tests/test_manager.py b/tests/test_manager.py index a55fae1932..7861927930 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -383,6 +383,13 @@ def test_raises_exception_for_empty_modname(self) -> None: with pytest.raises(AstroidBuildingError): self.manager.ast_from_module_name(None) + def test_denied_modules_raise(self) -> None: + self.manager.module_denylist.add("random") + with pytest.raises(AstroidImportError, match="random"): + self.manager.ast_from_module_name("random") + # and module not in the deny list shouldn't raise + self.manager.ast_from_module_name("math") + class IsolatedAstroidManagerTest(resources.AstroidCacheSetupMixin, unittest.TestCase): def test_no_user_warning(self):