python / python/mypy

Incorrect str-format error for bytes interpolation when `__bytes__` method exists

Open
#19,724 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

When the % operator is used to format a bytes string with a placeholder of %b (bytes) and an argument of type bytearray the interpolation will succeed as bytearray has a __bytes__ method as per PEP 0461. The same is also true of memoryview objects as well as any type with a __bytes__ method.

MyPy will not flag an error for bytearray or memoryview objects but will for classes with a __bytes__ method. If --disable-bytearray-promotion or --disable-memoryview-promotion are used then these string interpolations will be flagged as a str-format error - presumably is it stops silently treating these classes as equivalent to bytes.

PEP 0461 states that %b should be correct for any class with a __bytes__ method.

%b will insert a series of bytes. These bytes are collected in one of two ways:

  • input type supports Py_buffer [4]? use it to collect the necessary bytes
  • input type is something else? use its bytes method [5] ; if there isn’t one, raise a TypeError

To Reproduce

MyPy reports no errors with this code by default.

a = b"foo"
b = bytearray(a)
c = memoryview(a)

x = b"%b" % a
y = b"%b" % b
z = b"%b" % c

With --disable-bytearray-promotion it reports

/tmp/test.py:6: error: Incompatible types in string interpolation (expression has type "bytearray", placeholder has type "bytes") [str-format]

Similarly

class X:
    def __bytes__(self) -> bytes:
        return b"X"


m = b"%b" % X()

Will produce the error

/tmp/test.py:6: error: Incompatible types in string interpolation (expression has type "X", placeholder has type "bytes") [str-format]

Both of these samples run without error and with the expected behaviour.

Expected Behavior

No str-format error reported for interpolating %b placeholder in bytes string for objects with a __bytes__ method.

Your Environment

  • Mypy version used: 1.17.1
  • Mypy command-line flags: Defaults or --disable-bytearrary-promotion or --disable-memoryview-promotion
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.13.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named. Start by running the provided Python reproducer with the default settings and each promotion flag disabled, then locate the str-format checking path for bytes %b interpolation. Done means objects with __bytes__, including the shown custom class, no longer produce a str-format error while incompatible values still do.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.