Skip to content

Commit

Permalink
Update StreamResponse.write annotation for strict-bytes (#10154)
Browse files Browse the repository at this point in the history
## 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 <Name> <Surname>.
  * 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]>
(cherry picked from commit 3f07b1a)
  • Loading branch information
cdce8p authored and patchback[bot] committed Dec 11, 2024
1 parent b770b1a commit 395fe83
Show file tree
Hide file tree
Showing 4 changed files with 10 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 :meth:`aiohttp.web.StreamResponse.write` annotation to also allow :class:`bytearray` and :class:`memoryview` as inputs -- by :user:`cdce8p`.
3 changes: 2 additions & 1 deletion aiohttp/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Optional,
Tuple,
TypedDict,
Union,
)

from multidict import CIMultiDict
Expand Down Expand Up @@ -200,7 +201,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
Expand Down
8 changes: 6 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: Union[bytes, bytearray, memoryview]) -> None:
size = len(chunk)
self.buffer_size += size
self.output_size += size
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,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 395fe83

Please sign in to comment.