@@ -382,6 +382,14 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
382382 if not isinstance (node .parent .parent , nodes .Call ):
383383 return
384384
385+ # Don't raise message on bad format string
386+ try :
387+ keyword_args = [
388+ i [0 ] for i in utils .parse_format_method_string (node .value )[0 ]
389+ ]
390+ except utils .IncompleteFormatString :
391+ return
392+
385393 if node .parent .parent .args :
386394 for arg in node .parent .parent .args :
387395 # If star expressions with more than 1 element are being used
@@ -397,9 +405,6 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
397405 return
398406
399407 elif node .parent .parent .keywords :
400- keyword_args = [
401- i [0 ] for i in utils .parse_format_method_string (node .value )[0 ]
402- ]
403408 for keyword in node .parent .parent .keywords :
404409 # If keyword is used multiple times
405410 if keyword_args .count (keyword .arg ) > 1 :
@@ -408,9 +413,12 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
408413 keyword = utils .safe_infer (keyword .value )
409414
410415 # If lists of more than one element are being unpacked
411- if isinstance (keyword , nodes .Dict ):
412- if len (keyword .items ) > 1 and len (keyword_args ) > 1 :
413- return
416+ if (
417+ isinstance (keyword , nodes .Dict )
418+ and len (keyword .items ) > 1
419+ and len (keyword_args ) > 1
420+ ):
421+ return
414422
415423 # If all tests pass, then raise message
416424 self .add_message (
@@ -438,12 +446,10 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
438446 inferred_right = utils .safe_infer (node .parent .right )
439447
440448 # If dicts or lists of length > 1 are used
441- if isinstance (inferred_right , nodes .Dict ):
442- if len (inferred_right .items ) > 1 :
443- return
444- elif isinstance (inferred_right , nodes .List ):
445- if len (inferred_right .elts ) > 1 :
446- return
449+ if isinstance (inferred_right , nodes .Dict ) and len (inferred_right .items ) > 1 :
450+ return
451+ if isinstance (inferred_right , nodes .List ) and len (inferred_right .elts ) > 1 :
452+ return
447453
448454 # If all tests pass, then raise message
449455 self .add_message (
0 commit comments