Skip to content

Commit

Permalink
Add deny list for modules to be skipped on AST generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie- committed Mar 15, 2024
1 parent 3fb17a1 commit d47f144
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions astroid/manager.py
Expand Up @@ -59,6 +59,7 @@ class AstroidManager:
"optimize_ast": False,
"max_inferable_values": 100,
"extension_package_whitelist": set(),
"module_denylist": set(),
"_transform": TransformVisitor(),
}

Expand All @@ -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
Expand Down Expand Up @@ -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("Skipping ignored module")
if modname in self.astroid_cache and use_cache:
return self.astroid_cache[modname]
if modname == "__main__":
Expand Down
1 change: 1 addition & 0 deletions astroid/test_utils.py
Expand Up @@ -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
7 changes: 7 additions & 0 deletions tests/test_manager.py
Expand Up @@ -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):
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):
Expand Down

0 comments on commit d47f144

Please sign in to comment.