Skip to content

Commit

Permalink
messages: clarify bytes repr errors
Browse files Browse the repository at this point in the history
Resolves python#9236
  • Loading branch information
hauntsaninja committed Aug 2, 2020
1 parent 12855d4 commit 6d3583d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions mypy/checkstrformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')])
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6d3583d

Please sign in to comment.