From 1b36beaa6a68a5dfed130652797e31cfab84354b Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:22:12 +0100 Subject: [PATCH 1/3] Update StreamResponse.write annotation for strict-bytes --- CHANGES/10154.bugfix.rst | 1 + aiohttp/abc.py | 3 ++- aiohttp/http_writer.py | 8 ++++++-- aiohttp/web_response.py | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 CHANGES/10154.bugfix.rst 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..8032cf053e2 100644 --- a/aiohttp/abc.py +++ b/aiohttp/abc.py @@ -16,6 +16,7 @@ Optional, Tuple, TypedDict, + Union, ) from multidict import CIMultiDict @@ -196,7 +197,7 @@ class AbstractStreamWriter(ABC): length: Optional[int] = 0 @abstractmethod - async def write(self, chunk: bytes) -> None: + async def write(self, chunk: Union[bytes, bytearray, memoryview]) -> None: """Write chunk into stream.""" @abstractmethod diff --git a/aiohttp/http_writer.py b/aiohttp/http_writer.py index edd19ed65da..28b14f7a791 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: Union[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: Union[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) From f362a8baacab8bbb0699194bc138b870a3763ee1 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:58:36 +0100 Subject: [PATCH 2/3] Update CHANGES/10154.bugfix.rst Co-authored-by: J. Nick Koston --- CHANGES/10154.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES/10154.bugfix.rst b/CHANGES/10154.bugfix.rst index 131f88ee31e..a8a1925ddda 100644 --- a/CHANGES/10154.bugfix.rst +++ b/CHANGES/10154.bugfix.rst @@ -1 +1 @@ -Updated ``StreamResponse.write`` annotation to also allow ``bytearray`` and ``memoryview`` as inputs. +Updated :meth:`aiohttp.web.StreamResponse.write` annotation to also allow ``bytearray`` and ``memoryview`` as inputs -- by :user:`cdce8p`. From 526b54f9e9481959c294b98883db481cfb3d0c20 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 10 Dec 2024 23:34:16 +0100 Subject: [PATCH 3/3] Add cross references to the Python docs --- CHANGES/10154.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES/10154.bugfix.rst b/CHANGES/10154.bugfix.rst index a8a1925ddda..382d9e56e6c 100644 --- a/CHANGES/10154.bugfix.rst +++ b/CHANGES/10154.bugfix.rst @@ -1 +1 @@ -Updated :meth:`aiohttp.web.StreamResponse.write` annotation to also allow ``bytearray`` and ``memoryview`` as inputs -- by :user:`cdce8p`. +Updated :meth:`aiohttp.web.StreamResponse.write` annotation to also allow :class:`bytearray` and :class:`memoryview` as inputs -- by :user:`cdce8p`.