Skip to content

Commit 1f5b679

Browse files
committed
Remove python2 logic from checkstrformat.py
1 parent 1a65c1d commit 1f5b679

File tree

1 file changed

+37
-59
lines changed

1 file changed

+37
-59
lines changed

mypy/checkstrformat.py

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -453,17 +453,16 @@ def perform_special_format_checks(
453453
if len(c_typ.value) != 1:
454454
self.msg.requires_int_or_char(call, format_call=True)
455455
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+
)
467466
if spec.flags:
468467
numeric_types = UnionType(
469468
[self.named_type("builtins.int"), self.named_type("builtins.float")]
@@ -706,7 +705,7 @@ def check_str_interpolation(self, expr: FormatStringExpr, replacements: Expressi
706705
self.exprchk.accept(expr)
707706
specifiers = parse_conversion_specifiers(expr.value)
708707
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):
710709
self.msg.fail(
711710
"Bytes formatting is only supported in Python 3.5 and later",
712711
replacements,
@@ -819,7 +818,7 @@ def check_mapping_str_interpolation(
819818
):
820819
mapping: Dict[str, Type] = {}
821820
for k, v in replacements.items:
822-
if self.chk.options.python_version >= (3, 0) and isinstance(expr, BytesExpr):
821+
if isinstance(expr, BytesExpr):
823822
# Special case: for bytes formatting keys must be bytes.
824823
if not isinstance(k, BytesExpr):
825824
self.msg.fail(
@@ -870,21 +869,14 @@ def check_mapping_str_interpolation(
870869
def build_dict_type(self, expr: FormatStringExpr) -> Type:
871870
"""Build expected mapping type for right operand in % formatting."""
872871
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):
883876
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"
888880

889881
def build_replacement_checkers(
890882
self, specifiers: List[ConversionSpecifier], context: Context, expr: FormatStringExpr
@@ -979,29 +971,24 @@ def check_s_special_cases(self, expr: FormatStringExpr, typ: Type, context: Cont
979971
"""Additional special cases for %s in bytes vs string context."""
980972
if isinstance(expr, StrExpr):
981973
# 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
995983
if isinstance(expr, BytesExpr):
996984
# 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
1005992
return True
1006993

1007994
def checkers_for_c_type(
@@ -1016,7 +1003,7 @@ def checkers_for_c_type(
10161003

10171004
def check_type(type: Type) -> bool:
10181005
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):
10201007
err_msg = '"%c" requires an integer in range(256) or a single byte'
10211008
else:
10221009
err_msg = '"%c" requires int or char'
@@ -1037,13 +1024,11 @@ def check_expr(expr: Expression) -> None:
10371024
if check_type(type):
10381025
# Python 3 doesn't support b'%c' % str
10391026
if (
1040-
self.chk.options.python_version >= (3, 0)
1041-
and isinstance(format_expr, BytesExpr)
1027+
isinstance(format_expr, BytesExpr)
10421028
and isinstance(expr, BytesExpr)
10431029
and len(expr.value) != 1
10441030
):
10451031
self.msg.requires_int_or_single_byte(context)
1046-
# In Python 2, b'%c' is the same as '%c'
10471032
elif isinstance(expr, (StrExpr, BytesExpr)) and len(expr.value) != 1:
10481033
self.msg.requires_int_or_char(context)
10491034

@@ -1079,13 +1064,6 @@ def conversion_type(
10791064
return None
10801065
return self.named_type("builtins.bytes")
10811066
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
10891067
# TODO: return type object?
10901068
return AnyType(TypeOfAny.special_form)
10911069
elif p in ["s", "r"]:

0 commit comments

Comments
 (0)