@@ -337,6 +337,7 @@ def check_specs_in_format_call(
337
337
338
338
The core logic for format checking is implemented in this method.
339
339
"""
340
+
340
341
assert all (s .key for s in specs ), "Keys must be auto-generated first!"
341
342
replacements = self .find_replacements_in_call (call , [cast (str , s .key ) for s in specs ])
342
343
assert len (replacements ) == len (specs )
@@ -448,24 +449,27 @@ def perform_special_format_checks(
448
449
call ,
449
450
code = codes .STRING_FORMATTING ,
450
451
)
451
- a_type = get_proper_type ( actual_type )
452
- if isinstance (a_type , NoneType ):
452
+
453
+ if isinstance (get_proper_type ( actual_type ) , NoneType ):
453
454
# Perform type check of alignment specifiers on None
454
- if spec .format_spec and any (c in spec .format_spec for c in "<>^" ):
455
- specifierIndex = - 1
456
- for i in range (len ("<>^" )):
457
- if spec .format_spec [i ] in "<>^" :
458
- specifierIndex = i
459
- if specifierIndex > - 1 :
460
- self .msg .fail (
461
- (
462
- f"Alignment format specifier "
463
- f'"{ spec .format_spec [specifierIndex ]} " '
464
- f"is not supported for None"
465
- ),
466
- call ,
467
- code = codes .STRING_FORMATTING ,
468
- )
455
+ # If spec.format_spec is None then we use "" instead of avoid crashing
456
+ specifier_char = None
457
+ if spec .non_standard_format_spec and isinstance (call .args [- 1 ], StrExpr ):
458
+ arg = call .args [- 1 ].value
459
+ specifier_char = next ((c for c in (arg or "" ) if c in "<>^" ), None )
460
+ elif isinstance (spec .format_spec , str ):
461
+ specifier_char = next ((c for c in (spec .format_spec or "" ) if c in "<>^" ), None )
462
+
463
+ if specifier_char :
464
+ self .msg .fail (
465
+ (
466
+ f"Alignment format specifier "
467
+ f'"{ specifier_char } " '
468
+ f"is not supported for None"
469
+ ),
470
+ call ,
471
+ code = codes .STRING_FORMATTING ,
472
+ )
469
473
470
474
def find_replacements_in_call (self , call : CallExpr , keys : list [str ]) -> list [Expression ]:
471
475
"""Find replacement expression for every specifier in str.format() call.
0 commit comments