@@ -32,15 +32,15 @@ def _is_posix():
3232 """Return True if the current system is POSIX-compatible."""
3333 # NB: we could simply use `os.name` instead of `platform.system()`. However, that solution would be difficult to
3434 # 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' }
3636
3737
3838_is_unix = _is_posix
3939
4040# ==============================================================================
4141
4242
43- def _read_octal_mode_option (name , value , default ):
43+ def _read_octal_mode_option (name , value , default ): # noqa: C901
4444 """
4545 Read an integer or list of integer configuration option.
4646
@@ -78,23 +78,27 @@ def _str_to_int(arg):
7878 try :
7979 allowed_modes = [_str_to_int (mode ) for mode in modes if mode ]
8080 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
8283 else :
8384 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 )
8587 return allowed_modes
8688 elif modes and modes [0 ]:
8789 # Single values (ie. max allowed value for mode)
8890 try :
8991 return _str_to_int (value )
9092 except ValueError as error :
91- if value in ( 'y' , 'yes' , 'true' ) :
93+ if value in { 'y' , 'yes' , 'true' } :
9294 return default
93- if value in ( 'n' , 'no' , 'false' ) :
95+ if value in { 'n' , 'no' , 'false' } :
9496 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
9699 else :
97- raise ValueError (f'Invalid value for `{ name } `: { value } !' )
100+ msg = f'Invalid value for `{ name } `: { value } !'
101+ raise ValueError (msg )
98102
99103
100104# ==============================================================================
@@ -126,7 +130,7 @@ def _is_os_path_call(node):
126130 and node .func .expr .expr .name == 'os'
127131 )
128132 )
129- and node .func .attrname in ( 'abspath' , 'relpath' )
133+ and node .func .attrname in { 'abspath' , 'relpath' }
130134 )
131135
132136
@@ -183,8 +187,8 @@ def _is_shell_true_call(node):
183187 return False
184188
185189 # 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' } :
188192 if node .keywords :
189193 for keyword in node .keywords :
190194 if (
@@ -201,7 +205,7 @@ def _is_shell_true_call(node):
201205 ):
202206 return True
203207
204- if node .func .attrname in ( 'getoutput' , 'getstatusoutput' ) :
208+ if node .func .attrname in { 'getoutput' , 'getstatusoutput' } :
205209 return True
206210
207211 # asyncio module
@@ -249,7 +253,7 @@ def _is_yaml_unsafe_call(node):
249253 and isinstance (node .func .expr , astroid .Name )
250254 and node .func .expr .name == 'yaml'
251255 ):
252- if node .func .attrname in ( 'unsafe_load' , 'full_load' ) :
256+ if node .func .attrname in { 'unsafe_load' , 'full_load' } :
253257 # Cover:
254258 # * yaml.full_load().
255259 # * yaml.unsafe_load().
@@ -279,7 +283,7 @@ def _is_yaml_unsafe_call(node):
279283 # * yaml.load(x, FullLoader).
280284 return True
281285
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' } :
283287 # Cover:
284288 # * unsafe_load(...).
285289 # * full_load(...).
@@ -348,7 +352,8 @@ def _chmod_get_mode(node):
348352 if isinstance (node , astroid .BinOp ):
349353 return _binop [node .op ](_chmod_get_mode (node .left ), _chmod_get_mode (node .right ))
350354
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 )
352357
353358
354359def _chmod_has_wx_for_go (node ):
@@ -370,7 +375,8 @@ def _chmod_has_wx_for_go(node):
370375 else :
371376 if modes is None :
372377 # 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 )
374380 # pylint: disable=no-member
375381 return bool (modes & (stat .S_IWGRP | stat .S_IXGRP | stat .S_IWOTH | stat .S_IXOTH ))
376382
@@ -440,11 +446,9 @@ class SecureCodingStandardChecker(BaseChecker): # pylint: disable=too-many-inst
440446 'E8003' : (
441447 'Avoid using `shell=True` when calling `subprocess` functions and avoid functions that internally call it' ,
442448 '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'
448452 ),
449453 ),
450454 'R8004' : (
@@ -542,7 +546,7 @@ def __init__(self, linter):
542546 self ._os_mknod_msg_arg = ''
543547 self ._os_mknod_modes_allowed = []
544548
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
546550 """Visitor method called for astroid.Call nodes."""
547551 if _is_pdb_call (node ):
548552 self .add_message ('avoid-debug-stmt' , node = node )
@@ -562,7 +566,7 @@ def visit_call(self, node): # pylint: disable=too-many-branches # noqa: PLR0912
562566 self .add_message ('avoid-os-popen' , node = node )
563567 elif _is_builtin_open_for_writing (node ) and self ._os_open_modes_allowed :
564568 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' } ):
566570 self .add_message ('avoid-eval-exec' , node = node )
567571 elif not _is_posix () and _is_function_call (node , module = 'shlex' , function = 'quote' ):
568572 self .add_message ('avoid-shlex-quote-on-non-posix' , node = node )
@@ -609,17 +613,17 @@ def visit_import(self, node):
609613 # * import pdb as xxx.
610614 self .add_message ('avoid-debug-stmt' , node = node )
611615
612- def visit_importfrom (self , node ):
616+ def visit_importfrom (self , node ): # noqa: C901
613617 """Visitor method called for astroid.ImportFrom nodes."""
614618 if node .modname == 'pdb' :
615619 self .add_message ('avoid-debug-stmt' , node = node )
616620 elif node .modname == 'tempfile' and [name for (name , _ ) in node .names if name == 'mktemp' ]:
617621 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' } ]:
619623 self .add_message ('replace-os-relpath-abspath' , node = node )
620624 elif (
621625 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' } ]
623627 ) or (node .modname == 'asyncio' and [name for (name , _ ) in node .names if name == 'create_subprocess_shell' ]):
624628 self .add_message ('avoid-shell-true' , node = node )
625629 elif node .modname == 'os' and [name for (name , _ ) in node .names if name == 'system' ]:
@@ -628,9 +632,9 @@ def visit_importfrom(self, node):
628632 self .add_message ('avoid-os-popen' , node = node )
629633 elif not _is_posix () and node .modname == 'shlex' and [name for (name , _ ) in node .names if name == 'quote' ]:
630634 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' } ]:
632636 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' } ]:
634638 self .add_message ('avoid-marshal-load' , node = node )
635639 elif node .modname == 'shelve' and [name for (name , _ ) in node .names if name == 'open' ]:
636640 self .add_message ('avoid-shelve-open' , node = node )
0 commit comments