Skip to content

Commit

Permalink
[PR aio-libs#7775/5f64328c backport][3.9] Add codespell support: pre-…
Browse files Browse the repository at this point in the history
…commit entry, configuration, some typoes get fixed (aio-libs#7800)

**This is a backport of PR aio-libs#7775 as merged into master
(5f64328).**

See https://github.com/codespell-project/codespell for the codespell
project. I like it and promote everywhere I go ;)

but feel free to disregard this PR, may be take just last commit with 1
obvious typo and may be the "repr" typo fix.

Another commit fixes typos it found and some were I guess whitelisted in
docs/spelling_wordlist.txt where it fixed them too. What is the role/how
that file is used? (I am not familiar, but found similar ones in
jsonschema and few other projects)

somewhat since there is following fix

```
         if t is None:
-            t_repr = "<<Unkown>>"
+            t_repr = "<<Unknown>>"
```

so some reprs would be effected . another change is functional in the
test (taking "an" not "ans" from "answer") but that must not be user
visible

please advise on either you see value for me to bother with CHANGES etc

- [ ] 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_id>.<type>` for example (588.bugfix)
* if you don't have an `issue_id` change it to the pr id after creating
the pr
  * ensure type is one of the following:
    * `.feature`: Signifying a new feature.
    * `.bugfix`: Signifying a bug fix.
    * `.doc`: Signifying a documentation improvement.
    * `.removal`: Signifying a deprecation or removal of public API.
* `.misc`: A ticket has been closed, but it is not of interest to users.
* Make sure to use full sentences with correct case and punctuation, for
example: "Fix issue with non-ascii contents in doctest text files."

Co-authored-by: Yaroslav Halchenko <[email protected]>
  • Loading branch information
2 people authored and Xiang Li committed Dec 4, 2023
1 parent ff6ac61 commit a96fc47
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 15 deletions.
2 changes: 1 addition & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ async def read(self) -> bytes:
except BaseException:
self.close()
raise
elif self._released: # Response explicity released
elif self._released: # Response explicitly released
raise ClientConnectionError("Connection closed")

await self._wait_released() # Underlying connection released
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ def __repr__(self) -> str:
t = get_args(self.__orig_class__)[0]

if t is None:
t_repr = "<<Unkown>>"
t_repr = "<<Unknown>>"
elif isinstance(t, type):
if t.__module__ == "builtins":
t_repr = t.__qualname__
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ def feed_data(

# if stream does not contain trailer, after 0\r\n
# we should get another \r\n otherwise
# trailers needs to be skiped until \r\n\r\n
# trailers needs to be skipped until \r\n\r\n
if self._chunk == ChunkState.PARSE_MAYBE_TRAILERS:
head = chunk[: len(SEP)]
if head == SEP:
Expand Down
75 changes: 75 additions & 0 deletions aiohttp/payload_streamer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Payload implementation for coroutines as data provider.
As a simple case, you can upload data from file::
@aiohttp.streamer
async def file_sender(writer, file_name=None):
with open(file_name, 'rb') as f:
chunk = f.read(2**16)
while chunk:
await writer.write(chunk)
chunk = f.read(2**16)
Then you can use `file_sender` like this:
async with session.post('http://httpbin.org/post',
data=file_sender(file_name='huge_file')) as resp:
print(await resp.text())
..note:: Coroutine must accept `writer` as first argument
"""

import types
import warnings
from typing import Any, Awaitable, Callable, Dict, Tuple

from .abc import AbstractStreamWriter
from .payload import Payload, payload_type

__all__ = ("streamer",)


class _stream_wrapper:
def __init__(
self,
coro: Callable[..., Awaitable[None]],
args: Tuple[Any, ...],
kwargs: Dict[str, Any],
) -> None:
self.coro = types.coroutine(coro)
self.args = args
self.kwargs = kwargs

async def __call__(self, writer: AbstractStreamWriter) -> None:
await self.coro(writer, *self.args, **self.kwargs)


class streamer:
def __init__(self, coro: Callable[..., Awaitable[None]]) -> None:
warnings.warn(
"@streamer is deprecated, use async generators instead",
DeprecationWarning,
stacklevel=2,
)
self.coro = coro

def __call__(self, *args: Any, **kwargs: Any) -> _stream_wrapper:
return _stream_wrapper(self.coro, args, kwargs)


@payload_type(_stream_wrapper)
class StreamWrapperPayload(Payload):
async def write(self, writer: AbstractStreamWriter) -> None:
await self._value(writer)


@payload_type(streamer)
class StreamPayload(StreamWrapperPayload):
def __init__(self, value: Any, *args: Any, **kwargs: Any) -> None:
super().__init__(value(), *args, **kwargs)

async def write(self, writer: AbstractStreamWriter) -> None:
await self._value(writer)
2 changes: 1 addition & 1 deletion aiohttp/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def get(self, key: Union[str, AppKey[_T]], default: Any = None) -> Any:
def loop(self) -> asyncio.AbstractEventLoop:
# Technically the loop can be None
# but we mask it by explicit type cast
# to provide more convinient type annotation
# to provide more convenient type annotation
warnings.warn("loop property is deprecated", DeprecationWarning, stacklevel=2)
return cast(asyncio.AbstractEventLoop, self._loop)

Expand Down
21 changes: 15 additions & 6 deletions docs/client_quickstart.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.. currentmodule:: aiohttp

.. _aiohttp-client-quickstart:

===================
Client Quickstart
===================

.. currentmodule:: aiohttp

Eager to get started? This page gives a good introduction in how to
get started with aiohttp client API.

Expand Down Expand Up @@ -77,9 +77,6 @@ endpoints of ``http://httpbin.org`` can be used the following code::
A session contains a connection pool inside. Connection reusage and
keep-alive (both are on by default) may speed up total performance.

You may find more information about creating persistent sessions
in :ref:`aiohttp-persistent-session`.

A session context manager usage is not mandatory
but ``await session.close()`` method
should be called in this case, e.g.::
Expand Down Expand Up @@ -198,7 +195,7 @@ Any of session's request methods like :func:`request`,
`json` parameter::

async with aiohttp.ClientSession() as session:
await session.post(url, json={'test': 'object'})
async with session.post(url, json={'test': 'object'})


By default session uses python's standard :mod:`json` module for
Expand Down Expand Up @@ -368,6 +365,18 @@ can chain get and post requests together::
await session.post('http://httpbin.org/post',
data=resp.content)

.. note::

Python 3.5 has no native support for asynchronous generators, use
``async_generator`` library as workaround.

.. deprecated:: 3.1

``aiohttp`` still supports ``aiohttp.streamer`` decorator but this
approach is deprecated in favor of *asynchronous generators* as
shown above.


.. _aiohttp-client-websockets:


Expand Down
2 changes: 1 addition & 1 deletion docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ Response object
Reading from the stream may raise
:exc:`aiohttp.ClientPayloadError` if the response object is
closed before response receives all data or in case if any
transfer encoding related errors like misformed chunked
transfer encoding related errors like malformed chunked
encoding of broken compression data.

.. attribute:: cookies
Expand Down
2 changes: 1 addition & 1 deletion docs/web_quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Resource in turn has at least one *route*.

Route corresponds to handling *HTTP method* by calling *web handler*.

Thus when you add a *route* the *resouce* object is created under the hood.
Thus when you add a *route* the *resource* object is created under the hood.

The library implementation **merges** all subsequent route additions
for the same path adding the only resource for all HTTP methods.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,7 @@ async def handler(request):

async def test_payload_content_length_by_chunks(aiohttp_client) -> None:
async def handler(request):
resp = web.StreamResponse(headers={"content-length": "3"})
resp = web.StreamResponse(headers={"content-length": "2"})
await resp.prepare(request)
await resp.write(b"answer")
await resp.write(b"two")
Expand All @@ -2059,7 +2059,7 @@ async def handler(request):

resp = await client.get("/")
data = await resp.read()
assert data == b"ans"
assert data == b"an"
resp.close()


Expand Down
2 changes: 1 addition & 1 deletion tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def stream():
return mock.Mock()


@pytest.mark.skipif(NO_EXTENSIONS, reason="Extentions available but not imported")
@pytest.mark.skipif(NO_EXTENSIONS, reason="Extensions available but not imported")
def test_c_parser_loaded():
assert "HttpRequestParserC" in dir(aiohttp.http_parser)
assert "HttpResponseParserC" in dir(aiohttp.http_parser)
Expand Down

0 comments on commit a96fc47

Please sign in to comment.