Skip to content

Commit

Permalink
Add target-version to black config (#9962)
Browse files Browse the repository at this point in the history
Add `target-version` option to black config in `pyproject.toml` and
reformat code.

https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#t-target-version
(cherry picked from commit 00fd4eb)
  • Loading branch information
cdce8p authored and asvetlov committed Nov 19, 2024
1 parent db43803 commit 5a03c5a
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 144 deletions.
10 changes: 5 additions & 5 deletions aiohttp/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ async def __call__(
__param: Application,
*,
server_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Any
**kwargs: Any,
) -> TestClient[Request, Application]: ...
@overload
async def __call__(
self,
__param: BaseTestServer,
*,
server_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Any
**kwargs: Any,
) -> TestClient[BaseRequest, None]: ...


Expand Down Expand Up @@ -379,22 +379,22 @@ async def go(
__param: Application,
*,
server_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Any
**kwargs: Any,
) -> TestClient[Request, Application]: ...

@overload
async def go(
__param: BaseTestServer,
*,
server_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Any
**kwargs: Any,
) -> TestClient[BaseRequest, None]: ...

async def go(
__param: Union[Application, BaseTestServer],
*args: Any,
server_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Any
**kwargs: Any,
) -> TestClient[Any, Any]:
if isinstance(__param, Callable) and not isinstance( # type: ignore[arg-type]
__param, (Application, BaseTestServer)
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(
self,
loop: Optional[asyncio.AbstractEventLoop] = None,
*args: Any,
**kwargs: Any
**kwargs: Any,
) -> None:
if aiodns is None:
raise RuntimeError("Resolver requires aiodns library")
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(
request_factory: Optional[_RequestFactory] = None,
handler_cancellation: bool = False,
loop: Optional[asyncio.AbstractEventLoop] = None,
**kwargs: Any
**kwargs: Any,
) -> None:
self._loop = loop or asyncio.get_running_loop()
self._connections: Dict[RequestHandler, asyncio.Transport] = {}
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ ignore-words-list = 'te'
# TODO(3.13): Remove aiohttp.helpers once https://github.com/python/cpython/pull/106771
# is available in all supported cpython versions
exclude-modules = "(^aiohttp\\.helpers)"

[tool.black]
# TODO: Remove when project metadata is moved here.
# Black can read the value from [project.requires-python].
target-version = ["py39", "py310", "py311", "py312"]
7 changes: 4 additions & 3 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2955,9 +2955,10 @@ async def close(self) -> None:

connector = aiohttp.TCPConnector(resolver=FakeResolver(), ssl=False)

async with aiohttp.ClientSession(connector=connector) as client, client.get(
url_from, auth=aiohttp.BasicAuth("user", "pass")
) as resp:
async with (
aiohttp.ClientSession(connector=connector) as client,
client.get(url_from, auth=aiohttp.BasicAuth("user", "pass")) as resp,
):
assert len(resp.history) == 1
assert str(resp.url) == "http://example.com"
assert resp.status == 200
Expand Down
22 changes: 12 additions & 10 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,12 @@ async def create_connection(req, traces, timeout):
return create_mocked_conn()

connector = session._connector
with mock.patch.object(connector, "connect", connect), mock.patch.object(
connector, "_create_connection", create_connection
), mock.patch.object(connector, "_release"), mock.patch(
"aiohttp.client.os"
) as m_os:
with (
mock.patch.object(connector, "connect", connect),
mock.patch.object(connector, "_create_connection", create_connection),
mock.patch.object(connector, "_release"),
mock.patch("aiohttp.client.os") as m_os,
):
m_os.urandom.return_value = key_data
await session.ws_connect(f"{protocol}://example")

Expand Down Expand Up @@ -576,11 +577,12 @@ async def create_connection(
return create_mocked_conn()

connector = session._connector
with mock.patch.object(connector, "connect", connect), mock.patch.object(
connector, "_create_connection", create_connection
), mock.patch.object(connector, "_release"), mock.patch(
"aiohttp.client.os"
) as m_os:
with (
mock.patch.object(connector, "connect", connect),
mock.patch.object(connector, "_create_connection", create_connection),
mock.patch.object(connector, "_release"),
mock.patch("aiohttp.client.os") as m_os,
):
m_os.urandom.return_value = key_data
await session.ws_connect(f"{protocol}://example")

Expand Down
29 changes: 17 additions & 12 deletions tests/test_client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ async def test_ws_connect_read_timeout_is_reset_to_inf(
hdrs.SEC_WEBSOCKET_PROTOCOL: "chat",
}
resp.connection.protocol.read_timeout = 0.5
with mock.patch("aiohttp.client.os") as m_os, mock.patch(
"aiohttp.client.ClientSession.request"
) as m_req:
with (
mock.patch("aiohttp.client.os") as m_os,
mock.patch("aiohttp.client.ClientSession.request") as m_req,
):
m_os.urandom.return_value = key_data
m_req.return_value = loop.create_future()
m_req.return_value.set_result(resp)
Expand All @@ -80,9 +81,10 @@ async def test_ws_connect_read_timeout_stays_inf(
hdrs.SEC_WEBSOCKET_PROTOCOL: "chat",
}
resp.connection.protocol.read_timeout = None
with mock.patch("aiohttp.client.os") as m_os, mock.patch(
"aiohttp.client.ClientSession.request"
) as m_req:
with (
mock.patch("aiohttp.client.os") as m_os,
mock.patch("aiohttp.client.ClientSession.request") as m_req,
):
m_os.urandom.return_value = key_data
m_req.return_value = loop.create_future()
m_req.return_value.set_result(resp)
Expand Down Expand Up @@ -111,9 +113,10 @@ async def test_ws_connect_read_timeout_reset_to_max(
hdrs.SEC_WEBSOCKET_PROTOCOL: "chat",
}
resp.connection.protocol.read_timeout = 0.5
with mock.patch("aiohttp.client.os") as m_os, mock.patch(
"aiohttp.client.ClientSession.request"
) as m_req:
with (
mock.patch("aiohttp.client.os") as m_os,
mock.patch("aiohttp.client.ClientSession.request") as m_req,
):
m_os.urandom.return_value = key_data
m_req.return_value = loop.create_future()
m_req.return_value.set_result(resp)
Expand Down Expand Up @@ -416,9 +419,11 @@ async def test_close_connection_lost(
hdrs.SEC_WEBSOCKET_ACCEPT: ws_key,
}
mresp.connection.protocol.read_timeout = None
with mock.patch("aiohttp.client.WebSocketWriter"), mock.patch(
"aiohttp.client.os"
) as m_os, mock.patch("aiohttp.client.ClientSession.request") as m_req:
with (
mock.patch("aiohttp.client.WebSocketWriter"),
mock.patch("aiohttp.client.os") as m_os,
mock.patch("aiohttp.client.ClientSession.request") as m_req,
):
m_os.urandom.return_value = key_data
m_req.return_value = loop.create_future()
m_req.return_value.set_result(mresp)
Expand Down
13 changes: 8 additions & 5 deletions tests/test_client_ws_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,11 +756,14 @@ async def handler(request: web.Request) -> NoReturn:
# since if we closed the connection normally, the client would
# would cancel the heartbeat task and we wouldn't get a ping
assert resp._conn is not None
with mock.patch.object(
resp._conn.transport, "write", side_effect=ClientConnectionResetError
), mock.patch.object(
resp._writer, "send_frame", wraps=resp._writer.send_frame
) as send_frame:
with (
mock.patch.object(
resp._conn.transport, "write", side_effect=ClientConnectionResetError
),
mock.patch.object(
resp._writer, "send_frame", wraps=resp._writer.send_frame
) as send_frame,
):
await resp.receive()
ping_count = send_frame.call_args_list.count(mock.call(b"", WSMsgType.PING))
# Connection should be closed roughly after 1.5x heartbeat.
Expand Down
Loading

0 comments on commit 5a03c5a

Please sign in to comment.