From 6d3583dc6ff0dfffad6e13218f32a1d67b67119f Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Sat, 1 Aug 2020 19:40:04 -0700 Subject: [PATCH] messages: clarify bytes repr errors Resolves #9236 --- mypy/checkstrformat.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mypy/checkstrformat.py b/mypy/checkstrformat.py index f3081a2fa491..b9a2a4099e52 100644 --- a/mypy/checkstrformat.py +++ b/mypy/checkstrformat.py @@ -384,9 +384,10 @@ def perform_special_format_checks(self, spec: ConversionSpecifier, call: CallExp if self.chk.options.python_version >= (3, 0): if (has_type_component(actual_type, 'builtins.bytes') and not custom_special_method(actual_type, '__str__')): - self.msg.fail("On Python 3 '{}'.format(b'abc') produces \"b'abc'\";" - " use !r if this is a desired behavior", call, - code=codes.STR_BYTES_PY3) + self.msg.fail( + "On Python 3 '{}'.format(b'abc') produces \"b'abc'\", not 'abc'; " + "use '{!r}'.format(b'abc') if this is desired behavior", + call, code=codes.STR_BYTES_PY3) if spec.flags: numeric_types = UnionType([self.named_type('builtins.int'), self.named_type('builtins.float')]) @@ -843,9 +844,10 @@ def check_s_special_cases(self, expr: FormatStringExpr, typ: Type, context: Cont # Couple special cases for string formatting. if self.chk.options.python_version >= (3, 0): if has_type_component(typ, 'builtins.bytes'): - self.msg.fail("On Python 3 '%s' % b'abc' produces \"b'abc'\";" - " use %r if this is a desired behavior", context, - code=codes.STR_BYTES_PY3) + self.msg.fail( + "On Python 3 '%s' % b'abc' produces \"b'abc'\", not 'abc'; " + "use '%r' % b'abc' if this is desired behavior", + context, code=codes.STR_BYTES_PY3) if self.chk.options.python_version < (3, 0): if has_type_component(typ, 'builtins.unicode'): self.unicode_upcast = True