File tree Expand file tree Collapse file tree 4 files changed +18
-0
lines changed Expand file tree Collapse file tree 4 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,12 @@ Release date: TBA
13
13
Closes #1015
14
14
Refs pylint-dev/pylint#4696
15
15
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
+
16
22
17
23
What's New in astroid 3.1.1?
18
24
============================
Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ class AstroidManager:
59
59
"optimize_ast" : False ,
60
60
"max_inferable_values" : 100 ,
61
61
"extension_package_whitelist" : set (),
62
+ "module_denylist" : set (),
62
63
"_transform" : TransformVisitor (),
63
64
}
64
65
@@ -70,6 +71,7 @@ def __init__(self) -> None:
70
71
self .extension_package_whitelist = AstroidManager .brain [
71
72
"extension_package_whitelist"
72
73
]
74
+ self .module_denylist = AstroidManager .brain ["module_denylist" ]
73
75
self ._transform = AstroidManager .brain ["_transform" ]
74
76
75
77
@property
@@ -200,6 +202,8 @@ def ast_from_module_name( # noqa: C901
200
202
# importing a module with the same name as the file that is importing
201
203
# we want to fallback on the import system to make sure we get the correct
202
204
# module.
205
+ if modname in self .module_denylist :
206
+ raise AstroidImportError (f"Skipping ignored module { modname !r} " )
203
207
if modname in self .astroid_cache and use_cache :
204
208
return self .astroid_cache [modname ]
205
209
if modname == "__main__" :
Original file line number Diff line number Diff line change @@ -74,4 +74,5 @@ def brainless_manager():
74
74
m ._mod_file_cache = {}
75
75
m ._transform = transforms .TransformVisitor ()
76
76
m .extension_package_whitelist = set ()
77
+ m .module_denylist = set ()
77
78
return m
Original file line number Diff line number Diff line change @@ -383,6 +383,13 @@ def test_raises_exception_for_empty_modname(self) -> None:
383
383
with pytest .raises (AstroidBuildingError ):
384
384
self .manager .ast_from_module_name (None )
385
385
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
+
386
393
387
394
class IsolatedAstroidManagerTest (resources .AstroidCacheSetupMixin , unittest .TestCase ):
388
395
def test_no_user_warning (self ):
You can’t perform that action at this time.
0 commit comments