Incorrect str-format error for bytes interpolation when `__bytes__` method exists
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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:
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Es werden keine Quelldateien oder Tests genannt. Beginne damit, den bereitgestellten Python-Reproducer mit den Standardeinstellungen und mit deaktiviertem jeweiligen Promotion-Flag auszuführen, und finde dann den Prüfpfad für die str-Formatierung für die Interpolation von Bytes mit %b. Als erledigt gilt die Aufgabe, wenn Objekte mit __bytes__, einschließlich der gezeigten benutzerdefinierten Klasse, keinen str-format-Fehler mehr erzeugen, inkompatible Werte dies jedoch weiterhin tun.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100