@@ -32,15 +32,15 @@ def _is_posix():
32
32
"""Return True if the current system is POSIX-compatible."""
33
33
# NB: we could simply use `os.name` instead of `platform.system()`. However, that solution would be difficult to
34
34
# test using `mock` as a few modules (like `pytest`) actually use it internally...
35
- return platform .system () in ( 'Linux' , 'Darwin' )
35
+ return platform .system () in { 'Linux' , 'Darwin' }
36
36
37
37
38
38
_is_unix = _is_posix
39
39
40
40
# ==============================================================================
41
41
42
42
43
- def _read_octal_mode_option (name , value , default ):
43
+ def _read_octal_mode_option (name , value , default ): # noqa: C901
44
44
"""
45
45
Read an integer or list of integer configuration option.
46
46
@@ -78,23 +78,27 @@ def _str_to_int(arg):
78
78
try :
79
79
allowed_modes = [_str_to_int (mode ) for mode in modes if mode ]
80
80
except ValueError as error :
81
- raise ValueError (f'Unable to convert { modes } elements to integers!' ) from error
81
+ msg = f'Unable to convert { modes } elements to integers!'
82
+ raise ValueError (msg ) from error
82
83
else :
83
84
if not allowed_modes :
84
- raise ValueError (f'Calculated empty value for `{ name } `!' )
85
+ msg = f'Calculated empty value for `{ name } `!'
86
+ raise ValueError (msg )
85
87
return allowed_modes
86
88
elif modes and modes [0 ]:
87
89
# Single values (ie. max allowed value for mode)
88
90
try :
89
91
return _str_to_int (value )
90
92
except ValueError as error :
91
- if value in ( 'y' , 'yes' , 'true' ) :
93
+ if value in { 'y' , 'yes' , 'true' } :
92
94
return default
93
- if value in ( 'n' , 'no' , 'false' ) :
95
+ if value in { 'n' , 'no' , 'false' } :
94
96
return None
95
- raise ValueError (f'Invalid value for `{ name } `: { value } !' ) from error
97
+ msg = f'Invalid value for `{ name } `: { value } !'
98
+ raise ValueError (msg ) from error
96
99
else :
97
- raise ValueError (f'Invalid value for `{ name } `: { value } !' )
100
+ msg = f'Invalid value for `{ name } `: { value } !'
101
+ raise ValueError (msg )
98
102
99
103
100
104
# ==============================================================================
@@ -126,7 +130,7 @@ def _is_os_path_call(node):
126
130
and node .func .expr .expr .name == 'os'
127
131
)
128
132
)
129
- and node .func .attrname in ( 'abspath' , 'relpath' )
133
+ and node .func .attrname in { 'abspath' , 'relpath' }
130
134
)
131
135
132
136
@@ -183,8 +187,8 @@ def _is_shell_true_call(node):
183
187
return False
184
188
185
189
# subprocess module
186
- if node .func .expr .name in ( 'subprocess' , 'sp' ) :
187
- if node .func .attrname in ( 'call' , 'check_call' , 'check_output' , 'Popen' , 'run' ) :
190
+ if node .func .expr .name in { 'subprocess' , 'sp' } :
191
+ if node .func .attrname in { 'call' , 'check_call' , 'check_output' , 'Popen' , 'run' } :
188
192
if node .keywords :
189
193
for keyword in node .keywords :
190
194
if (
@@ -201,7 +205,7 @@ def _is_shell_true_call(node):
201
205
):
202
206
return True
203
207
204
- if node .func .attrname in ( 'getoutput' , 'getstatusoutput' ) :
208
+ if node .func .attrname in { 'getoutput' , 'getstatusoutput' } :
205
209
return True
206
210
207
211
# asyncio module
@@ -249,7 +253,7 @@ def _is_yaml_unsafe_call(node):
249
253
and isinstance (node .func .expr , astroid .Name )
250
254
and node .func .expr .name == 'yaml'
251
255
):
252
- if node .func .attrname in ( 'unsafe_load' , 'full_load' ) :
256
+ if node .func .attrname in { 'unsafe_load' , 'full_load' } :
253
257
# Cover:
254
258
# * yaml.full_load().
255
259
# * yaml.unsafe_load().
@@ -279,7 +283,7 @@ def _is_yaml_unsafe_call(node):
279
283
# * yaml.load(x, FullLoader).
280
284
return True
281
285
282
- if isinstance (node .func , astroid .Name ) and node .func .name in ( 'unsafe_load' , 'full_load' ) :
286
+ if isinstance (node .func , astroid .Name ) and node .func .name in { 'unsafe_load' , 'full_load' } :
283
287
# Cover:
284
288
# * unsafe_load(...).
285
289
# * full_load(...).
@@ -348,7 +352,8 @@ def _chmod_get_mode(node):
348
352
if isinstance (node , astroid .BinOp ):
349
353
return _binop [node .op ](_chmod_get_mode (node .left ), _chmod_get_mode (node .right ))
350
354
351
- raise ValueError (f'Do not know how to process node: { node .repr_tree ()} ' )
355
+ msg = f'Do not know how to process node: { node .repr_tree ()} '
356
+ raise ValueError (msg )
352
357
353
358
354
359
def _chmod_has_wx_for_go (node ):
@@ -370,7 +375,8 @@ def _chmod_has_wx_for_go(node):
370
375
else :
371
376
if modes is None :
372
377
# NB: this would be from invalid code such as `os.chmod("file.txt")`
373
- raise RuntimeError ('Unable to extract `mode` argument from function call!' )
378
+ msg = 'Unable to extract `mode` argument from function call!'
379
+ raise RuntimeError (msg )
374
380
# pylint: disable=no-member
375
381
return bool (modes & (stat .S_IWGRP | stat .S_IXGRP | stat .S_IWOTH | stat .S_IXOTH ))
376
382
@@ -440,11 +446,9 @@ class SecureCodingStandardChecker(BaseChecker): # pylint: disable=too-many-inst
440
446
'E8003' : (
441
447
'Avoid using `shell=True` when calling `subprocess` functions and avoid functions that internally call it' ,
442
448
'avoid-shell-true' ,
443
- ' ' .join (
444
- [
445
- 'Use of `shell=True` in subprocess functions or use of functions that internally set it should be'
446
- 'should be avoided' ,
447
- ]
449
+ (
450
+ 'Use of `shell=True` in subprocess functions or use of functions that internally set it should be '
451
+ 'should be avoided'
448
452
),
449
453
),
450
454
'R8004' : (
@@ -542,7 +546,7 @@ def __init__(self, linter):
542
546
self ._os_mknod_msg_arg = ''
543
547
self ._os_mknod_modes_allowed = []
544
548
545
- def visit_call (self , node ): # pylint: disable=too-many-branches # noqa: PLR0912
549
+ def visit_call (self , node ): # pylint: disable=too-many-branches # noqa: PLR0912, C901
546
550
"""Visitor method called for astroid.Call nodes."""
547
551
if _is_pdb_call (node ):
548
552
self .add_message ('avoid-debug-stmt' , node = node )
@@ -562,7 +566,7 @@ def visit_call(self, node): # pylint: disable=too-many-branches # noqa: PLR0912
562
566
self .add_message ('avoid-os-popen' , node = node )
563
567
elif _is_builtin_open_for_writing (node ) and self ._os_open_modes_allowed :
564
568
self .add_message ('replace-builtin-open' , node = node )
565
- elif isinstance (node .func , astroid .Name ) and (node .func .name in ( 'eval' , 'exec' ) ):
569
+ elif isinstance (node .func , astroid .Name ) and (node .func .name in { 'eval' , 'exec' } ):
566
570
self .add_message ('avoid-eval-exec' , node = node )
567
571
elif not _is_posix () and _is_function_call (node , module = 'shlex' , function = 'quote' ):
568
572
self .add_message ('avoid-shlex-quote-on-non-posix' , node = node )
@@ -609,17 +613,17 @@ def visit_import(self, node):
609
613
# * import pdb as xxx.
610
614
self .add_message ('avoid-debug-stmt' , node = node )
611
615
612
- def visit_importfrom (self , node ):
616
+ def visit_importfrom (self , node ): # noqa: C901
613
617
"""Visitor method called for astroid.ImportFrom nodes."""
614
618
if node .modname == 'pdb' :
615
619
self .add_message ('avoid-debug-stmt' , node = node )
616
620
elif node .modname == 'tempfile' and [name for (name , _ ) in node .names if name == 'mktemp' ]:
617
621
self .add_message ('replace-mktemp' , node = node )
618
- elif node .modname in ( 'os.path' , 'op' ) and [name for (name , _ ) in node .names if name in ( 'relpath' , 'abspath' ) ]:
622
+ elif node .modname in { 'os.path' , 'op' } and [name for (name , _ ) in node .names if name in { 'relpath' , 'abspath' } ]:
619
623
self .add_message ('replace-os-relpath-abspath' , node = node )
620
624
elif (
621
625
node .modname == 'subprocess'
622
- and [name for (name , _ ) in node .names if name in ( 'getoutput' , 'getstatusoutput' ) ]
626
+ and [name for (name , _ ) in node .names if name in { 'getoutput' , 'getstatusoutput' } ]
623
627
) or (node .modname == 'asyncio' and [name for (name , _ ) in node .names if name == 'create_subprocess_shell' ]):
624
628
self .add_message ('avoid-shell-true' , node = node )
625
629
elif node .modname == 'os' and [name for (name , _ ) in node .names if name == 'system' ]:
@@ -628,9 +632,9 @@ def visit_importfrom(self, node):
628
632
self .add_message ('avoid-os-popen' , node = node )
629
633
elif not _is_posix () and node .modname == 'shlex' and [name for (name , _ ) in node .names if name == 'quote' ]:
630
634
self .add_message ('avoid-shlex-quote-on-non-posix' , node = node )
631
- elif node .modname == 'pickle' and [name for (name , _ ) in node .names if name in ( 'load' , 'loads' ) ]:
635
+ elif node .modname == 'pickle' and [name for (name , _ ) in node .names if name in { 'load' , 'loads' } ]:
632
636
self .add_message ('avoid-pickle-load' , node = node )
633
- elif node .modname == 'marshal' and [name for (name , _ ) in node .names if name in ( 'load' , 'loads' ) ]:
637
+ elif node .modname == 'marshal' and [name for (name , _ ) in node .names if name in { 'load' , 'loads' } ]:
634
638
self .add_message ('avoid-marshal-load' , node = node )
635
639
elif node .modname == 'shelve' and [name for (name , _ ) in node .names if name == 'open' ]:
636
640
self .add_message ('avoid-shelve-open' , node = node )
0 commit comments