@@ -453,17 +453,16 @@ def perform_special_format_checks(
453
453
if len (c_typ .value ) != 1 :
454
454
self .msg .requires_int_or_char (call , format_call = True )
455
455
if (not spec .conv_type or spec .conv_type == "s" ) and not spec .conversion :
456
- if self .chk .options .python_version >= (3 , 0 ):
457
- if has_type_component (actual_type , "builtins.bytes" ) and not custom_special_method (
458
- actual_type , "__str__"
459
- ):
460
- self .msg .fail (
461
- 'On Python 3 formatting "b\' abc\' " with "{}" '
462
- 'produces "b\' abc\' ", not "abc"; '
463
- 'use "{!r}" if this is desired behavior' ,
464
- call ,
465
- code = codes .STR_BYTES_PY3 ,
466
- )
456
+ if has_type_component (actual_type , "builtins.bytes" ) and not custom_special_method (
457
+ actual_type , "__str__"
458
+ ):
459
+ self .msg .fail (
460
+ 'On Python 3 formatting "b\' abc\' " with "{}" '
461
+ 'produces "b\' abc\' ", not "abc"; '
462
+ 'use "{!r}" if this is desired behavior' ,
463
+ call ,
464
+ code = codes .STR_BYTES_PY3 ,
465
+ )
467
466
if spec .flags :
468
467
numeric_types = UnionType (
469
468
[self .named_type ("builtins.int" ), self .named_type ("builtins.float" )]
@@ -706,7 +705,7 @@ def check_str_interpolation(self, expr: FormatStringExpr, replacements: Expressi
706
705
self .exprchk .accept (expr )
707
706
specifiers = parse_conversion_specifiers (expr .value )
708
707
has_mapping_keys = self .analyze_conversion_specifiers (specifiers , expr )
709
- if isinstance (expr , BytesExpr ) and ( 3 , 0 ) <= self .chk .options .python_version < (3 , 5 ):
708
+ if isinstance (expr , BytesExpr ) and self .chk .options .python_version < (3 , 5 ):
710
709
self .msg .fail (
711
710
"Bytes formatting is only supported in Python 3.5 and later" ,
712
711
replacements ,
@@ -819,7 +818,7 @@ def check_mapping_str_interpolation(
819
818
):
820
819
mapping : Dict [str , Type ] = {}
821
820
for k , v in replacements .items :
822
- if self . chk . options . python_version >= ( 3 , 0 ) and isinstance (expr , BytesExpr ):
821
+ if isinstance (expr , BytesExpr ):
823
822
# Special case: for bytes formatting keys must be bytes.
824
823
if not isinstance (k , BytesExpr ):
825
824
self .msg .fail (
@@ -870,21 +869,14 @@ def check_mapping_str_interpolation(
870
869
def build_dict_type (self , expr : FormatStringExpr ) -> Type :
871
870
"""Build expected mapping type for right operand in % formatting."""
872
871
any_type = AnyType (TypeOfAny .special_form )
873
- if self .chk .options .python_version >= (3 , 0 ):
874
- if isinstance (expr , BytesExpr ):
875
- bytes_type = self .chk .named_generic_type ("builtins.bytes" , [])
876
- return self .chk .named_generic_type ("typing.Mapping" , [bytes_type , any_type ])
877
- elif isinstance (expr , StrExpr ):
878
- str_type = self .chk .named_generic_type ("builtins.str" , [])
879
- return self .chk .named_generic_type ("typing.Mapping" , [str_type , any_type ])
880
- else :
881
- assert False , "There should not be UnicodeExpr on Python 3"
882
- else :
872
+ if isinstance (expr , BytesExpr ):
873
+ bytes_type = self .chk .named_generic_type ("builtins.bytes" , [])
874
+ return self .chk .named_generic_type ("typing.Mapping" , [bytes_type , any_type ])
875
+ elif isinstance (expr , StrExpr ):
883
876
str_type = self .chk .named_generic_type ("builtins.str" , [])
884
- unicode_type = self .chk .named_generic_type ("builtins.unicode" , [])
885
- str_map = self .chk .named_generic_type ("typing.Mapping" , [str_type , any_type ])
886
- unicode_map = self .chk .named_generic_type ("typing.Mapping" , [unicode_type , any_type ])
887
- return UnionType .make_union ([str_map , unicode_map ])
877
+ return self .chk .named_generic_type ("typing.Mapping" , [str_type , any_type ])
878
+ else :
879
+ assert False , "There should not be UnicodeExpr on Python 3"
888
880
889
881
def build_replacement_checkers (
890
882
self , specifiers : List [ConversionSpecifier ], context : Context , expr : FormatStringExpr
@@ -979,29 +971,24 @@ def check_s_special_cases(self, expr: FormatStringExpr, typ: Type, context: Cont
979
971
"""Additional special cases for %s in bytes vs string context."""
980
972
if isinstance (expr , StrExpr ):
981
973
# Couple special cases for string formatting.
982
- if self .chk .options .python_version >= (3 , 0 ):
983
- if has_type_component (typ , "builtins.bytes" ):
984
- self .msg .fail (
985
- 'On Python 3 formatting "b\' abc\' " with "%s" '
986
- 'produces "b\' abc\' ", not "abc"; '
987
- 'use "%r" if this is desired behavior' ,
988
- context ,
989
- code = codes .STR_BYTES_PY3 ,
990
- )
991
- return False
992
- if self .chk .options .python_version < (3 , 0 ):
993
- if has_type_component (typ , "builtins.unicode" ):
994
- self .unicode_upcast = True
974
+ if has_type_component (typ , "builtins.bytes" ):
975
+ self .msg .fail (
976
+ 'On Python 3 formatting "b\' abc\' " with "%s" '
977
+ 'produces "b\' abc\' ", not "abc"; '
978
+ 'use "%r" if this is desired behavior' ,
979
+ context ,
980
+ code = codes .STR_BYTES_PY3 ,
981
+ )
982
+ return False
995
983
if isinstance (expr , BytesExpr ):
996
984
# A special case for bytes formatting: b'%s' actually requires bytes on Python 3.
997
- if self .chk .options .python_version >= (3 , 0 ):
998
- if has_type_component (typ , "builtins.str" ):
999
- self .msg .fail (
1000
- "On Python 3 b'%s' requires bytes, not string" ,
1001
- context ,
1002
- code = codes .STRING_FORMATTING ,
1003
- )
1004
- return False
985
+ if has_type_component (typ , "builtins.str" ):
986
+ self .msg .fail (
987
+ "On Python 3 b'%s' requires bytes, not string" ,
988
+ context ,
989
+ code = codes .STRING_FORMATTING ,
990
+ )
991
+ return False
1005
992
return True
1006
993
1007
994
def checkers_for_c_type (
@@ -1016,7 +1003,7 @@ def checkers_for_c_type(
1016
1003
1017
1004
def check_type (type : Type ) -> bool :
1018
1005
assert expected_type is not None
1019
- if self . chk . options . python_version >= ( 3 , 0 ) and isinstance (format_expr , BytesExpr ):
1006
+ if isinstance (format_expr , BytesExpr ):
1020
1007
err_msg = '"%c" requires an integer in range(256) or a single byte'
1021
1008
else :
1022
1009
err_msg = '"%c" requires int or char'
@@ -1037,13 +1024,11 @@ def check_expr(expr: Expression) -> None:
1037
1024
if check_type (type ):
1038
1025
# Python 3 doesn't support b'%c' % str
1039
1026
if (
1040
- self .chk .options .python_version >= (3 , 0 )
1041
- and isinstance (format_expr , BytesExpr )
1027
+ isinstance (format_expr , BytesExpr )
1042
1028
and isinstance (expr , BytesExpr )
1043
1029
and len (expr .value ) != 1
1044
1030
):
1045
1031
self .msg .requires_int_or_single_byte (context )
1046
- # In Python 2, b'%c' is the same as '%c'
1047
1032
elif isinstance (expr , (StrExpr , BytesExpr )) and len (expr .value ) != 1 :
1048
1033
self .msg .requires_int_or_char (context )
1049
1034
@@ -1079,13 +1064,6 @@ def conversion_type(
1079
1064
return None
1080
1065
return self .named_type ("builtins.bytes" )
1081
1066
elif p == "a" :
1082
- if self .chk .options .python_version < (3 , 0 ):
1083
- self .msg .fail (
1084
- 'Format character "a" is only supported in Python 3' ,
1085
- context ,
1086
- code = codes .STRING_FORMATTING ,
1087
- )
1088
- return None
1089
1067
# TODO: return type object?
1090
1068
return AnyType (TypeOfAny .special_form )
1091
1069
elif p in ["s" , "r" ]:
0 commit comments