diff --git a/CHANGES/10154.bugfix.rst b/CHANGES/10154.bugfix.rst new file mode 100644 index 00000000000..131f88ee31e --- /dev/null +++ b/CHANGES/10154.bugfix.rst @@ -0,0 +1 @@ +Updated ``StreamResponse.write`` annotation to also allow ``bytearray`` and ``memoryview`` as inputs. diff --git a/aiohttp/abc.py b/aiohttp/abc.py index feeb3ad65c9..80c20bc24d2 100644 --- a/aiohttp/abc.py +++ b/aiohttp/abc.py @@ -196,7 +196,7 @@ class AbstractStreamWriter(ABC): length: Optional[int] = 0 @abstractmethod - async def write(self, chunk: bytes) -> None: + async def write(self, chunk: bytes | bytearray | memoryview) -> None: """Write chunk into stream.""" @abstractmethod diff --git a/aiohttp/http_writer.py b/aiohttp/http_writer.py index edd19ed65da..043354c3330 100644 --- a/aiohttp/http_writer.py +++ b/aiohttp/http_writer.py @@ -72,7 +72,7 @@ def enable_compression( ) -> None: self._compress = ZLibCompressor(encoding=encoding, strategy=strategy) - def _write(self, chunk: bytes) -> None: + def _write(self, chunk: bytes | bytearray | memoryview) -> None: size = len(chunk) self.buffer_size += size self.output_size += size @@ -93,7 +93,11 @@ def _writelines(self, chunks: Iterable[bytes]) -> None: transport.write(b"".join(chunks)) async def write( - self, chunk: bytes, *, drain: bool = True, LIMIT: int = 0x10000 + self, + chunk: bytes | bytearray | memoryview, + *, + drain: bool = True, + LIMIT: int = 0x10000, ) -> None: """Writes chunk of data to a stream. diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index cb3e3717c66..b1eb99a17e1 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -432,7 +432,7 @@ async def _write_headers(self) -> None: status_line = f"HTTP/{version[0]}.{version[1]} {self._status} {self._reason}" await writer.write_headers(status_line, self._headers) - async def write(self, data: bytes) -> None: + async def write(self, data: Union[bytes, bytearray, memoryview]) -> None: assert isinstance( data, (bytes, bytearray, memoryview) ), "data argument must be byte-ish (%r)" % type(data)