@@ -382,6 +382,14 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
382
382
if not isinstance (node .parent .parent , nodes .Call ):
383
383
return
384
384
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
+
385
393
if node .parent .parent .args :
386
394
for arg in node .parent .parent .args :
387
395
# 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:
397
405
return
398
406
399
407
elif node .parent .parent .keywords :
400
- keyword_args = [
401
- i [0 ] for i in utils .parse_format_method_string (node .value )[0 ]
402
- ]
403
408
for keyword in node .parent .parent .keywords :
404
409
# If keyword is used multiple times
405
410
if keyword_args .count (keyword .arg ) > 1 :
@@ -408,9 +413,12 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
408
413
keyword = utils .safe_infer (keyword .value )
409
414
410
415
# 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
414
422
415
423
# If all tests pass, then raise message
416
424
self .add_message (
@@ -438,12 +446,10 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
438
446
inferred_right = utils .safe_infer (node .parent .right )
439
447
440
448
# 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
447
453
448
454
# If all tests pass, then raise message
449
455
self .add_message (
0 commit comments