Skip to content

Commit 3f07b1a

Browse files
cdce8pbdraco
andauthored
Update StreamResponse.write annotation for strict-bytes (#10154)
## What do these changes do? Mypy will add a `--strict-bytes` flag. python/mypy#18263 With that `bytearray` and `memoryview` are no longer subclasses of `bytes` and must be listed explicitly instead if they are supported. ## Are there changes in behavior for the user? -- ## Related issue number -- ## Checklist - [ ] I think the code is well written - [ ] Unit tests for the changes exist - [ ] Documentation reflects the changes - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` * The format is &lt;Name&gt; &lt;Surname&gt;. * Please keep alphabetical order, the file is sorted by names. - [ ] Add a new news fragment into the `CHANGES/` folder * name it `<issue_or_pr_num>.<type>.rst` (e.g. `588.bugfix.rst`) * if you don't have an issue number, change it to the pull request number after creating the PR * `.bugfix`: A bug fix for something the maintainers deemed an improper undesired behavior that got corrected to match pre-agreed expectations. * `.feature`: A new behavior, public APIs. That sort of stuff. * `.deprecation`: A declaration of future API removals and breaking changes in behavior. * `.breaking`: When something public is removed in a breaking way. Could be deprecated in an earlier release. * `.doc`: Notable updates to the documentation structure or build process. * `.packaging`: Notes for downstreams about unobvious side effects and tooling. Changes in the test invocation considerations and runtime assumptions. * `.contrib`: Stuff that affects the contributor experience. e.g. Running tests, building the docs, setting up the development environment. * `.misc`: Changes that are hard to assign to any of the above categories. * Make sure to use full sentences with correct case and punctuation, for example: ```rst Fixed issue with non-ascii contents in doctest text files -- by :user:`contributor-gh-handle`. ``` Use the past tense or the present tense a non-imperative mood, referring to what's changed compared to the last released version of this project. --------- Co-authored-by: J. Nick Koston <[email protected]>
1 parent 7f8e2d3 commit 3f07b1a

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

CHANGES/10154.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated :meth:`aiohttp.web.StreamResponse.write` annotation to also allow :class:`bytearray` and :class:`memoryview` as inputs -- by :user:`cdce8p`.

aiohttp/abc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Optional,
1717
Tuple,
1818
TypedDict,
19+
Union,
1920
)
2021

2122
from multidict import CIMultiDict
@@ -196,7 +197,7 @@ class AbstractStreamWriter(ABC):
196197
length: Optional[int] = 0
197198

198199
@abstractmethod
199-
async def write(self, chunk: bytes) -> None:
200+
async def write(self, chunk: Union[bytes, bytearray, memoryview]) -> None:
200201
"""Write chunk into stream."""
201202

202203
@abstractmethod

aiohttp/http_writer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def enable_compression(
7272
) -> None:
7373
self._compress = ZLibCompressor(encoding=encoding, strategy=strategy)
7474

75-
def _write(self, chunk: bytes) -> None:
75+
def _write(self, chunk: Union[bytes, bytearray, memoryview]) -> None:
7676
size = len(chunk)
7777
self.buffer_size += size
7878
self.output_size += size
@@ -93,7 +93,11 @@ def _writelines(self, chunks: Iterable[bytes]) -> None:
9393
transport.write(b"".join(chunks))
9494

9595
async def write(
96-
self, chunk: bytes, *, drain: bool = True, LIMIT: int = 0x10000
96+
self,
97+
chunk: Union[bytes, bytearray, memoryview],
98+
*,
99+
drain: bool = True,
100+
LIMIT: int = 0x10000,
97101
) -> None:
98102
"""Writes chunk of data to a stream.
99103

aiohttp/web_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ async def _write_headers(self) -> None:
432432
status_line = f"HTTP/{version[0]}.{version[1]} {self._status} {self._reason}"
433433
await writer.write_headers(status_line, self._headers)
434434

435-
async def write(self, data: bytes) -> None:
435+
async def write(self, data: Union[bytes, bytearray, memoryview]) -> None:
436436
assert isinstance(
437437
data, (bytes, bytearray, memoryview)
438438
), "data argument must be byte-ish (%r)" % type(data)

0 commit comments

Comments
 (0)