From 5c176c7a6629835a8e3724cb94baf1c6a0e0d080 Mon Sep 17 00:00:00 2001 From: Tester Date: Tue, 13 Feb 2024 19:14:43 +0330 Subject: [PATCH] format `httpcore/_(a)sync` to pass unasync check --- httpcore/_async/connection.py | 14 +++++++++++--- httpcore/_async/connection_pool.py | 5 +++-- httpcore/_async/http11.py | 17 +++++++++++++---- httpcore/_async/http2.py | 17 +++++++++++++---- httpcore/_async/http_proxy.py | 2 +- httpcore/_sync/connection.py | 18 +++++++++++++++--- httpcore/_sync/connection_pool.py | 9 +++++++-- httpcore/_sync/http11.py | 25 +++++++++++++++++++++---- httpcore/_sync/http2.py | 25 +++++++++++++++++++++---- httpcore/_sync/http_proxy.py | 4 +++- 10 files changed, 108 insertions(+), 28 deletions(-) diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index 8c2355c8..3a7d18d5 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -119,7 +119,12 @@ async def _connect(self, request: Request) -> AsyncNetworkStream: "timeout": timeout, "socket_options": self._socket_options, } - async with Trace("connect_tcp", logger, request, kwargs) as trace: + async with Trace( + "connect_tcp", + logger, + request, + kwargs, + ) as trace: stream = await self._network_backend.connect_tcp(**kwargs) trace.return_value = stream else: @@ -129,10 +134,13 @@ async def _connect(self, request: Request) -> AsyncNetworkStream: "socket_options": self._socket_options, } async with Trace( - "connect_unix_socket", logger, request, kwargs + "connect_unix_socket", + logger, + request, + kwargs, ) as trace: stream = await self._network_backend.connect_unix_socket( - **kwargs + **kwargs, ) trace.return_value = stream diff --git a/httpcore/_async/connection_pool.py b/httpcore/_async/connection_pool.py index 5d701bd5..60248a0a 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -26,7 +26,8 @@ def __init__(self, request: Request) -> None: self._connection_acquired = AsyncEvent() def assign_to_connection( - self, connection: Optional[AsyncConnectionInterface] + self, + connection: Optional[AsyncConnectionInterface], ) -> None: self.connection = connection self._connection_acquired.set() @@ -205,7 +206,7 @@ async def handle_async_request(self, request: Request) -> Response: try: # Send the request on the assigned connection. response = await connection.handle_async_request( - pool_request.request + pool_request.request, ) except ConnectionNotAvailable: # In some cases a connection may initially be available to diff --git a/httpcore/_async/http11.py b/httpcore/_async/http11.py index a5eb4808..1ee63679 100644 --- a/httpcore/_async/http11.py +++ b/httpcore/_async/http11.py @@ -86,7 +86,10 @@ async def handle_async_request(self, request: Request) -> Response: kwargs = {"request": request} try: async with Trace( - "send_request_headers", logger, request, kwargs + "send_request_headers", + logger, + request, + kwargs, ) as trace: await self._send_request_headers(**kwargs) async with Trace("send_request_body", logger, request, kwargs) as trace: @@ -100,7 +103,10 @@ async def handle_async_request(self, request: Request) -> Response: pass async with Trace( - "receive_response_headers", logger, request, kwargs + "receive_response_headers", + logger, + request, + kwargs, ) as trace: ( http_version, @@ -157,7 +163,9 @@ async def _send_request_body(self, request: Request) -> None: await self._send_event(h11.EndOfMessage(), timeout=timeout) async def _send_event( - self, event: h11.Event, timeout: Optional[float] = None + self, + event: h11.Event, + timeout: Optional[float] = None, ) -> None: bytes_to_send = self._h11_state.send(event) if bytes_to_send is not None: @@ -209,7 +217,8 @@ async def _receive_event( if event is h11.NEED_DATA: data = await self._network_stream.read( - self.READ_NUM_BYTES, timeout=timeout + self.READ_NUM_BYTES, + timeout=timeout, ) # If we feed this case through h11 we'll raise an exception like: diff --git a/httpcore/_async/http2.py b/httpcore/_async/http2.py index 0002a76f..c503ffe7 100644 --- a/httpcore/_async/http2.py +++ b/httpcore/_async/http2.py @@ -143,7 +143,10 @@ async def handle_async_request(self, request: Request) -> Response: async with Trace("send_request_body", logger, request, kwargs): await self._send_request_body(request=request, stream_id=stream_id) async with Trace( - "receive_response_headers", logger, request, kwargs + "receive_response_headers", + logger, + request, + kwargs, ) as trace: status, headers = await self._receive_response( request=request, stream_id=stream_id @@ -261,7 +264,10 @@ async def _send_request_body(self, request: Request, stream_id: int) -> None: await self._send_end_stream(request, stream_id) async def _send_stream_data( - self, request: Request, stream_id: int, data: bytes + self, + request: Request, + stream_id: int, + data: bytes, ) -> None: """ Send a single chunk of data in one or more data frames. @@ -362,7 +368,9 @@ async def _receive_events( for event in events: if isinstance(event, h2.events.RemoteSettingsChanged): async with Trace( - "receive_remote_settings", logger, request + "receive_remote_settings", + logger, + request, ) as trace: await self._receive_remote_settings_change(event) trace.return_value = event @@ -426,7 +434,8 @@ async def aclose(self) -> None: # Wrappers around network read/write operations... async def _read_incoming_data( - self, request: Request + self, + request: Request, ) -> typing.List[h2.events.Event]: timeouts = request.extensions.get("timeout", {}) timeout = timeouts.get("read", None) diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index f4837ee5..9c98fb56 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -288,7 +288,7 @@ async def handle_async_request(self, request: Request) -> Response: extensions=request.extensions, ) connect_response = await self._connection.handle_async_request( - connect_request + connect_request, ) if connect_response.status < 200 or connect_response.status > 299: diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index 6ad5dd10..6c813f42 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -119,7 +119,12 @@ def _connect(self, request: Request) -> NetworkStream: "timeout": timeout, "socket_options": self._socket_options, } - with Trace("connect_tcp", logger, request, kwargs) as trace: + with Trace( + "connect_tcp", + logger, + request, + kwargs, + ) as trace: stream = self._network_backend.connect_tcp(**kwargs) trace.return_value = stream else: @@ -128,8 +133,15 @@ def _connect(self, request: Request) -> NetworkStream: "timeout": timeout, "socket_options": self._socket_options, } - with Trace("connect_unix_socket", logger, request, kwargs) as trace: - stream = self._network_backend.connect_unix_socket(**kwargs) + with Trace( + "connect_unix_socket", + logger, + request, + kwargs, + ) as trace: + stream = self._network_backend.connect_unix_socket( + **kwargs, + ) trace.return_value = stream if self._origin.scheme in (b"https", b"wss"): diff --git a/httpcore/_sync/connection_pool.py b/httpcore/_sync/connection_pool.py index 6ff30ff4..62dda925 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -25,7 +25,10 @@ def __init__(self, request: Request) -> None: self.connection: Optional[ConnectionInterface] = None self._connection_acquired = Event() - def assign_to_connection(self, connection: Optional[ConnectionInterface]) -> None: + def assign_to_connection( + self, + connection: Optional[ConnectionInterface], + ) -> None: self.connection = connection self._connection_acquired.set() @@ -202,7 +205,9 @@ def handle_request(self, request: Request) -> Response: try: # Send the request on the assigned connection. - response = connection.handle_request(pool_request.request) + response = connection.handle_request( + pool_request.request, + ) except ConnectionNotAvailable: # In some cases a connection may initially be available to # handle a request, but then become unavailable. diff --git a/httpcore/_sync/http11.py b/httpcore/_sync/http11.py index 60b1748a..cbbd945d 100644 --- a/httpcore/_sync/http11.py +++ b/httpcore/_sync/http11.py @@ -85,7 +85,12 @@ def handle_request(self, request: Request) -> Response: try: kwargs = {"request": request} try: - with Trace("send_request_headers", logger, request, kwargs) as trace: + with Trace( + "send_request_headers", + logger, + request, + kwargs, + ) as trace: self._send_request_headers(**kwargs) with Trace("send_request_body", logger, request, kwargs) as trace: self._send_request_body(**kwargs) @@ -97,7 +102,12 @@ def handle_request(self, request: Request) -> Response: # error response. pass - with Trace("receive_response_headers", logger, request, kwargs) as trace: + with Trace( + "receive_response_headers", + logger, + request, + kwargs, + ) as trace: ( http_version, status, @@ -152,7 +162,11 @@ def _send_request_body(self, request: Request) -> None: self._send_event(h11.EndOfMessage(), timeout=timeout) - def _send_event(self, event: h11.Event, timeout: Optional[float] = None) -> None: + def _send_event( + self, + event: h11.Event, + timeout: Optional[float] = None, + ) -> None: bytes_to_send = self._h11_state.send(event) if bytes_to_send is not None: self._network_stream.write(bytes_to_send, timeout=timeout) @@ -202,7 +216,10 @@ def _receive_event( event = self._h11_state.next_event() if event is h11.NEED_DATA: - data = self._network_stream.read(self.READ_NUM_BYTES, timeout=timeout) + data = self._network_stream.read( + self.READ_NUM_BYTES, + timeout=timeout, + ) # If we feed this case through h11 we'll raise an exception like: # diff --git a/httpcore/_sync/http2.py b/httpcore/_sync/http2.py index ae73c8e3..373b9985 100644 --- a/httpcore/_sync/http2.py +++ b/httpcore/_sync/http2.py @@ -142,7 +142,12 @@ def handle_request(self, request: Request) -> Response: self._send_request_headers(request=request, stream_id=stream_id) with Trace("send_request_body", logger, request, kwargs): self._send_request_body(request=request, stream_id=stream_id) - with Trace("receive_response_headers", logger, request, kwargs) as trace: + with Trace( + "receive_response_headers", + logger, + request, + kwargs, + ) as trace: status, headers = self._receive_response( request=request, stream_id=stream_id ) @@ -258,7 +263,12 @@ def _send_request_body(self, request: Request, stream_id: int) -> None: self._send_stream_data(request, stream_id, data) self._send_end_stream(request, stream_id) - def _send_stream_data(self, request: Request, stream_id: int, data: bytes) -> None: + def _send_stream_data( + self, + request: Request, + stream_id: int, + data: bytes, + ) -> None: """ Send a single chunk of data in one or more data frames. """ @@ -357,7 +367,11 @@ def _receive_events( events = self._read_incoming_data(request) for event in events: if isinstance(event, h2.events.RemoteSettingsChanged): - with Trace("receive_remote_settings", logger, request) as trace: + with Trace( + "receive_remote_settings", + logger, + request, + ) as trace: self._receive_remote_settings_change(event) trace.return_value = event @@ -419,7 +433,10 @@ def close(self) -> None: # Wrappers around network read/write operations... - def _read_incoming_data(self, request: Request) -> typing.List[h2.events.Event]: + def _read_incoming_data( + self, + request: Request, + ) -> typing.List[h2.events.Event]: timeouts = request.extensions.get("timeout", {}) timeout = timeouts.get("read", None) diff --git a/httpcore/_sync/http_proxy.py b/httpcore/_sync/http_proxy.py index 7a6a5198..ea31c9fa 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -287,7 +287,9 @@ def handle_request(self, request: Request) -> Response: headers=connect_headers, extensions=request.extensions, ) - connect_response = self._connection.handle_request(connect_request) + connect_response = self._connection.handle_request( + connect_request, + ) if connect_response.status < 200 or connect_response.status > 299: reason_bytes = connect_response.extensions.get("reason_phrase", b"")