Skip to content

Commit 209a719

Browse files
authored
Fixes error message for f string and str-bytes-safe (#11139)
It does not mention `.format()` anymore. Closes #10979
1 parent d37c2be commit 209a719

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

mypy/checkstrformat.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,9 @@ def perform_special_format_checks(self, spec: ConversionSpecifier, call: CallExp
380380
if (has_type_component(actual_type, 'builtins.bytes') and
381381
not custom_special_method(actual_type, '__str__')):
382382
self.msg.fail(
383-
"On Python 3 '{}'.format(b'abc') produces \"b'abc'\", not 'abc'; "
384-
"use '{!r}'.format(b'abc') if this is desired behavior",
383+
'On Python 3 formatting "b\'abc\'" with "{}" '
384+
'produces "b\'abc\'", not "abc"; '
385+
'use "{!r}" if this is desired behavior',
385386
call, code=codes.STR_BYTES_PY3)
386387
if spec.flags:
387388
numeric_types = UnionType([self.named_type('builtins.int'),
@@ -836,8 +837,9 @@ def check_s_special_cases(self, expr: FormatStringExpr, typ: Type, context: Cont
836837
if self.chk.options.python_version >= (3, 0):
837838
if has_type_component(typ, 'builtins.bytes'):
838839
self.msg.fail(
839-
"On Python 3 '%s' % b'abc' produces \"b'abc'\", not 'abc'; "
840-
"use '%r' % b'abc' if this is desired behavior",
840+
'On Python 3 formatting "b\'abc\'" with "%s" '
841+
'produces "b\'abc\'", not "abc"; '
842+
'use "%r" if this is desired behavior',
841843
context, code=codes.STR_BYTES_PY3)
842844
return False
843845
if self.chk.options.python_version < (3, 0):

mypyc/test-data/run-strings.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ def test_fstring_basics() -> None:
218218

219219
x = bytes([1, 2, 3, 4])
220220
# assert f'bytes: {x}' == "bytes: b'\\x01\\x02\\x03\\x04'"
221-
# error: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc';
222-
# use '{!r}'.format(b'abc') if this is desired behavior
221+
# error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc";
222+
# use "{!r}" if this is desired behavior behavior
223223

224224
float_num = 123.4
225225
assert f'{float_num}' == '123.4'
@@ -354,7 +354,7 @@ def test_format_method_basics() -> None:
354354
def test_format_method_empty_braces() -> None:
355355
name = 'Eric'
356356
age = 14
357-
357+
358358
assert 'Hello, {}!'.format(name) == 'Hello, Eric!'
359359
assert '{}'.format(name) == 'Eric'
360360
assert '{}! Hi!'.format(name) == 'Eric! Hi!'

test-data/unit/check-errorcodes.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,8 @@ def g() -> int:
665665
'%d' % 'no' # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsInt]") [str-format]
666666
'%d + %d' % (1, 2, 3) # E: Not all arguments converted during string formatting [str-format]
667667

668-
'{}'.format(b'abc') # E: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior [str-bytes-safe]
669-
'%s' % b'abc' # E: On Python 3 '%s' % b'abc' produces "b'abc'", not 'abc'; use '%r' % b'abc' if this is desired behavior [str-bytes-safe]
668+
'{}'.format(b'abc') # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior [str-bytes-safe]
669+
'%s' % b'abc' # E: On Python 3 formatting "b'abc'" with "%s" produces "b'abc'", not "abc"; use "%r" if this is desired behavior [str-bytes-safe]
670670
[builtins fixtures/primitives.pyi]
671671
[typing fixtures/typing-medium.pyi]
672672

test-data/unit/check-expressions.test

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,8 @@ xb: bytes
11831183
xs: str
11841184

11851185
'%s' % xs # OK
1186-
'%s' % xb # E: On Python 3 '%s' % b'abc' produces "b'abc'", not 'abc'; use '%r' % b'abc' if this is desired behavior
1187-
'%(name)s' % {'name': b'value'} # E: On Python 3 '%s' % b'abc' produces "b'abc'", not 'abc'; use '%r' % b'abc' if this is desired behavior
1186+
'%s' % xb # E: On Python 3 formatting "b'abc'" with "%s" produces "b'abc'", not "abc"; use "%r" if this is desired behavior
1187+
'%(name)s' % {'name': b'value'} # E: On Python 3 formatting "b'abc'" with "%s" produces "b'abc'", not "abc"; use "%r" if this is desired behavior
11881188
[builtins fixtures/primitives.pyi]
11891189

11901190
[case testStringInterpolationSBytesVsStrResultsPy2]
@@ -1617,22 +1617,32 @@ N = NewType('N', bytes)
16171617
n: N
16181618

16191619
'{}'.format(a)
1620-
'{}'.format(b) # E: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
1621-
'{}'.format(x) # E: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
1622-
'{}'.format(n) # E: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
1620+
'{}'.format(b) # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
1621+
'{}'.format(x) # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
1622+
'{}'.format(n) # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
1623+
1624+
f'{b}' # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
1625+
f'{x}' # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
1626+
f'{n}' # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
16231627

16241628
class C(Generic[B]):
16251629
x: B
16261630
def meth(self) -> None:
1627-
'{}'.format(self.x) # E: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
1631+
'{}'.format(self.x) # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
16281632

16291633
def func(x: A) -> A:
1630-
'{}'.format(x) # E: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
1634+
'{}'.format(x) # E: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior
16311635
return x
16321636

1637+
'{!r}'.format(a)
16331638
'{!r}'.format(b)
16341639
'{!r}'.format(x)
16351640
'{!r}'.format(n)
1641+
f'{a}'
1642+
f'{a!r}'
1643+
f'{b!r}'
1644+
f'{x!r}'
1645+
f'{n!r}'
16361646

16371647
class D(bytes):
16381648
def __str__(self) -> str:

0 commit comments

Comments
 (0)