From 32620a095c7e8f3dd2e1fbff2441b97e9ff9dcac Mon Sep 17 00:00:00 2001 From: Tester Date: Tue, 13 Feb 2024 17:30:27 +0330 Subject: [PATCH 1/8] Ruff linter: Use the default line-length --- httpcore/_async/connection.py | 3 +- httpcore/_async/connection_pool.py | 18 ++-- httpcore/_async/http2.py | 8 +- httpcore/_async/http_proxy.py | 3 +- httpcore/_backends/trio.py | 3 +- httpcore/_sync/connection.py | 3 +- httpcore/_sync/connection_pool.py | 18 ++-- httpcore/_sync/http2.py | 8 +- httpcore/_sync/http_proxy.py | 3 +- pyproject.toml | 21 ++-- tests/_async/test_connection.py | 12 +-- tests/_async/test_connection_pool.py | 138 ++++++++++++++++----------- tests/_async/test_http11.py | 30 +++--- tests/_async/test_http2.py | 6 +- tests/_async/test_http_proxy.py | 27 ++++-- tests/_async/test_socks_proxy.py | 21 ++-- tests/_sync/test_connection.py | 12 +-- tests/_sync/test_connection_pool.py | 138 ++++++++++++++++----------- tests/_sync/test_http11.py | 30 +++--- tests/_sync/test_http2.py | 6 +- tests/_sync/test_http_proxy.py | 27 ++++-- tests/_sync/test_socks_proxy.py | 21 ++-- unasync.py | 9 +- 23 files changed, 335 insertions(+), 230 deletions(-) diff --git a/httpcore/_async/connection.py b/httpcore/_async/connection.py index 2f439cf0..8c2355c8 100644 --- a/httpcore/_async/connection.py +++ b/httpcore/_async/connection.py @@ -67,7 +67,8 @@ def __init__( async def handle_async_request(self, request: Request) -> Response: if not self.can_handle_request(request.url.origin): raise RuntimeError( - f"Attempted to send request to {request.url.origin} on connection to {self._origin}" + f"Attempted to send request to {request.url.origin}" + f" on connection to {self._origin}" ) try: diff --git a/httpcore/_async/connection_pool.py b/httpcore/_async/connection_pool.py index 018b0ba2..e993afea 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -119,9 +119,10 @@ def __init__( self._connections: List[AsyncConnectionInterface] = [] self._requests: List[AsyncPoolRequest] = [] - # We only mutate the state of the connection pool within an 'optional_thread_lock' - # context. This holds a threading lock unless we're running in async mode, - # in which case it is a no-op. + # We only mutate the state of the connection pool + # within an 'optional_thread_lock' context. + # This holds a threading lock unless we're running in + # async mode, in which case it is a no-op. self._optional_thread_lock = AsyncThreadLock() def create_connection(self, origin: Origin) -> AsyncConnectionInterface: @@ -148,9 +149,12 @@ def connections(self) -> List[AsyncConnectionInterface]: ```python >>> pool.connections [ - , - , - , + , + , + , ] ``` """ @@ -160,7 +164,7 @@ async def handle_async_request(self, request: Request) -> Response: """ Send an HTTP request, and return an HTTP response. - This is the core implementation that is called into by `.request()` or `.stream()`. + The core implementation that is called into by `.request()` or `.stream()`. """ scheme = request.url.scheme.decode() if scheme == "": diff --git a/httpcore/_async/http2.py b/httpcore/_async/http2.py index 8dc776ff..0002a76f 100644 --- a/httpcore/_async/http2.py +++ b/httpcore/_async/http2.py @@ -470,10 +470,10 @@ async def _write_outgoing_data(self, request: Request) -> None: except Exception as exc: # pragma: nocover # If we get a network error we should: # - # 1. Save the exception and just raise it immediately on any future write. - # (For example, this means that a single write timeout or disconnect will - # immediately close all pending streams. Without requiring multiple - # sequential timeouts.) + # 1. Save the exception and just raise it immediately on any + # future write. (For example, this means that a single write timeout + # or disconnect will immediately close all pending streams, + # without requiring multiple sequential timeouts.) # 2. Mark the connection as errored, so that we don't accept any other # incoming requests. self._write_exception = exc diff --git a/httpcore/_async/http_proxy.py b/httpcore/_async/http_proxy.py index 4aa7d874..f4837ee5 100644 --- a/httpcore/_async/http_proxy.py +++ b/httpcore/_async/http_proxy.py @@ -89,7 +89,8 @@ def __init__( ssl_context: An SSL context to use for verifying connections. If not specified, the default `httpcore.default_ssl_context()` will be used. - proxy_ssl_context: The same as `ssl_context`, but for a proxy server rather than a remote origin. + proxy_ssl_context: The same as `ssl_context`, but for a proxy server + rather than a remote origin. max_connections: The maximum number of concurrent HTTP connections that the pool should allow. Any attempt to send a request on a pool that would exceed this amount will block until a connection is available. diff --git a/httpcore/_backends/trio.py b/httpcore/_backends/trio.py index b1626d28..4cecef6e 100644 --- a/httpcore/_backends/trio.py +++ b/httpcore/_backends/trio.py @@ -82,7 +82,8 @@ async def start_tls( def get_extra_info(self, info: str) -> typing.Any: if info == "ssl_object" and isinstance(self._stream, trio.SSLStream): - # Type checkers cannot see `_ssl_object` attribute because trio._ssl.SSLStream uses __getattr__/__setattr__. + # Type checkers cannot see `_ssl_object` attribute + # because `trio._ssl.SSLStream` uses `__getattr__`/`__setattr__`. # Tracked at https://github.com/python-trio/trio/issues/542 return self._stream._ssl_object # type: ignore[attr-defined] if info == "client_addr": diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index c3890f34..399bba9c 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -67,7 +67,8 @@ def __init__( def handle_request(self, request: Request) -> Response: if not self.can_handle_request(request.url.origin): raise RuntimeError( - f"Attempted to send request to {request.url.origin} on connection to {self._origin}" + f"Attempted to send request to {request.url.origin}" + f" on connection to {self._origin}" ) try: diff --git a/httpcore/_sync/connection_pool.py b/httpcore/_sync/connection_pool.py index 8dcf348c..5894261a 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -119,9 +119,10 @@ def __init__( self._connections: List[ConnectionInterface] = [] self._requests: List[PoolRequest] = [] - # We only mutate the state of the connection pool within an 'optional_thread_lock' - # context. This holds a threading lock unless we're running in async mode, - # in which case it is a no-op. + # We only mutate the state of the connection pool + # within an 'optional_thread_lock' context. + # This holds a threading lock unless we're running in + # async mode, in which case it is a no-op. self._optional_thread_lock = ThreadLock() def create_connection(self, origin: Origin) -> ConnectionInterface: @@ -148,9 +149,12 @@ def connections(self) -> List[ConnectionInterface]: ```python >>> pool.connections [ - , - , - , + , + , + , ] ``` """ @@ -160,7 +164,7 @@ def handle_request(self, request: Request) -> Response: """ Send an HTTP request, and return an HTTP response. - This is the core implementation that is called into by `.request()` or `.stream()`. + The core implementation that is called into by `.request()` or `.stream()`. """ scheme = request.url.scheme.decode() if scheme == "": diff --git a/httpcore/_sync/http2.py b/httpcore/_sync/http2.py index d141d459..abd026e1 100644 --- a/httpcore/_sync/http2.py +++ b/httpcore/_sync/http2.py @@ -470,10 +470,10 @@ def _write_outgoing_data(self, request: Request) -> None: except Exception as exc: # pragma: nocover # If we get a network error we should: # - # 1. Save the exception and just raise it immediately on any future write. - # (For example, this means that a single write timeout or disconnect will - # immediately close all pending streams. Without requiring multiple - # sequential timeouts.) + # 1. Save the exception and just raise it immediately on any + # future write. (For example, this means that a single write timeout + # or disconnect will immediately close all pending streams, + # without requiring multiple sequential timeouts.) # 2. Mark the connection as errored, so that we don't accept any other # incoming requests. self._write_exception = exc diff --git a/httpcore/_sync/http_proxy.py b/httpcore/_sync/http_proxy.py index 6acac9a7..912d6099 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -89,7 +89,8 @@ def __init__( ssl_context: An SSL context to use for verifying connections. If not specified, the default `httpcore.default_ssl_context()` will be used. - proxy_ssl_context: The same as `ssl_context`, but for a proxy server rather than a remote origin. + proxy_ssl_context: The same as `ssl_context`, but for a proxy server + rather than a remote origin. max_connections: The maximum number of concurrent HTTP connections that the pool should allow. Any attempt to send a request on a pool that would exceed this amount will block until a connection is available. diff --git a/pyproject.toml b/pyproject.toml index a35ad6b3..9bf77ea4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,7 @@ include = [ "/httpcore", "/CHANGELOG.md", "/README.md", - "/tests" + "/tests", ] [tool.hatch.metadata.hooks.fancy-pypi-readme] @@ -96,26 +96,27 @@ filterwarnings = ["error"] [tool.coverage.run] omit = [ - "venv/*", - "httpcore/_sync/*" + "venv/*", + "httpcore/_sync/*", +] +include = [ + "httpcore/*", + "tests/*", ] -include = ["httpcore/*", "tests/*"] [tool.ruff] exclude = [ "httpcore/_sync", "tests/_sync", ] -line-length = 88 + +[tool.ruff.lint] select = [ "E", "F", "W", - "I" + "I", ] -[tool.ruff.pycodestyle] -max-line-length = 120 - -[tool.ruff.isort] +[tool.ruff.lint.isort] combine-as-imports = true diff --git a/tests/_async/test_connection.py b/tests/_async/test_connection.py index b6ee0c7e..b5093c28 100644 --- a/tests/_async/test_connection.py +++ b/tests/_async/test_connection.py @@ -42,9 +42,9 @@ async def test_http_connection(): assert repr(conn) == "" async with conn.stream("GET", "https://example.com/") as response: - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) await response.aread() @@ -55,9 +55,9 @@ async def test_http_connection(): assert not conn.is_closed() assert conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) diff --git a/tests/_async/test_connection_pool.py b/tests/_async/test_connection_pool.py index 2fc27204..192cd769 100644 --- a/tests/_async/test_connection_pool.py +++ b/tests/_async/test_connection_pool.py @@ -36,11 +36,12 @@ async def test_connection_pool_with_keepalive(): async with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) await response.aread() @@ -48,22 +49,25 @@ async def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - # Sending a second request to the same origin will reuse the existing IDLE connection. + # Sending a second request to the same origin will + # reuse the existing IDLE connection. async with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) await response.aread() @@ -71,23 +75,27 @@ async def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - # Sending a request to a different origin will not reuse the existing IDLE connection. + # Sending a request to a different origin will not + # reuse the existing IDLE connection. async with pool.stream("GET", "http://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "", - "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) await response.aread() @@ -95,12 +103,14 @@ async def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "", - "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) @@ -127,7 +137,8 @@ async def test_connection_pool_with_close(): ) as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] await response.aread() @@ -185,17 +196,20 @@ async def test_connection_pool_with_http2(): info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - # Sending a second request to the same origin will reuse the existing IDLE connection. + # Sending a second request to the same origin will + # reuse the existing IDLE connection. response = await pool.request("GET", "https://example.com/") assert response.status == 200 assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] @@ -239,7 +253,8 @@ async def test_connection_pool_with_http2_goaway(): info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] # Sending a second request to the same origin will require a new connection. @@ -250,7 +265,8 @@ async def test_connection_pool_with_http2_goaway(): info = [repr(c) for c in pool.connections] assert info == [ - "", + "", ] @@ -321,7 +337,8 @@ async def test_debug_request(caplog): ( "httpcore.connection", logging.DEBUG, - "connect_tcp.started host='example.com' port=80 local_address=None timeout=None socket_options=None", + "connect_tcp.started host='example.com' port=80 local_address=None" + " timeout=None socket_options=None", ), ( "httpcore.connection", @@ -349,7 +366,8 @@ async def test_debug_request(caplog): "httpcore.http11", logging.DEBUG, "receive_response_headers.complete return_value=" - "(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'plain/text'), (b'Content-Length', b'13')])", + "(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'plain/text')," + " (b'Content-Length', b'13')])", ), ( "httpcore.http11", @@ -470,7 +488,8 @@ async def test_connection_pool_with_immediate_expiry(): async with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] await response.aread() @@ -503,7 +522,8 @@ async def test_connection_pool_with_no_keepalive_connections_allowed(): async with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] await response.aread() @@ -550,11 +570,16 @@ async def fetch(pool, domain, info_list): # Each connection was to a different host, and only sent a single # request on that connection. assert item[0] in [ - "", - "", - "", - "", - "", + "", + "", + "", + "", + "", ] @@ -594,9 +619,9 @@ async def fetch(pool, domain, info_list): # single connection was established at any one time. assert len(item) == 1 # Only a single request was sent on each connection. - assert ( - item[0] - == "" + assert item[0] == ( + "" ) @@ -637,16 +662,21 @@ async def fetch(pool, domain, info_list): assert len(item) == 1 # The connection sent multiple requests. assert item[0] in [ - "", - "", - "", - "", - "", + "", + "", + "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) diff --git a/tests/_async/test_http11.py b/tests/_async/test_http11.py index 489d68b4..db34011a 100644 --- a/tests/_async/test_http11.py +++ b/tests/_async/test_http11.py @@ -26,9 +26,9 @@ async def test_http11_connection(): assert not conn.is_closed() assert conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -56,9 +56,9 @@ async def test_http11_connection_unread_response(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -78,9 +78,9 @@ async def test_http11_connection_with_remote_protocol_error(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -107,9 +107,9 @@ async def test_http11_connection_with_incomplete_response(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -139,9 +139,9 @@ async def test_http11_connection_with_local_protocol_error(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) diff --git a/tests/_async/test_http2.py b/tests/_async/test_http2.py index b4ec6648..fe1c1516 100644 --- a/tests/_async/test_http2.py +++ b/tests/_async/test_http2.py @@ -40,9 +40,9 @@ async def test_http2_connection(): assert ( conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) diff --git a/tests/_async/test_http_proxy.py b/tests/_async/test_http_proxy.py index b35fc289..5a4475d2 100644 --- a/tests/_async/test_http_proxy.py +++ b/tests/_async/test_http_proxy.py @@ -40,7 +40,8 @@ async def test_proxy_forwarding(): async with proxy.stream("GET", "http://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -48,13 +49,15 @@ async def test_proxy_forwarding(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a forwarding proxy can only handle HTTP requests to the same origin. + # A connection on a forwarding proxy can only + # handle HTTP requests to the same origin. assert proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -95,7 +98,8 @@ async def test_proxy_tunneling(): async with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -103,13 +107,15 @@ async def test_proxy_tunneling(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -187,7 +193,8 @@ async def test_proxy_tunneling_http2(): async with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -195,13 +202,15 @@ async def test_proxy_tunneling_http2(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) diff --git a/tests/_async/test_socks_proxy.py b/tests/_async/test_socks_proxy.py index 3f5dd1cc..c70c8c61 100644 --- a/tests/_async/test_socks_proxy.py +++ b/tests/_async/test_socks_proxy.py @@ -32,7 +32,8 @@ async def test_socks5_request(): async with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -40,13 +41,15 @@ async def test_socks5_request(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( httpcore.Origin(b"http", b"example.com", 80) ) @@ -93,7 +96,8 @@ async def test_authenticated_socks5_request(): async with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] await response.aread() @@ -101,7 +105,8 @@ async def test_authenticated_socks5_request(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() @@ -157,9 +162,9 @@ async def test_socks5_request_failed_to_provide_auth(): # Sending a request, which the proxy rejects with pytest.raises(httpcore.ProxyError) as exc_info: await proxy.request("GET", "https://example.com/") - assert ( - str(exc_info.value) - == "Requested NO AUTHENTICATION REQUIRED from proxy server, but got USERNAME/PASSWORD." + assert str(exc_info.value) == ( + "Requested NO AUTHENTICATION REQUIRED from proxy server," + " but got USERNAME/PASSWORD." ) assert not proxy.connections diff --git a/tests/_sync/test_connection.py b/tests/_sync/test_connection.py index 37c82e02..dca76cbf 100644 --- a/tests/_sync/test_connection.py +++ b/tests/_sync/test_connection.py @@ -42,9 +42,9 @@ def test_http_connection(): assert repr(conn) == "" with conn.stream("GET", "https://example.com/") as response: - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) response.read() @@ -55,9 +55,9 @@ def test_http_connection(): assert not conn.is_closed() assert conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) diff --git a/tests/_sync/test_connection_pool.py b/tests/_sync/test_connection_pool.py index ee303e5c..45a8133b 100644 --- a/tests/_sync/test_connection_pool.py +++ b/tests/_sync/test_connection_pool.py @@ -36,11 +36,12 @@ def test_connection_pool_with_keepalive(): with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) response.read() @@ -48,22 +49,25 @@ def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - # Sending a second request to the same origin will reuse the existing IDLE connection. + # Sending a second request to the same origin will + # reuse the existing IDLE connection. with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) response.read() @@ -71,23 +75,27 @@ def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) - # Sending a request to a different origin will not reuse the existing IDLE connection. + # Sending a request to a different origin will not + # reuse the existing IDLE connection. with pool.stream("GET", "http://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "", - "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) response.read() @@ -95,12 +103,14 @@ def test_connection_pool_with_keepalive(): assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "", - "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) @@ -127,7 +137,8 @@ def test_connection_pool_with_close(): ) as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] response.read() @@ -185,17 +196,20 @@ def test_connection_pool_with_http2(): info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] - # Sending a second request to the same origin will reuse the existing IDLE connection. + # Sending a second request to the same origin will + # reuse the existing IDLE connection. response = pool.request("GET", "https://example.com/") assert response.status == 200 assert response.content == b"Hello, world!" info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] @@ -239,7 +253,8 @@ def test_connection_pool_with_http2_goaway(): info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] # Sending a second request to the same origin will require a new connection. @@ -250,7 +265,8 @@ def test_connection_pool_with_http2_goaway(): info = [repr(c) for c in pool.connections] assert info == [ - "", + "", ] @@ -321,7 +337,8 @@ def test_debug_request(caplog): ( "httpcore.connection", logging.DEBUG, - "connect_tcp.started host='example.com' port=80 local_address=None timeout=None socket_options=None", + "connect_tcp.started host='example.com' port=80 local_address=None" + " timeout=None socket_options=None", ), ( "httpcore.connection", @@ -349,7 +366,8 @@ def test_debug_request(caplog): "httpcore.http11", logging.DEBUG, "receive_response_headers.complete return_value=" - "(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'plain/text'), (b'Content-Length', b'13')])", + "(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'plain/text')," + " (b'Content-Length', b'13')])", ), ( "httpcore.http11", @@ -470,7 +488,8 @@ def test_connection_pool_with_immediate_expiry(): with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] response.read() @@ -503,7 +522,8 @@ def test_connection_pool_with_no_keepalive_connections_allowed(): with pool.stream("GET", "https://example.com/") as response: info = [repr(c) for c in pool.connections] assert info == [ - "" + "" ] response.read() @@ -550,11 +570,16 @@ def fetch(pool, domain, info_list): # Each connection was to a different host, and only sent a single # request on that connection. assert item[0] in [ - "", - "", - "", - "", - "", + "", + "", + "", + "", + "", ] @@ -594,9 +619,9 @@ def fetch(pool, domain, info_list): # single connection was established at any one time. assert len(item) == 1 # Only a single request was sent on each connection. - assert ( - item[0] - == "" + assert item[0] == ( + "" ) @@ -637,16 +662,21 @@ def fetch(pool, domain, info_list): assert len(item) == 1 # The connection sent multiple requests. assert item[0] in [ - "", - "", - "", - "", - "", + "", + "", + "", + "", + "", ] - assert ( - repr(pool) - == "" + assert repr(pool) == ( + "" ) diff --git a/tests/_sync/test_http11.py b/tests/_sync/test_http11.py index dcd80e84..67b4748c 100644 --- a/tests/_sync/test_http11.py +++ b/tests/_sync/test_http11.py @@ -26,9 +26,9 @@ def test_http11_connection(): assert not conn.is_closed() assert conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -56,9 +56,9 @@ def test_http11_connection_unread_response(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -78,9 +78,9 @@ def test_http11_connection_with_remote_protocol_error(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -107,9 +107,9 @@ def test_http11_connection_with_incomplete_response(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) @@ -139,9 +139,9 @@ def test_http11_connection_with_local_protocol_error(): assert conn.is_closed() assert not conn.is_available() assert not conn.has_expired() - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) diff --git a/tests/_sync/test_http2.py b/tests/_sync/test_http2.py index 695359bd..c7e5ef65 100644 --- a/tests/_sync/test_http2.py +++ b/tests/_sync/test_http2.py @@ -40,9 +40,9 @@ def test_http2_connection(): assert ( conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) - assert ( - repr(conn) - == "" + assert repr(conn) == ( + "" ) diff --git a/tests/_sync/test_http_proxy.py b/tests/_sync/test_http_proxy.py index 2d66578e..8ccd9938 100644 --- a/tests/_sync/test_http_proxy.py +++ b/tests/_sync/test_http_proxy.py @@ -40,7 +40,8 @@ def test_proxy_forwarding(): with proxy.stream("GET", "http://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -48,13 +49,15 @@ def test_proxy_forwarding(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a forwarding proxy can only handle HTTP requests to the same origin. + # A connection on a forwarding proxy can only + # handle HTTP requests to the same origin. assert proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -95,7 +98,8 @@ def test_proxy_tunneling(): with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -103,13 +107,15 @@ def test_proxy_tunneling(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) @@ -187,7 +193,8 @@ def test_proxy_tunneling_http2(): with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -195,13 +202,15 @@ def test_proxy_tunneling_http2(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( Origin(b"http", b"example.com", 80) ) diff --git a/tests/_sync/test_socks_proxy.py b/tests/_sync/test_socks_proxy.py index 2d39bb97..c61c9037 100644 --- a/tests/_sync/test_socks_proxy.py +++ b/tests/_sync/test_socks_proxy.py @@ -32,7 +32,8 @@ def test_socks5_request(): with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -40,13 +41,15 @@ def test_socks5_request(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() assert not proxy.connections[0].is_closed() - # A connection on a tunneled proxy can only handle HTTPS requests to the same origin. + # A connection on a tunneled proxy can only + # handle HTTPS requests to the same origin. assert not proxy.connections[0].can_handle_request( httpcore.Origin(b"http", b"example.com", 80) ) @@ -93,7 +96,8 @@ def test_authenticated_socks5_request(): with proxy.stream("GET", "https://example.com/") as response: info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] response.read() @@ -101,7 +105,8 @@ def test_authenticated_socks5_request(): assert response.content == b"Hello, world!" info = [repr(c) for c in proxy.connections] assert info == [ - "" + "" ] assert proxy.connections[0].is_idle() assert proxy.connections[0].is_available() @@ -157,9 +162,9 @@ def test_socks5_request_failed_to_provide_auth(): # Sending a request, which the proxy rejects with pytest.raises(httpcore.ProxyError) as exc_info: proxy.request("GET", "https://example.com/") - assert ( - str(exc_info.value) - == "Requested NO AUTHENTICATION REQUIRED from proxy server, but got USERNAME/PASSWORD." + assert str(exc_info.value) == ( + "Requested NO AUTHENTICATION REQUIRED from proxy server," + " but got USERNAME/PASSWORD." ) assert not proxy.connections diff --git a/unasync.py b/unasync.py index 5a5627d7..b8dee7bc 100755 --- a/unasync.py +++ b/unasync.py @@ -5,7 +5,10 @@ from pprint import pprint SUBS = [ - ('from .._backends.auto import AutoBackend', 'from .._backends.sync import SyncBackend'), + ( + 'from .._backends.auto import AutoBackend', + 'from .._backends.sync import SyncBackend', + ), ('import trio as concurrency', 'from tests import concurrency'), ('AsyncIterator', 'Iterator'), ('Async([A-Z][A-Za-z0-9_]*)', r'\2'), @@ -87,8 +90,8 @@ def main(): print("These patterns were not used:") pprint(unused_subs) - exit(1) - + exit(1) + if __name__ == '__main__': main() From 2e95f26a2b787063549f5ad6042099cf1ac7b3de Mon Sep 17 00:00:00 2001 From: Tester Date: Tue, 13 Feb 2024 18:48:41 +0330 Subject: [PATCH 2/8] `per-file-ignores` instead of exclude. --- httpcore/_async/connection_pool.py | 9 ++++++++- httpcore/_sync/connection_pool.py | 9 ++++++++- pyproject.toml | 11 +++++------ tests/_async/test_integration.py | 2 +- tests/_sync/test_integration.py | 2 +- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/httpcore/_async/connection_pool.py b/httpcore/_async/connection_pool.py index e993afea..5d701bd5 100644 --- a/httpcore/_async/connection_pool.py +++ b/httpcore/_async/connection_pool.py @@ -1,7 +1,14 @@ import ssl import sys from types import TracebackType -from typing import AsyncIterable, AsyncIterator, Iterable, List, Optional, Type +from typing import ( # noqa: F811 + AsyncIterable, + AsyncIterator, + Iterable, + List, + Optional, + Type, +) from .._backends.auto import AutoBackend from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend diff --git a/httpcore/_sync/connection_pool.py b/httpcore/_sync/connection_pool.py index 5894261a..a9eddf99 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -1,7 +1,14 @@ import ssl import sys from types import TracebackType -from typing import Iterable, Iterator, Iterable, List, Optional, Type +from typing import ( # noqa: F811 + Iterable, + Iterator, + Iterable, + List, + Optional, + Type, +) from .._backends.sync import SyncBackend from .._backends.base import SOCKET_OPTION, NetworkBackend diff --git a/pyproject.toml b/pyproject.toml index 9bf77ea4..e5c20dad 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -104,12 +104,6 @@ include = [ "tests/*", ] -[tool.ruff] -exclude = [ - "httpcore/_sync", - "tests/_sync", -] - [tool.ruff.lint] select = [ "E", @@ -120,3 +114,8 @@ select = [ [tool.ruff.lint.isort] combine-as-imports = true + +[tool.ruff.lint.per-file-ignores] +# needed for unasync.py +"httpcore/_sync/*" = ["I001"] +"tests/_sync/*" = ["I001"] diff --git a/tests/_async/test_integration.py b/tests/_async/test_integration.py index 1970531d..afa5dd9e 100644 --- a/tests/_async/test_integration.py +++ b/tests/_async/test_integration.py @@ -1,6 +1,6 @@ import ssl -import pytest +import pytest # noqa: F401 import httpcore diff --git a/tests/_sync/test_integration.py b/tests/_sync/test_integration.py index e3327e69..07254f66 100644 --- a/tests/_sync/test_integration.py +++ b/tests/_sync/test_integration.py @@ -1,6 +1,6 @@ import ssl -import pytest +import pytest # noqa: F401 import httpcore From 25ef477721fdd2384fe111bd22a1b11de74d57a0 Mon Sep 17 00:00:00 2001 From: Tester Date: Tue, 13 Feb 2024 18:53:43 +0330 Subject: [PATCH 3/8] ruff format --- httpcore/_sync/connection.py | 8 +--- httpcore/_sync/connection_pool.py | 8 +--- httpcore/_sync/http11.py | 16 ++------ httpcore/_sync/http2.py | 16 ++------ httpcore/_sync/http_proxy.py | 4 +- tests/_sync/test_connection.py | 33 +++------------- tests/_sync/test_connection_pool.py | 34 ++-------------- tests/_sync/test_http11.py | 15 +------- tests/_sync/test_http2.py | 13 +------ tests/_sync/test_http_proxy.py | 5 --- tests/_sync/test_integration.py | 3 -- tests/_sync/test_socks_proxy.py | 5 --- unasync.py | 60 ++++++++++++++--------------- 13 files changed, 55 insertions(+), 165 deletions(-) diff --git a/httpcore/_sync/connection.py b/httpcore/_sync/connection.py index 399bba9c..6ad5dd10 100644 --- a/httpcore/_sync/connection.py +++ b/httpcore/_sync/connection.py @@ -128,12 +128,8 @@ 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 a9eddf99..6ff30ff4 100644 --- a/httpcore/_sync/connection_pool.py +++ b/httpcore/_sync/connection_pool.py @@ -25,9 +25,7 @@ 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() @@ -204,9 +202,7 @@ 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 e108f88b..60b1748a 100644 --- a/httpcore/_sync/http11.py +++ b/httpcore/_sync/http11.py @@ -85,9 +85,7 @@ 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) @@ -99,9 +97,7 @@ 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, @@ -156,9 +152,7 @@ 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) @@ -208,9 +202,7 @@ 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 abd026e1..ae73c8e3 100644 --- a/httpcore/_sync/http2.py +++ b/httpcore/_sync/http2.py @@ -142,9 +142,7 @@ 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 ) @@ -260,9 +258,7 @@ 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. """ @@ -361,9 +357,7 @@ 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 @@ -425,9 +419,7 @@ 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 912d6099..7a6a5198 100644 --- a/httpcore/_sync/http_proxy.py +++ b/httpcore/_sync/http_proxy.py @@ -287,9 +287,7 @@ 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"") diff --git a/tests/_sync/test_connection.py b/tests/_sync/test_connection.py index dca76cbf..c84a882b 100644 --- a/tests/_sync/test_connection.py +++ b/tests/_sync/test_connection.py @@ -19,7 +19,6 @@ ) - def test_http_connection(): origin = Origin(b"https", b"example.com", 443) network_backend = MockBackend( @@ -61,7 +60,6 @@ def test_http_connection(): ) - def test_concurrent_requests_not_available_on_http11_connections(): """ Attempting to issue a request against an already active HTTP/1.1 connection @@ -87,7 +85,6 @@ def test_concurrent_requests_not_available_on_http11_connections(): @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") - def test_write_error_with_response_sent(): """ If a server half-closes the connection while the client is sending @@ -102,9 +99,7 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: super().__init__(buffer, http2) self.count = 0 - def write( - self, buffer: bytes, timeout: typing.Optional[float] = None - ) -> None: + def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: self.count += len(buffer) if self.count > 1_000_000: @@ -141,7 +136,6 @@ def connect_tcp( assert response.content == b"Request body exceeded 1,000,000 bytes" - @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") def test_write_error_without_response_sent(): """ @@ -156,9 +150,7 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: super().__init__(buffer, http2) self.count = 0 - def write( - self, buffer: bytes, timeout: typing.Optional[float] = None - ) -> None: + def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: self.count += len(buffer) if self.count > 1_000_000: @@ -187,7 +179,6 @@ def connect_tcp( assert str(exc_info.value) == "Server disconnected without sending a response." - @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") def test_http2_connection(): origin = Origin(b"https", b"example.com", 443) @@ -221,16 +212,13 @@ def test_http2_connection(): assert response.extensions["http_version"] == b"HTTP/2" - def test_request_to_incorrect_origin(): """ A connection can only send requests whichever origin it is connected to. """ origin = Origin(b"https", b"example.com", 443) network_backend = MockBackend([]) - with HTTPConnection( - origin=origin, network_backend=network_backend - ) as conn: + with HTTPConnection(origin=origin, network_backend=network_backend) as conn: with pytest.raises(RuntimeError): conn.request("GET", "https://other.com/") @@ -265,20 +253,14 @@ def connect_tcp( return self._NeedsRetryAsyncNetworkStream(self, stream) class _NeedsRetryAsyncNetworkStream(NetworkStream): - def __init__( - self, backend: "NeedsRetryBackend", stream: NetworkStream - ) -> None: + def __init__(self, backend: "NeedsRetryBackend", stream: NetworkStream) -> None: self._backend = backend self._stream = stream - def read( - self, max_bytes: int, timeout: typing.Optional[float] = None - ) -> bytes: + def read(self, max_bytes: int, timeout: typing.Optional[float] = None) -> bytes: return self._stream.read(max_bytes, timeout) - def write( - self, buffer: bytes, timeout: typing.Optional[float] = None - ) -> None: + def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: self._stream.write(buffer, timeout) def close(self) -> None: @@ -301,7 +283,6 @@ def get_extra_info(self, info: str) -> typing.Any: return self._stream.get_extra_info(info) - def test_connection_retries(): origin = Origin(b"https", b"example.com", 443) content = [ @@ -328,7 +309,6 @@ def test_connection_retries(): conn.request("GET", "https://example.com/") - def test_connection_retries_tls(): origin = Origin(b"https", b"example.com", 443) content = [ @@ -359,7 +339,6 @@ def test_connection_retries_tls(): conn.request("GET", "https://example.com/") - def test_uds_connections(): # We're not actually testing Unix Domain Sockets here, because we're just # using a mock backend, but at least we're covering the UDS codepath diff --git a/tests/_sync/test_connection_pool.py b/tests/_sync/test_connection_pool.py index 45a8133b..1a296ac1 100644 --- a/tests/_sync/test_connection_pool.py +++ b/tests/_sync/test_connection_pool.py @@ -9,7 +9,6 @@ import httpcore - def test_connection_pool_with_keepalive(): """ By default HTTP/1.1 requests should be returned to the connection pool. @@ -114,7 +113,6 @@ def test_connection_pool_with_keepalive(): ) - def test_connection_pool_with_close(): """ HTTP/1.1 requests that include a 'Connection: Close' header should @@ -148,7 +146,6 @@ def test_connection_pool_with_close(): assert info == [] - def test_connection_pool_with_http2(): """ Test a connection pool with HTTP/2 requests. @@ -213,7 +210,6 @@ def test_connection_pool_with_http2(): ] - def test_connection_pool_with_http2_goaway(): """ Test a connection pool with HTTP/2 requests, that cleanly disconnects @@ -270,7 +266,6 @@ def test_connection_pool_with_http2_goaway(): ] - def test_trace_request(): """ The 'trace' request extension allows for a callback function to inspect the @@ -312,7 +307,6 @@ def trace(name, kwargs): ] - def test_debug_request(caplog): """ The 'trace' request extension allows for a callback function to inspect the @@ -382,7 +376,6 @@ def test_debug_request(caplog): ] - def test_connection_pool_with_http_exception(): """ HTTP/1.1 requests that result in an exception during the connection should @@ -398,9 +391,7 @@ def trace(name, kwargs): with httpcore.ConnectionPool(network_backend=network_backend) as pool: # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): - pool.request( - "GET", "https://example.com/", extensions={"trace": trace} - ) + pool.request("GET", "https://example.com/", extensions={"trace": trace}) info = [repr(c) for c in pool.connections] assert info == [] @@ -421,7 +412,6 @@ def trace(name, kwargs): ] - def test_connection_pool_with_connect_exception(): """ HTTP/1.1 requests that result in an exception during connection should not @@ -451,9 +441,7 @@ def trace(name, kwargs): with httpcore.ConnectionPool(network_backend=network_backend) as pool: # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): - pool.request( - "GET", "https://example.com/", extensions={"trace": trace} - ) + pool.request("GET", "https://example.com/", extensions={"trace": trace}) info = [repr(c) for c in pool.connections] assert info == [] @@ -464,7 +452,6 @@ def trace(name, kwargs): ] - def test_connection_pool_with_immediate_expiry(): """ Connection pools with keepalive_expiry=0.0 should immediately expire @@ -499,7 +486,6 @@ def test_connection_pool_with_immediate_expiry(): assert info == [] - def test_connection_pool_with_no_keepalive_connections_allowed(): """ When 'max_keepalive_connections=0' is used, IDLE connections should not @@ -533,7 +519,6 @@ def test_connection_pool_with_no_keepalive_connections_allowed(): assert info == [] - def test_connection_pool_concurrency(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -583,7 +568,6 @@ def fetch(pool, domain, info_list): ] - def test_connection_pool_concurrency_same_domain_closing(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -625,7 +609,6 @@ def fetch(pool, domain, info_list): ) - def test_connection_pool_concurrency_same_domain_keepalive(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -680,7 +663,6 @@ def fetch(pool, domain, info_list): ) - def test_unsupported_protocol(): with httpcore.ConnectionPool() as pool: with pytest.raises(httpcore.UnsupportedProtocol): @@ -690,7 +672,6 @@ def test_unsupported_protocol(): pool.request("GET", "://www.example.com/") - def test_connection_pool_closed_while_request_in_flight(): """ Closing a connection pool while a request/response is still in-flight @@ -717,7 +698,6 @@ def test_connection_pool_closed_while_request_in_flight(): response.read() - def test_connection_pool_timeout(): """ Ensure that exceeding max_connections can cause a request to timeout. @@ -744,7 +724,6 @@ def test_connection_pool_timeout(): pool.request("GET", "https://example.com/", extensions=extensions) - def test_connection_pool_timeout_zero(): """ A pool timeout of 0 shouldn't raise a PoolTimeout if there's @@ -774,15 +753,11 @@ def test_connection_pool_timeout_zero(): ) as pool: # Two consecutive requests with a pool timeout of zero. # Both succeed without raising a timeout. - response = pool.request( - "GET", "https://example.com/", extensions=extensions - ) + response = pool.request("GET", "https://example.com/", extensions=extensions) assert response.status == 200 assert response.content == b"Hello, world!" - response = pool.request( - "GET", "https://example.com/", extensions=extensions - ) + response = pool.request("GET", "https://example.com/", extensions=extensions) assert response.status == 200 assert response.content == b"Hello, world!" @@ -806,7 +781,6 @@ def test_connection_pool_timeout_zero(): assert response.content == b"Hello, world!" - def test_http11_upgrade_connection(): """ HTTP "101 Switching Protocols" indicates an upgraded connection. diff --git a/tests/_sync/test_http11.py b/tests/_sync/test_http11.py index 67b4748c..c6486b80 100644 --- a/tests/_sync/test_http11.py +++ b/tests/_sync/test_http11.py @@ -3,7 +3,6 @@ import httpcore - def test_http11_connection(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -27,12 +26,10 @@ def test_http11_connection(): assert conn.is_available() assert not conn.has_expired() assert repr(conn) == ( - "" + "" ) - def test_http11_connection_unread_response(): """ If the client releases the response without reading it to termination, @@ -62,7 +59,6 @@ def test_http11_connection_unread_response(): ) - def test_http11_connection_with_remote_protocol_error(): """ If a remote protocol error occurs, then no response will be returned, @@ -84,7 +80,6 @@ def test_http11_connection_with_remote_protocol_error(): ) - def test_http11_connection_with_incomplete_response(): """ We should be gracefully handling the case where the connection ends prematurely. @@ -113,7 +108,6 @@ def test_http11_connection_with_incomplete_response(): ) - def test_http11_connection_with_local_protocol_error(): """ If a local protocol error occurs, then no response will be returned, @@ -145,7 +139,6 @@ def test_http11_connection_with_local_protocol_error(): ) - def test_http11_connection_handles_one_active_request(): """ Attempting to send a request while one is already in-flight will raise @@ -167,7 +160,6 @@ def test_http11_connection_handles_one_active_request(): conn.request("GET", "https://example.com/") - def test_http11_connection_attempt_close(): """ A connection can only be closed when it is idle. @@ -189,7 +181,6 @@ def test_http11_connection_attempt_close(): assert response.content == b"Hello, world!" - def test_http11_request_to_incorrect_origin(): """ A connection can only send requests to whichever origin it is connected to. @@ -201,7 +192,6 @@ def test_http11_request_to_incorrect_origin(): conn.request("GET", "https://other.com/") - def test_http11_expect_continue(): """ HTTP "100 Continue" is an interim response. @@ -234,7 +224,6 @@ def test_http11_expect_continue(): assert response.content == b"Hello, world!" - def test_http11_upgrade_connection(): """ HTTP "101 Switching Protocols" indicates an upgraded connection. @@ -269,7 +258,6 @@ def test_http11_upgrade_connection(): assert content == b"..." - def test_http11_early_hints(): """ HTTP "103 Early Hints" is an interim response. @@ -305,7 +293,6 @@ def test_http11_early_hints(): assert response.content == b"Hello, world! ..." - def test_http11_header_sub_100kb(): """ A connection should be able to handle a http header size up to 100kB. diff --git a/tests/_sync/test_http2.py b/tests/_sync/test_http2.py index c7e5ef65..576b5ff6 100644 --- a/tests/_sync/test_http2.py +++ b/tests/_sync/test_http2.py @@ -5,7 +5,6 @@ import httpcore - def test_http2_connection(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -41,12 +40,10 @@ def test_http2_connection(): conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) assert repr(conn) == ( - "" + "" ) - def test_http2_connection_closed(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -82,7 +79,6 @@ def test_http2_connection_closed(): assert not conn.is_available() - def test_http2_connection_post_request(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -114,7 +110,6 @@ def test_http2_connection_post_request(): assert response.content == b"Hello, world!" - def test_http2_connection_with_remote_protocol_error(): """ If a remote protocol error occurs, then no response will be returned, @@ -127,7 +122,6 @@ def test_http2_connection_with_remote_protocol_error(): conn.request("GET", "https://example.com/") - def test_http2_connection_with_rst_stream(): """ If a stream reset occurs, then no response will be returned, @@ -173,7 +167,6 @@ def test_http2_connection_with_rst_stream(): assert response.status == 200 - def test_http2_connection_with_goaway(): """ If a GoAway frame occurs, then no response will be returned, @@ -223,7 +216,6 @@ def test_http2_connection_with_goaway(): conn.request("GET", "https://example.com/") - def test_http2_connection_with_flow_control(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -283,7 +275,6 @@ def test_http2_connection_with_flow_control(): assert response.content == b"100,000 bytes received" - def test_http2_connection_attempt_close(): """ A connection can only be closed when it is idle. @@ -318,7 +309,6 @@ def test_http2_connection_attempt_close(): conn.request("GET", "https://example.com/") - def test_http2_request_to_incorrect_origin(): """ A connection can only send requests to whichever origin it is connected to. @@ -330,7 +320,6 @@ def test_http2_request_to_incorrect_origin(): conn.request("GET", "https://other.com/") - def test_http2_remote_max_streams_update(): """ If the remote server updates the maximum concurrent streams value, we should diff --git a/tests/_sync/test_http_proxy.py b/tests/_sync/test_http_proxy.py index 8ccd9938..0db03ee9 100644 --- a/tests/_sync/test_http_proxy.py +++ b/tests/_sync/test_http_proxy.py @@ -16,7 +16,6 @@ ) - def test_proxy_forwarding(): """ Send an HTTP request via a proxy. @@ -72,7 +71,6 @@ def test_proxy_forwarding(): ) - def test_proxy_tunneling(): """ Send an HTTPS request via a proxy. @@ -157,7 +155,6 @@ def connect_tcp( return HTTP1ThenHTTP2Stream(list(self._buffer)) - def test_proxy_tunneling_http2(): """ Send an HTTP/2 request via a proxy. @@ -225,7 +222,6 @@ def test_proxy_tunneling_http2(): ) - def test_proxy_tunneling_with_403(): """ Send an HTTPS request via a proxy. @@ -246,7 +242,6 @@ def test_proxy_tunneling_with_403(): assert not proxy.connections - def test_proxy_tunneling_with_auth(): """ Send an authenticated HTTPS request via a proxy. diff --git a/tests/_sync/test_integration.py b/tests/_sync/test_integration.py index 07254f66..ca5a2d9a 100644 --- a/tests/_sync/test_integration.py +++ b/tests/_sync/test_integration.py @@ -5,14 +5,12 @@ import httpcore - def test_request(httpbin): with httpcore.ConnectionPool() as pool: response = pool.request("GET", httpbin.url) assert response.status == 200 - def test_ssl_request(httpbin_secure): ssl_context = ssl.create_default_context() ssl_context.check_hostname = False @@ -22,7 +20,6 @@ def test_ssl_request(httpbin_secure): assert response.status == 200 - def test_extra_info(httpbin_secure): ssl_context = ssl.create_default_context() ssl_context.check_hostname = False diff --git a/tests/_sync/test_socks_proxy.py b/tests/_sync/test_socks_proxy.py index c61c9037..a0652130 100644 --- a/tests/_sync/test_socks_proxy.py +++ b/tests/_sync/test_socks_proxy.py @@ -3,7 +3,6 @@ import httpcore - def test_socks5_request(): """ Send an HTTP request via a SOCKS proxy. @@ -64,7 +63,6 @@ def test_socks5_request(): ) - def test_authenticated_socks5_request(): """ Send an HTTP request via a SOCKS proxy. @@ -113,7 +111,6 @@ def test_authenticated_socks5_request(): assert not proxy.connections[0].is_closed() - def test_socks5_request_connect_failed(): """ Attempt to send an HTTP request via a SOCKS proxy, resulting in a connect failure. @@ -142,7 +139,6 @@ def test_socks5_request_connect_failed(): assert not proxy.connections - def test_socks5_request_failed_to_provide_auth(): """ Attempt to send an HTTP request via an authenticated SOCKS proxy, @@ -170,7 +166,6 @@ def test_socks5_request_failed_to_provide_auth(): assert not proxy.connections - def test_socks5_request_incorrect_auth(): """ Attempt to send an HTTP request via an authenticated SOCKS proxy, diff --git a/unasync.py b/unasync.py index b8dee7bc..b6a54b96 100755 --- a/unasync.py +++ b/unasync.py @@ -6,35 +6,35 @@ SUBS = [ ( - 'from .._backends.auto import AutoBackend', - 'from .._backends.sync import SyncBackend', + "from .._backends.auto import AutoBackend", + "from .._backends.sync import SyncBackend", ), - ('import trio as concurrency', 'from tests import concurrency'), - ('AsyncIterator', 'Iterator'), - ('Async([A-Z][A-Za-z0-9_]*)', r'\2'), - ('async def', 'def'), - ('async with', 'with'), - ('async for', 'for'), - ('await ', ''), - ('handle_async_request', 'handle_request'), - ('aclose', 'close'), - ('aiter_stream', 'iter_stream'), - ('aread', 'read'), - ('asynccontextmanager', 'contextmanager'), - ('__aenter__', '__enter__'), - ('__aexit__', '__exit__'), - ('__aiter__', '__iter__'), - ('@pytest.mark.anyio', ''), - ('@pytest.mark.trio', ''), - ('AutoBackend', 'SyncBackend'), + ("import trio as concurrency", "from tests import concurrency"), + ("AsyncIterator", "Iterator"), + ("Async([A-Z][A-Za-z0-9_]*)", r"\2"), + ("async def", "def"), + ("async with", "with"), + ("async for", "for"), + ("await ", ""), + ("handle_async_request", "handle_request"), + ("aclose", "close"), + ("aiter_stream", "iter_stream"), + ("aread", "read"), + ("asynccontextmanager", "contextmanager"), + ("__aenter__", "__enter__"), + ("__aexit__", "__exit__"), + ("__aiter__", "__iter__"), + ("@pytest.mark.anyio", ""), + ("@pytest.mark.trio", ""), + ("AutoBackend", "SyncBackend"), ] COMPILED_SUBS = [ - (re.compile(r'(^|\b)' + regex + r'($|\b)'), repl) - for regex, repl in SUBS + (re.compile(r"(^|\b)" + regex + r"($|\b)"), repl) for regex, repl in SUBS ] USED_SUBS = set() + def unasync_line(line): for index, (regex, repl) in enumerate(COMPILED_SUBS): old_line = line @@ -58,22 +58,22 @@ def unasync_file_check(in_path, out_path): for in_line, out_line in zip(in_file.readlines(), out_file.readlines()): expected = unasync_line(in_line) if out_line != expected: - print(f'unasync mismatch between {in_path!r} and {out_path!r}') - print(f'Async code: {in_line!r}') - print(f'Expected sync code: {expected!r}') - print(f'Actual sync code: {out_line!r}') + print(f"unasync mismatch between {in_path!r} and {out_path!r}") + print(f"Async code: {in_line!r}") + print(f"Expected sync code: {expected!r}") + print(f"Actual sync code: {out_line!r}") sys.exit(1) def unasync_dir(in_dir, out_dir, check_only=False): for dirpath, dirnames, filenames in os.walk(in_dir): for filename in filenames: - if not filename.endswith('.py'): + if not filename.endswith(".py"): continue rel_dir = os.path.relpath(dirpath, in_dir) in_path = os.path.normpath(os.path.join(in_dir, rel_dir, filename)) out_path = os.path.normpath(os.path.join(out_dir, rel_dir, filename)) - print(in_path, '->', out_path) + print(in_path, "->", out_path) if check_only: unasync_file_check(in_path, out_path) else: @@ -81,7 +81,7 @@ def unasync_dir(in_dir, out_dir, check_only=False): def main(): - check_only = '--check' in sys.argv + check_only = "--check" in sys.argv unasync_dir("httpcore/_async", "httpcore/_sync", check_only=check_only) unasync_dir("tests/_async", "tests/_sync", check_only=check_only) @@ -93,5 +93,5 @@ def main(): exit(1) -if __name__ == '__main__': +if __name__ == "__main__": main() From 5c176c7a6629835a8e3724cb94baf1c6a0e0d080 Mon Sep 17 00:00:00 2001 From: Tester Date: Tue, 13 Feb 2024 19:14:43 +0330 Subject: [PATCH 4/8] 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"") From 779653c8f3f3ec74f8d9e36083f333b7cb7bf703 Mon Sep 17 00:00:00 2001 From: Tester Date: Tue, 13 Feb 2024 21:10:48 +0330 Subject: [PATCH 5/8] format `tests/_(a)sync` to pass unasync check --- tests/_async/test_connection.py | 29 ++++++++++++----- tests/_async/test_connection_pool.py | 16 +++++++--- tests/_async/test_http11.py | 2 +- tests/_async/test_http2.py | 3 +- tests/_sync/test_connection.py | 48 ++++++++++++++++++++++++---- tests/_sync/test_connection_pool.py | 42 +++++++++++++++++++++--- tests/_sync/test_http11.py | 17 ++++++++-- tests/_sync/test_http2.py | 12 ++++++- tests/_sync/test_http_proxy.py | 5 +++ tests/_sync/test_integration.py | 3 ++ tests/_sync/test_socks_proxy.py | 5 +++ unasync.py | 4 +-- 12 files changed, 155 insertions(+), 31 deletions(-) diff --git a/tests/_async/test_connection.py b/tests/_async/test_connection.py index b5093c28..414c3edf 100644 --- a/tests/_async/test_connection.py +++ b/tests/_async/test_connection.py @@ -86,8 +86,8 @@ async def test_concurrent_requests_not_available_on_http11_connections(): await conn.request("GET", "https://example.com/") -@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") @pytest.mark.anyio +@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") async def test_write_error_with_response_sent(): """ If a server half-closes the connection while the client is sending @@ -103,7 +103,9 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: self.count = 0 async def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: self.count += len(buffer) @@ -157,7 +159,9 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: self.count = 0 async def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: self.count += len(buffer) @@ -212,7 +216,9 @@ async def test_http2_connection(): ) async with AsyncHTTPConnection( - origin=origin, network_backend=network_backend, http2=True + origin=origin, + network_backend=network_backend, + http2=True, ) as conn: response = await conn.request("GET", "https://example.com/") @@ -229,7 +235,8 @@ async def test_request_to_incorrect_origin(): origin = Origin(b"https", b"example.com", 443) network_backend = AsyncMockBackend([]) async with AsyncHTTPConnection( - origin=origin, network_backend=network_backend + origin=origin, + network_backend=network_backend, ) as conn: with pytest.raises(RuntimeError): await conn.request("GET", "https://other.com/") @@ -266,18 +273,24 @@ async def connect_tcp( class _NeedsRetryAsyncNetworkStream(AsyncNetworkStream): def __init__( - self, backend: "NeedsRetryBackend", stream: AsyncNetworkStream + self, + backend: "NeedsRetryBackend", + stream: AsyncNetworkStream, ) -> None: self._backend = backend self._stream = stream async def read( - self, max_bytes: int, timeout: typing.Optional[float] = None + self, + max_bytes: int, + timeout: typing.Optional[float] = None, ) -> bytes: return await self._stream.read(max_bytes, timeout) async def write( - self, buffer: bytes, timeout: typing.Optional[float] = None + self, + buffer: bytes, + timeout: typing.Optional[float] = None, ) -> None: await self._stream.write(buffer, timeout) diff --git a/tests/_async/test_connection_pool.py b/tests/_async/test_connection_pool.py index 192cd769..0dc8b485 100644 --- a/tests/_async/test_connection_pool.py +++ b/tests/_async/test_connection_pool.py @@ -399,7 +399,9 @@ async def trace(name, kwargs): # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): await pool.request( - "GET", "https://example.com/", extensions={"trace": trace} + "GET", + "https://example.com/", + extensions={"trace": trace}, ) info = [repr(c) for c in pool.connections] @@ -452,7 +454,9 @@ async def trace(name, kwargs): # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): await pool.request( - "GET", "https://example.com/", extensions={"trace": trace} + "GET", + "https://example.com/", + extensions={"trace": trace}, ) info = [repr(c) for c in pool.connections] @@ -775,13 +779,17 @@ async def test_connection_pool_timeout_zero(): # Two consecutive requests with a pool timeout of zero. # Both succeed without raising a timeout. response = await pool.request( - "GET", "https://example.com/", extensions=extensions + "GET", + "https://example.com/", + extensions=extensions, ) assert response.status == 200 assert response.content == b"Hello, world!" response = await pool.request( - "GET", "https://example.com/", extensions=extensions + "GET", + "https://example.com/", + extensions=extensions, ) assert response.status == 200 assert response.content == b"Hello, world!" diff --git a/tests/_async/test_http11.py b/tests/_async/test_http11.py index db34011a..90b213bf 100644 --- a/tests/_async/test_http11.py +++ b/tests/_async/test_http11.py @@ -29,7 +29,7 @@ async def test_http11_connection(): assert repr(conn) == ( "" - ) + ) # fmt: skip @pytest.mark.anyio diff --git a/tests/_async/test_http2.py b/tests/_async/test_http2.py index fe1c1516..45658348 100644 --- a/tests/_async/test_http2.py +++ b/tests/_async/test_http2.py @@ -41,8 +41,7 @@ async def test_http2_connection(): conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) assert repr(conn) == ( - "" + "" ) diff --git a/tests/_sync/test_connection.py b/tests/_sync/test_connection.py index c84a882b..fbae0630 100644 --- a/tests/_sync/test_connection.py +++ b/tests/_sync/test_connection.py @@ -19,6 +19,7 @@ ) +# unasync anyio def test_http_connection(): origin = Origin(b"https", b"example.com", 443) network_backend = MockBackend( @@ -60,6 +61,7 @@ def test_http_connection(): ) +# unasync anyio def test_concurrent_requests_not_available_on_http11_connections(): """ Attempting to issue a request against an already active HTTP/1.1 connection @@ -84,6 +86,7 @@ def test_concurrent_requests_not_available_on_http11_connections(): conn.request("GET", "https://example.com/") +# unasync anyio @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") def test_write_error_with_response_sent(): """ @@ -99,7 +102,11 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: super().__init__(buffer, http2) self.count = 0 - def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: + def write( + self, + buffer: bytes, + timeout: typing.Optional[float] = None, + ) -> None: self.count += len(buffer) if self.count > 1_000_000: @@ -136,6 +143,7 @@ def connect_tcp( assert response.content == b"Request body exceeded 1,000,000 bytes" +# unasync anyio @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") def test_write_error_without_response_sent(): """ @@ -150,7 +158,11 @@ def __init__(self, buffer: typing.List[bytes], http2: bool = False) -> None: super().__init__(buffer, http2) self.count = 0 - def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: + def write( + self, + buffer: bytes, + timeout: typing.Optional[float] = None, + ) -> None: self.count += len(buffer) if self.count > 1_000_000: @@ -179,6 +191,7 @@ def connect_tcp( assert str(exc_info.value) == "Server disconnected without sending a response." +# unasync anyio @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") def test_http2_connection(): origin = Origin(b"https", b"example.com", 443) @@ -203,7 +216,9 @@ def test_http2_connection(): ) with HTTPConnection( - origin=origin, network_backend=network_backend, http2=True + origin=origin, + network_backend=network_backend, + http2=True, ) as conn: response = conn.request("GET", "https://example.com/") @@ -212,13 +227,17 @@ def test_http2_connection(): assert response.extensions["http_version"] == b"HTTP/2" +# unasync anyio def test_request_to_incorrect_origin(): """ A connection can only send requests whichever origin it is connected to. """ origin = Origin(b"https", b"example.com", 443) network_backend = MockBackend([]) - with HTTPConnection(origin=origin, network_backend=network_backend) as conn: + with HTTPConnection( + origin=origin, + network_backend=network_backend, + ) as conn: with pytest.raises(RuntimeError): conn.request("GET", "https://other.com/") @@ -253,14 +272,26 @@ def connect_tcp( return self._NeedsRetryAsyncNetworkStream(self, stream) class _NeedsRetryAsyncNetworkStream(NetworkStream): - def __init__(self, backend: "NeedsRetryBackend", stream: NetworkStream) -> None: + def __init__( + self, + backend: "NeedsRetryBackend", + stream: NetworkStream, + ) -> None: self._backend = backend self._stream = stream - def read(self, max_bytes: int, timeout: typing.Optional[float] = None) -> bytes: + def read( + self, + max_bytes: int, + timeout: typing.Optional[float] = None, + ) -> bytes: return self._stream.read(max_bytes, timeout) - def write(self, buffer: bytes, timeout: typing.Optional[float] = None) -> None: + def write( + self, + buffer: bytes, + timeout: typing.Optional[float] = None, + ) -> None: self._stream.write(buffer, timeout) def close(self) -> None: @@ -283,6 +314,7 @@ def get_extra_info(self, info: str) -> typing.Any: return self._stream.get_extra_info(info) +# unasync anyio def test_connection_retries(): origin = Origin(b"https", b"example.com", 443) content = [ @@ -309,6 +341,7 @@ def test_connection_retries(): conn.request("GET", "https://example.com/") +# unasync anyio def test_connection_retries_tls(): origin = Origin(b"https", b"example.com", 443) content = [ @@ -339,6 +372,7 @@ def test_connection_retries_tls(): conn.request("GET", "https://example.com/") +# unasync anyio def test_uds_connections(): # We're not actually testing Unix Domain Sockets here, because we're just # using a mock backend, but at least we're covering the UDS codepath diff --git a/tests/_sync/test_connection_pool.py b/tests/_sync/test_connection_pool.py index 1a296ac1..c115c4dd 100644 --- a/tests/_sync/test_connection_pool.py +++ b/tests/_sync/test_connection_pool.py @@ -9,6 +9,7 @@ import httpcore +# unasync anyio def test_connection_pool_with_keepalive(): """ By default HTTP/1.1 requests should be returned to the connection pool. @@ -113,6 +114,7 @@ def test_connection_pool_with_keepalive(): ) +# unasync anyio def test_connection_pool_with_close(): """ HTTP/1.1 requests that include a 'Connection: Close' header should @@ -146,6 +148,7 @@ def test_connection_pool_with_close(): assert info == [] +# unasync anyio def test_connection_pool_with_http2(): """ Test a connection pool with HTTP/2 requests. @@ -210,6 +213,7 @@ def test_connection_pool_with_http2(): ] +# unasync anyio def test_connection_pool_with_http2_goaway(): """ Test a connection pool with HTTP/2 requests, that cleanly disconnects @@ -266,6 +270,7 @@ def test_connection_pool_with_http2_goaway(): ] +# unasync anyio def test_trace_request(): """ The 'trace' request extension allows for a callback function to inspect the @@ -307,6 +312,7 @@ def trace(name, kwargs): ] +# unasync anyio def test_debug_request(caplog): """ The 'trace' request extension allows for a callback function to inspect the @@ -376,6 +382,7 @@ def test_debug_request(caplog): ] +# unasync anyio def test_connection_pool_with_http_exception(): """ HTTP/1.1 requests that result in an exception during the connection should @@ -391,7 +398,11 @@ def trace(name, kwargs): with httpcore.ConnectionPool(network_backend=network_backend) as pool: # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): - pool.request("GET", "https://example.com/", extensions={"trace": trace}) + pool.request( + "GET", + "https://example.com/", + extensions={"trace": trace}, + ) info = [repr(c) for c in pool.connections] assert info == [] @@ -412,6 +423,7 @@ def trace(name, kwargs): ] +# unasync anyio def test_connection_pool_with_connect_exception(): """ HTTP/1.1 requests that result in an exception during connection should not @@ -441,7 +453,11 @@ def trace(name, kwargs): with httpcore.ConnectionPool(network_backend=network_backend) as pool: # Sending an initial request, which once complete will not return to the pool. with pytest.raises(Exception): - pool.request("GET", "https://example.com/", extensions={"trace": trace}) + pool.request( + "GET", + "https://example.com/", + extensions={"trace": trace}, + ) info = [repr(c) for c in pool.connections] assert info == [] @@ -452,6 +468,7 @@ def trace(name, kwargs): ] +# unasync anyio def test_connection_pool_with_immediate_expiry(): """ Connection pools with keepalive_expiry=0.0 should immediately expire @@ -486,6 +503,7 @@ def test_connection_pool_with_immediate_expiry(): assert info == [] +# unasync anyio def test_connection_pool_with_no_keepalive_connections_allowed(): """ When 'max_keepalive_connections=0' is used, IDLE connections should not @@ -519,6 +537,7 @@ def test_connection_pool_with_no_keepalive_connections_allowed(): assert info == [] +# unasync trio def test_connection_pool_concurrency(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -568,6 +587,7 @@ def fetch(pool, domain, info_list): ] +# unasync trio def test_connection_pool_concurrency_same_domain_closing(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -609,6 +629,7 @@ def fetch(pool, domain, info_list): ) +# unasync trio def test_connection_pool_concurrency_same_domain_keepalive(): """ HTTP/1.1 requests made in concurrency must not ever exceed the maximum number @@ -663,6 +684,7 @@ def fetch(pool, domain, info_list): ) +# unasync anyio def test_unsupported_protocol(): with httpcore.ConnectionPool() as pool: with pytest.raises(httpcore.UnsupportedProtocol): @@ -672,6 +694,7 @@ def test_unsupported_protocol(): pool.request("GET", "://www.example.com/") +# unasync anyio def test_connection_pool_closed_while_request_in_flight(): """ Closing a connection pool while a request/response is still in-flight @@ -698,6 +721,7 @@ def test_connection_pool_closed_while_request_in_flight(): response.read() +# unasync anyio def test_connection_pool_timeout(): """ Ensure that exceeding max_connections can cause a request to timeout. @@ -724,6 +748,7 @@ def test_connection_pool_timeout(): pool.request("GET", "https://example.com/", extensions=extensions) +# unasync anyio def test_connection_pool_timeout_zero(): """ A pool timeout of 0 shouldn't raise a PoolTimeout if there's @@ -753,11 +778,19 @@ def test_connection_pool_timeout_zero(): ) as pool: # Two consecutive requests with a pool timeout of zero. # Both succeed without raising a timeout. - response = pool.request("GET", "https://example.com/", extensions=extensions) + response = pool.request( + "GET", + "https://example.com/", + extensions=extensions, + ) assert response.status == 200 assert response.content == b"Hello, world!" - response = pool.request("GET", "https://example.com/", extensions=extensions) + response = pool.request( + "GET", + "https://example.com/", + extensions=extensions, + ) assert response.status == 200 assert response.content == b"Hello, world!" @@ -781,6 +814,7 @@ def test_connection_pool_timeout_zero(): assert response.content == b"Hello, world!" +# unasync anyio def test_http11_upgrade_connection(): """ HTTP "101 Switching Protocols" indicates an upgraded connection. diff --git a/tests/_sync/test_http11.py b/tests/_sync/test_http11.py index c6486b80..de4bebf9 100644 --- a/tests/_sync/test_http11.py +++ b/tests/_sync/test_http11.py @@ -3,6 +3,7 @@ import httpcore +# unasync anyio def test_http11_connection(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -26,10 +27,12 @@ def test_http11_connection(): assert conn.is_available() assert not conn.has_expired() assert repr(conn) == ( - "" - ) + "" + ) # fmt: skip +# unasync anyio def test_http11_connection_unread_response(): """ If the client releases the response without reading it to termination, @@ -59,6 +62,7 @@ def test_http11_connection_unread_response(): ) +# unasync anyio def test_http11_connection_with_remote_protocol_error(): """ If a remote protocol error occurs, then no response will be returned, @@ -80,6 +84,7 @@ def test_http11_connection_with_remote_protocol_error(): ) +# unasync anyio def test_http11_connection_with_incomplete_response(): """ We should be gracefully handling the case where the connection ends prematurely. @@ -108,6 +113,7 @@ def test_http11_connection_with_incomplete_response(): ) +# unasync anyio def test_http11_connection_with_local_protocol_error(): """ If a local protocol error occurs, then no response will be returned, @@ -139,6 +145,7 @@ def test_http11_connection_with_local_protocol_error(): ) +# unasync anyio def test_http11_connection_handles_one_active_request(): """ Attempting to send a request while one is already in-flight will raise @@ -160,6 +167,7 @@ def test_http11_connection_handles_one_active_request(): conn.request("GET", "https://example.com/") +# unasync anyio def test_http11_connection_attempt_close(): """ A connection can only be closed when it is idle. @@ -181,6 +189,7 @@ def test_http11_connection_attempt_close(): assert response.content == b"Hello, world!" +# unasync anyio def test_http11_request_to_incorrect_origin(): """ A connection can only send requests to whichever origin it is connected to. @@ -192,6 +201,7 @@ def test_http11_request_to_incorrect_origin(): conn.request("GET", "https://other.com/") +# unasync anyio def test_http11_expect_continue(): """ HTTP "100 Continue" is an interim response. @@ -224,6 +234,7 @@ def test_http11_expect_continue(): assert response.content == b"Hello, world!" +# unasync anyio def test_http11_upgrade_connection(): """ HTTP "101 Switching Protocols" indicates an upgraded connection. @@ -258,6 +269,7 @@ def test_http11_upgrade_connection(): assert content == b"..." +# unasync anyio def test_http11_early_hints(): """ HTTP "103 Early Hints" is an interim response. @@ -293,6 +305,7 @@ def test_http11_early_hints(): assert response.content == b"Hello, world! ..." +# unasync anyio def test_http11_header_sub_100kb(): """ A connection should be able to handle a http header size up to 100kB. diff --git a/tests/_sync/test_http2.py b/tests/_sync/test_http2.py index 576b5ff6..b7ad5ae4 100644 --- a/tests/_sync/test_http2.py +++ b/tests/_sync/test_http2.py @@ -5,6 +5,7 @@ import httpcore +# unasync anyio def test_http2_connection(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -40,10 +41,11 @@ def test_http2_connection(): conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1" ) assert repr(conn) == ( - "" + "" ) +# unasync anyio def test_http2_connection_closed(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -79,6 +81,7 @@ def test_http2_connection_closed(): assert not conn.is_available() +# unasync anyio def test_http2_connection_post_request(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -110,6 +113,7 @@ def test_http2_connection_post_request(): assert response.content == b"Hello, world!" +# unasync anyio def test_http2_connection_with_remote_protocol_error(): """ If a remote protocol error occurs, then no response will be returned, @@ -122,6 +126,7 @@ def test_http2_connection_with_remote_protocol_error(): conn.request("GET", "https://example.com/") +# unasync anyio def test_http2_connection_with_rst_stream(): """ If a stream reset occurs, then no response will be returned, @@ -167,6 +172,7 @@ def test_http2_connection_with_rst_stream(): assert response.status == 200 +# unasync anyio def test_http2_connection_with_goaway(): """ If a GoAway frame occurs, then no response will be returned, @@ -216,6 +222,7 @@ def test_http2_connection_with_goaway(): conn.request("GET", "https://example.com/") +# unasync anyio def test_http2_connection_with_flow_control(): origin = httpcore.Origin(b"https", b"example.com", 443) stream = httpcore.MockStream( @@ -275,6 +282,7 @@ def test_http2_connection_with_flow_control(): assert response.content == b"100,000 bytes received" +# unasync anyio def test_http2_connection_attempt_close(): """ A connection can only be closed when it is idle. @@ -309,6 +317,7 @@ def test_http2_connection_attempt_close(): conn.request("GET", "https://example.com/") +# unasync anyio def test_http2_request_to_incorrect_origin(): """ A connection can only send requests to whichever origin it is connected to. @@ -320,6 +329,7 @@ def test_http2_request_to_incorrect_origin(): conn.request("GET", "https://other.com/") +# unasync anyio def test_http2_remote_max_streams_update(): """ If the remote server updates the maximum concurrent streams value, we should diff --git a/tests/_sync/test_http_proxy.py b/tests/_sync/test_http_proxy.py index 0db03ee9..38609deb 100644 --- a/tests/_sync/test_http_proxy.py +++ b/tests/_sync/test_http_proxy.py @@ -16,6 +16,7 @@ ) +# unasync anyio def test_proxy_forwarding(): """ Send an HTTP request via a proxy. @@ -71,6 +72,7 @@ def test_proxy_forwarding(): ) +# unasync anyio def test_proxy_tunneling(): """ Send an HTTPS request via a proxy. @@ -155,6 +157,7 @@ def connect_tcp( return HTTP1ThenHTTP2Stream(list(self._buffer)) +# unasync anyio def test_proxy_tunneling_http2(): """ Send an HTTP/2 request via a proxy. @@ -222,6 +225,7 @@ def test_proxy_tunneling_http2(): ) +# unasync anyio def test_proxy_tunneling_with_403(): """ Send an HTTPS request via a proxy. @@ -242,6 +246,7 @@ def test_proxy_tunneling_with_403(): assert not proxy.connections +# unasync anyio def test_proxy_tunneling_with_auth(): """ Send an authenticated HTTPS request via a proxy. diff --git a/tests/_sync/test_integration.py b/tests/_sync/test_integration.py index ca5a2d9a..44502743 100644 --- a/tests/_sync/test_integration.py +++ b/tests/_sync/test_integration.py @@ -5,12 +5,14 @@ import httpcore +# unasync anyio def test_request(httpbin): with httpcore.ConnectionPool() as pool: response = pool.request("GET", httpbin.url) assert response.status == 200 +# unasync anyio def test_ssl_request(httpbin_secure): ssl_context = ssl.create_default_context() ssl_context.check_hostname = False @@ -20,6 +22,7 @@ def test_ssl_request(httpbin_secure): assert response.status == 200 +# unasync anyio def test_extra_info(httpbin_secure): ssl_context = ssl.create_default_context() ssl_context.check_hostname = False diff --git a/tests/_sync/test_socks_proxy.py b/tests/_sync/test_socks_proxy.py index a0652130..56d376e2 100644 --- a/tests/_sync/test_socks_proxy.py +++ b/tests/_sync/test_socks_proxy.py @@ -3,6 +3,7 @@ import httpcore +# unasync anyio def test_socks5_request(): """ Send an HTTP request via a SOCKS proxy. @@ -63,6 +64,7 @@ def test_socks5_request(): ) +# unasync anyio def test_authenticated_socks5_request(): """ Send an HTTP request via a SOCKS proxy. @@ -111,6 +113,7 @@ def test_authenticated_socks5_request(): assert not proxy.connections[0].is_closed() +# unasync anyio def test_socks5_request_connect_failed(): """ Attempt to send an HTTP request via a SOCKS proxy, resulting in a connect failure. @@ -139,6 +142,7 @@ def test_socks5_request_connect_failed(): assert not proxy.connections +# unasync anyio def test_socks5_request_failed_to_provide_auth(): """ Attempt to send an HTTP request via an authenticated SOCKS proxy, @@ -166,6 +170,7 @@ def test_socks5_request_failed_to_provide_auth(): assert not proxy.connections +# unasync anyio def test_socks5_request_incorrect_auth(): """ Attempt to send an HTTP request via an authenticated SOCKS proxy, diff --git a/unasync.py b/unasync.py index b6a54b96..1ed8ece4 100755 --- a/unasync.py +++ b/unasync.py @@ -24,8 +24,8 @@ ("__aenter__", "__enter__"), ("__aexit__", "__exit__"), ("__aiter__", "__iter__"), - ("@pytest.mark.anyio", ""), - ("@pytest.mark.trio", ""), + ("@pytest.mark.anyio", "# unasync anyio"), + ("@pytest.mark.trio", "# unasync trio"), ("AutoBackend", "SyncBackend"), ] COMPILED_SUBS = [ From 2ef5156dcf125fcf12af75251bd8e4649ba2963c Mon Sep 17 00:00:00 2001 From: T-256 <132141463+T-256@users.noreply.github.com> Date: Tue, 20 Feb 2024 20:23:13 +0330 Subject: [PATCH 6/8] Update unasync.py --- unasync.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/unasync.py b/unasync.py index 1ed8ece4..a312e515 100755 --- a/unasync.py +++ b/unasync.py @@ -5,10 +5,7 @@ from pprint import pprint SUBS = [ - ( - "from .._backends.auto import AutoBackend", - "from .._backends.sync import SyncBackend", - ), + ('from .._backends.auto import AutoBackend', 'from .._backends.sync import SyncBackend'), ("import trio as concurrency", "from tests import concurrency"), ("AsyncIterator", "Iterator"), ("Async([A-Z][A-Za-z0-9_]*)", r"\2"), From b9a86a21d56fa5942f7ffc7824a6373cfc09bbb4 Mon Sep 17 00:00:00 2001 From: T-256 <132141463+T-256@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:10:11 +0330 Subject: [PATCH 7/8] include `scripts` --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index d65e3f07..dbce8579 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -101,6 +101,7 @@ omit = [ ] include = [ "httpcore/*", + "scripts/*", "tests/*", ] From ff415957793dd83e0a4e161ffd5dcba8f2252f50 Mon Sep 17 00:00:00 2001 From: T-256 Date: Sat, 13 Apr 2024 15:35:25 +0330 Subject: [PATCH 8/8] correctly include `scripts` --- pyproject.toml | 1 - scripts/check | 2 +- scripts/lint | 2 +- scripts/unasync.py | 15 +++++++++------ 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index dbce8579..d65e3f07 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -101,7 +101,6 @@ omit = [ ] include = [ "httpcore/*", - "scripts/*", "tests/*", ] diff --git a/scripts/check b/scripts/check index cfc90918..511f4f6c 100755 --- a/scripts/check +++ b/scripts/check @@ -4,7 +4,7 @@ export PREFIX="" if [ -d 'venv' ] ; then export PREFIX="venv/bin/" fi -export SOURCE_FILES="httpcore tests" +export SOURCE_FILES="httpcore scripts tests" set -x diff --git a/scripts/lint b/scripts/lint index e40524f5..a57414b3 100755 --- a/scripts/lint +++ b/scripts/lint @@ -4,7 +4,7 @@ export PREFIX="" if [ -d 'venv' ] ; then export PREFIX="venv/bin/" fi -export SOURCE_FILES="httpcore tests" +export SOURCE_FILES="httpcore scripts tests" set -x diff --git a/scripts/unasync.py b/scripts/unasync.py index a312e515..618ff639 100644 --- a/scripts/unasync.py +++ b/scripts/unasync.py @@ -5,7 +5,10 @@ from pprint import pprint SUBS = [ - ('from .._backends.auto import AutoBackend', 'from .._backends.sync import SyncBackend'), + ( + "from .._backends.auto import AutoBackend", + "from .._backends.sync import SyncBackend", + ), ("import trio as concurrency", "from tests import concurrency"), ("AsyncIterator", "Iterator"), ("Async([A-Z][A-Za-z0-9_]*)", r"\2"), @@ -32,7 +35,7 @@ USED_SUBS = set() -def unasync_line(line): +def unasync_line(line: str) -> str: for index, (regex, repl) in enumerate(COMPILED_SUBS): old_line = line line = re.sub(regex, repl, line) @@ -41,7 +44,7 @@ def unasync_line(line): return line -def unasync_file(in_path, out_path): +def unasync_file(in_path: str, out_path: str) -> None: with open(in_path, "r") as in_file: with open(out_path, "w", newline="") as out_file: for line in in_file.readlines(): @@ -49,7 +52,7 @@ def unasync_file(in_path, out_path): out_file.write(line) -def unasync_file_check(in_path, out_path): +def unasync_file_check(in_path: str, out_path: str) -> None: with open(in_path, "r") as in_file: with open(out_path, "r") as out_file: for in_line, out_line in zip(in_file.readlines(), out_file.readlines()): @@ -62,7 +65,7 @@ def unasync_file_check(in_path, out_path): sys.exit(1) -def unasync_dir(in_dir, out_dir, check_only=False): +def unasync_dir(in_dir: str, out_dir: str, check_only: bool = False) -> None: for dirpath, dirnames, filenames in os.walk(in_dir): for filename in filenames: if not filename.endswith(".py"): @@ -77,7 +80,7 @@ def unasync_dir(in_dir, out_dir, check_only=False): unasync_file(in_path, out_path) -def main(): +def main() -> None: check_only = "--check" in sys.argv unasync_dir("httpcore/_async", "httpcore/_sync", check_only=check_only) unasync_dir("tests/_async", "tests/_sync", check_only=check_only)