@@ -199,15 +199,11 @@ def parse_format_value(
199
199
custom_match , start_pos = start_pos , non_standard_format_spec = True
200
200
)
201
201
else :
202
- msg .fail (
203
- "Invalid conversion specifier in format string" ,
204
- ctx ,
205
- code = codes .STRING_FORMATTING ,
206
- )
202
+ msg .fail (message_registry .FORMAT_STR_INVALID_SPECIFIER , ctx )
207
203
return None
208
204
209
205
if conv_spec .key and ("{" in conv_spec .key or "}" in conv_spec .key ):
210
- msg .fail ("Conversion value must not contain { or }" , ctx , code = codes . STRING_FORMATTING )
206
+ msg .fail (message_registry . FORMAT_STR_BRACES_IN_SPECIFIER , ctx )
211
207
return None
212
208
result .append (conv_spec )
213
209
@@ -218,11 +214,7 @@ def parse_format_value(
218
214
and ("{" in conv_spec .format_spec or "}" in conv_spec .format_spec )
219
215
):
220
216
if nested :
221
- msg .fail (
222
- "Formatting nesting must be at most two levels deep" ,
223
- ctx ,
224
- code = codes .STRING_FORMATTING ,
225
- )
217
+ msg .fail (message_registry .FORMAT_STR_NESTING_ATMOST_TWO_LEVELS , ctx )
226
218
return None
227
219
sub_conv_specs = parse_format_value (conv_spec .format_spec , ctx , msg , nested = True )
228
220
if sub_conv_specs is None :
@@ -260,11 +252,7 @@ def find_non_escaped_targets(
260
252
if pos < len (format_value ) - 1 and format_value [pos + 1 ] == "}" :
261
253
pos += 1
262
254
else :
263
- msg .fail (
264
- "Invalid conversion specifier in format string: unexpected }" ,
265
- ctx ,
266
- code = codes .STRING_FORMATTING ,
267
- )
255
+ msg .fail (message_registry .FORMAT_STR_UNEXPECTED_RBRACE , ctx )
268
256
return None
269
257
else :
270
258
# Adjust nesting level, then either continue adding chars or move on.
@@ -279,11 +267,7 @@ def find_non_escaped_targets(
279
267
next_spec = ""
280
268
pos += 1
281
269
if nesting :
282
- msg .fail (
283
- "Invalid conversion specifier in format string: unmatched {" ,
284
- ctx ,
285
- code = codes .STRING_FORMATTING ,
286
- )
270
+ msg .fail (message_registry .FORMAT_STR_UNMATCHED_LBRACE , ctx )
287
271
return None
288
272
return result
289
273
@@ -369,9 +353,8 @@ def check_specs_in_format_call(
369
353
):
370
354
# TODO: add support for some custom specs like datetime?
371
355
self .msg .fail (
372
- "Unrecognized format" ' specification "{}"' .format (spec .format_spec [1 :]),
356
+ message_registry . UNRECOGNIZED_FORMAT_SPEC .format (spec .format_spec [1 :]),
373
357
call ,
374
- code = codes .STRING_FORMATTING ,
375
358
)
376
359
continue
377
360
# Adjust expected and actual types.
@@ -390,10 +373,10 @@ def check_specs_in_format_call(
390
373
# If the explicit conversion is given, then explicit conversion is called _first_.
391
374
if spec .conversion [1 ] not in "rsa" :
392
375
self .msg .fail (
393
- 'Invalid conversion type "{}",'
394
- ' must be one of "r", "s" or "a"' .format (spec .conversion [1 ]),
376
+ message_registry .FORMAT_STR_INVALID_CONVERSION_TYPE .format (
377
+ spec .conversion [1 ]
378
+ ),
395
379
call ,
396
- code = codes .STRING_FORMATTING ,
397
380
)
398
381
actual_type = self .named_type ("builtins.str" )
399
382
@@ -433,13 +416,7 @@ def perform_special_format_checks(
433
416
if has_type_component (actual_type , "builtins.bytes" ) and not custom_special_method (
434
417
actual_type , "__str__"
435
418
):
436
- self .msg .fail (
437
- 'If x = b\' abc\' then f"{x}" or "{}".format(x) produces "b\' abc\' ", '
438
- 'not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). '
439
- "Otherwise, decode the bytes" ,
440
- call ,
441
- code = codes .STR_BYTES_PY3 ,
442
- )
419
+ self .msg .fail (message_registry .FORMAT_STR_BYTES_USE_REPR , call )
443
420
if spec .flags :
444
421
numeric_types = UnionType (
445
422
[self .named_type ("builtins.int" ), self .named_type ("builtins.float" )]
@@ -451,11 +428,7 @@ def perform_special_format_checks(
451
428
and not is_subtype (actual_type , numeric_types )
452
429
and not custom_special_method (actual_type , "__format__" )
453
430
):
454
- self .msg .fail (
455
- "Numeric flags are only allowed for numeric types" ,
456
- call ,
457
- code = codes .STRING_FORMATTING ,
458
- )
431
+ self .msg .fail (message_registry .FORMAT_STR_INVALID_NUMERIC_FLAG , call )
459
432
460
433
def find_replacements_in_call (self , call : CallExpr , keys : list [str ]) -> list [Expression ]:
461
434
"""Find replacement expression for every specifier in str.format() call.
@@ -469,19 +442,14 @@ def find_replacements_in_call(self, call: CallExpr, keys: list[str]) -> list[Exp
469
442
expr = self .get_expr_by_position (int (key ), call )
470
443
if not expr :
471
444
self .msg .fail (
472
- "Cannot find replacement for positional"
473
- " format specifier {}" .format (key ),
474
- call ,
475
- code = codes .STRING_FORMATTING ,
445
+ message_registry .FORMAT_STR_REPLACEMENT_NOT_FOUND .format (key ), call
476
446
)
477
447
expr = TempNode (AnyType (TypeOfAny .from_error ))
478
448
else :
479
449
expr = self .get_expr_by_name (key , call )
480
450
if not expr :
481
451
self .msg .fail (
482
- "Cannot find replacement for named" ' format specifier "{}"' .format (key ),
483
- call ,
484
- code = codes .STRING_FORMATTING ,
452
+ message_registry .FORMAT_STR_NAMED_REPLACEMENT_NOT_FOUND .format (key ), call
485
453
)
486
454
expr = TempNode (AnyType (TypeOfAny .from_error ))
487
455
result .append (expr )
@@ -555,11 +523,7 @@ def auto_generate_keys(self, all_specs: list[ConversionSpecifier], ctx: Context)
555
523
some_defined = any (s .key and s .key .isdecimal () for s in all_specs )
556
524
all_defined = all (bool (s .key ) for s in all_specs )
557
525
if some_defined and not all_defined :
558
- self .msg .fail (
559
- "Cannot combine automatic field numbering and manual field specification" ,
560
- ctx ,
561
- code = codes .STRING_FORMATTING ,
562
- )
526
+ self .msg .fail (message_registry .FORMAT_STR_PARTIAL_FIELD_NUMBERING , ctx )
563
527
return False
564
528
if all_defined :
565
529
return True
@@ -594,11 +558,7 @@ def apply_field_accessors(
594
558
dummy , fnam = "<format>" , module = None , options = self .chk .options , errors = temp_errors
595
559
)
596
560
if temp_errors .is_errors ():
597
- self .msg .fail (
598
- f'Syntax error in format specifier "{ spec .field } "' ,
599
- ctx ,
600
- code = codes .STRING_FORMATTING ,
601
- )
561
+ self .msg .fail (message_registry .FORMAT_STR_SYNTAX_ERROR .format (spec .field ), ctx )
602
562
return TempNode (AnyType (TypeOfAny .from_error ))
603
563
604
564
# These asserts are guaranteed by the original regexp.
@@ -637,10 +597,7 @@ class User(TypedDict):
637
597
"""
638
598
if not isinstance (temp_ast , (MemberExpr , IndexExpr )):
639
599
self .msg .fail (
640
- "Only index and member expressions are allowed in"
641
- ' format field accessors; got "{}"' .format (spec .field ),
642
- ctx ,
643
- code = codes .STRING_FORMATTING ,
600
+ message_registry .FORMAT_STR_INVALID_ACCESSOR_EXPR .format (spec .field ), ctx
644
601
)
645
602
return False
646
603
if isinstance (temp_ast , MemberExpr ):
@@ -651,10 +608,10 @@ class User(TypedDict):
651
608
assert spec .key , "Call this method only after auto-generating keys!"
652
609
assert spec .field
653
610
self .msg .fail (
654
- "Invalid index expression in format field"
655
- ' accessor "{}"' .format (spec .field [len (spec .key ) :]),
611
+ message_registry .FORMAT_STR_INVALID_INDEX_ACCESSOR .format (
612
+ spec .field [len (spec .key ) :]
613
+ ),
656
614
ctx ,
657
- code = codes .STRING_FORMATTING ,
658
615
)
659
616
return False
660
617
if isinstance (temp_ast .index , NameExpr ):
@@ -683,11 +640,7 @@ def check_str_interpolation(self, expr: FormatStringExpr, replacements: Expressi
683
640
specifiers = parse_conversion_specifiers (expr .value )
684
641
has_mapping_keys = self .analyze_conversion_specifiers (specifiers , expr )
685
642
if isinstance (expr , BytesExpr ) and self .chk .options .python_version < (3 , 5 ):
686
- self .msg .fail (
687
- "Bytes formatting is only supported in Python 3.5 and later" ,
688
- replacements ,
689
- code = codes .STRING_FORMATTING ,
690
- )
643
+ self .msg .fail (message_registry .FORMAT_STR_BYTES_ABOVE_PY35 , replacements )
691
644
return AnyType (TypeOfAny .from_error )
692
645
693
646
if has_mapping_keys is None :
@@ -794,9 +747,7 @@ def check_mapping_str_interpolation(
794
747
# Special case: for bytes formatting keys must be bytes.
795
748
if not isinstance (k , BytesExpr ):
796
749
self .msg .fail (
797
- "Dictionary keys in bytes formatting must be bytes, not strings" ,
798
- expr ,
799
- code = codes .STRING_FORMATTING ,
750
+ message_registry .FORMAT_STR_BYTES_DICT_KEYS_MUST_BE_BYTES , expr
800
751
)
801
752
key_str = cast (FormatStringExpr , k ).value
802
753
mapping [key_str ] = self .accept (v )
@@ -948,21 +899,12 @@ def check_s_special_cases(self, expr: FormatStringExpr, typ: Type, context: Cont
948
899
if isinstance (expr , StrExpr ):
949
900
# Couple special cases for string formatting.
950
901
if has_type_component (typ , "builtins.bytes" ):
951
- self .msg .fail (
952
- 'If x = b\' abc\' then "%s" % x produces "b\' abc\' ", not "abc". '
953
- 'If this is desired behavior use "%r" % x. Otherwise, decode the bytes' ,
954
- context ,
955
- code = codes .STR_BYTES_PY3 ,
956
- )
902
+ self .msg .fail (message_registry .FORMAT_STR_BYTES_USE_REPR_OLD , context )
957
903
return False
958
904
if isinstance (expr , BytesExpr ):
959
905
# A special case for bytes formatting: b'%s' actually requires bytes on Python 3.
960
906
if has_type_component (typ , "builtins.str" ):
961
- self .msg .fail (
962
- "On Python 3 b'%s' requires bytes, not string" ,
963
- context ,
964
- code = codes .STRING_FORMATTING ,
965
- )
907
+ self .msg .fail (message_registry .FORMAT_STR_BYTES_REQUIRED_PY3 , context )
966
908
return False
967
909
return True
968
910
@@ -1024,18 +966,10 @@ def conversion_type(
1024
966
INT_TYPES = REQUIRE_INT_NEW if format_call else REQUIRE_INT_OLD
1025
967
if p == "b" and not format_call :
1026
968
if self .chk .options .python_version < (3 , 5 ):
1027
- self .msg .fail (
1028
- 'Format character "b" is only supported in Python 3.5 and later' ,
1029
- context ,
1030
- code = codes .STRING_FORMATTING ,
1031
- )
969
+ self .msg .fail (message_registry .FORMAT_STR_INVALID_BYTES_SPECIFIER_PY35 , context )
1032
970
return None
1033
971
if not isinstance (expr , BytesExpr ):
1034
- self .msg .fail (
1035
- 'Format character "b" is only supported on bytes patterns' ,
1036
- context ,
1037
- code = codes .STRING_FORMATTING ,
1038
- )
972
+ self .msg .fail (message_registry .FORMAT_STR_INVALID_BYTES_SPECIFIER , context )
1039
973
return None
1040
974
return self .named_type ("builtins.bytes" )
1041
975
elif p == "a" :
0 commit comments