Skip to content

Commit

Permalink
Update StreamResponse.write annotation for strict-bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Dec 10, 2024
1 parent 7f8e2d3 commit dd3c209
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/10154.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated ``StreamResponse.write`` annotation to also allow ``bytearray`` and ``memoryview`` as inputs.
2 changes: 1 addition & 1 deletion aiohttp/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/http_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -93,7 +93,7 @@ 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.
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dd3c209

Please sign in to comment.