Skip to content

Commit a2921b3

Browse files
authored
Add deny list for modules to be skipped on AST generation (#2399)
Work towards pylint-dev/pylint#9442
1 parent 4a094d7 commit a2921b3

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ Release date: TBA
1313
Closes #1015
1414
Refs pylint-dev/pylint#4696
1515

16+
* Adds ``module_denylist`` to ``AstroidManager`` for modules to be skipped during AST
17+
generation. Modules in this list will cause an ``AstroidImportError`` to be raised
18+
when an AST for them is requested.
19+
20+
Refs pylint-dev/pylint#9442
21+
1622

1723
What's New in astroid 3.1.1?
1824
============================

astroid/manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class AstroidManager:
5959
"optimize_ast": False,
6060
"max_inferable_values": 100,
6161
"extension_package_whitelist": set(),
62+
"module_denylist": set(),
6263
"_transform": TransformVisitor(),
6364
}
6465

@@ -70,6 +71,7 @@ def __init__(self) -> None:
7071
self.extension_package_whitelist = AstroidManager.brain[
7172
"extension_package_whitelist"
7273
]
74+
self.module_denylist = AstroidManager.brain["module_denylist"]
7375
self._transform = AstroidManager.brain["_transform"]
7476

7577
@property
@@ -200,6 +202,8 @@ def ast_from_module_name( # noqa: C901
200202
# importing a module with the same name as the file that is importing
201203
# we want to fallback on the import system to make sure we get the correct
202204
# module.
205+
if modname in self.module_denylist:
206+
raise AstroidImportError(f"Skipping ignored module {modname!r}")
203207
if modname in self.astroid_cache and use_cache:
204208
return self.astroid_cache[modname]
205209
if modname == "__main__":

astroid/test_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ def brainless_manager():
7474
m._mod_file_cache = {}
7575
m._transform = transforms.TransformVisitor()
7676
m.extension_package_whitelist = set()
77+
m.module_denylist = set()
7778
return m

tests/test_manager.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ def test_raises_exception_for_empty_modname(self) -> None:
383383
with pytest.raises(AstroidBuildingError):
384384
self.manager.ast_from_module_name(None)
385385

386+
def test_denied_modules_raise(self) -> None:
387+
self.manager.module_denylist.add("random")
388+
with pytest.raises(AstroidImportError, match="random"):
389+
self.manager.ast_from_module_name("random")
390+
# and module not in the deny list shouldn't raise
391+
self.manager.ast_from_module_name("math")
392+
386393

387394
class IsolatedAstroidManagerTest(resources.AstroidCacheSetupMixin, unittest.TestCase):
388395
def test_no_user_warning(self):

0 commit comments

Comments
 (0)