From 876842d5626934d848d07c2bc14a8408c49a5ad2 Mon Sep 17 00:00:00 2001 From: Abdurrahman Hussain Date: Tue, 22 Sep 2026 20:08:21 -0700 Subject: [PATCH] Fix i2c_msg.__bytes__ return type in the type stub i2c_msg.__bytes__ is declared as returning str, but it returns ctypes.string_at(self.buf, self.len) which is bytes. The class's own __str__ relies on that -- it calls self.__bytes__().decode(errors="ignore"), which is only valid on bytes. Because the package is py.typed, this propagates to callers: bytes(msg) is inferred as str, so passing the result anywhere expecting bytes is reported as an error by a type checker. Same class of issue as #142 / #143, which corrected __iter__ on the neighbouring line. Co-Authored-By: Claude Opus 5 (1M context) --- smbus2/smbus2.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smbus2/smbus2.pyi b/smbus2/smbus2.pyi index 5930268..2315e12 100644 --- a/smbus2/smbus2.pyi +++ b/smbus2/smbus2.pyi @@ -69,7 +69,7 @@ class i2c_smbus_ioctl_data(Structure): class i2c_msg(Structure): def __iter__(self) -> Iterator[int]: ... def __len__(self) -> int: ... - def __bytes__(self) -> str: ... + def __bytes__(self) -> bytes: ... @staticmethod def read(address: int, length: int) -> "i2c_msg": ... @staticmethod