Skip to content

Commit 5541f64

Browse files
igor.stulikovigor.stulikov
igor.stulikov
authored and
igor.stulikov
committed
Fix linters (encode#550)
1 parent e061990 commit 5541f64

File tree

6 files changed

+88
-63
lines changed

6 files changed

+88
-63
lines changed

httpcore/_async/connection_pool.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ async def _attempt_to_acquire_connection(self, status: RequestStatus) -> bool:
174174

175175
# Attempt to close CONNECTING connections that no one needs
176176
if self._is_pool_full:
177-
for idx, connection in enumerate(self._pool): # Try to check old connections first
177+
for idx, connection in enumerate(
178+
self._pool
179+
): # Try to check old connections first
178180
if not connection.is_connecting():
179181
continue
180182
for req_status in self._requests:

httpcore/_sync/connection_pool.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ def _attempt_to_acquire_connection(self, status: RequestStatus) -> bool:
174174

175175
# Attempt to close CONNECTING connections that no one needs
176176
if self._is_pool_full:
177-
for idx, connection in enumerate(self._pool): # Try to check old connections first
177+
for idx, connection in enumerate(
178+
self._pool
179+
): # Try to check old connections first
178180
if not connection.is_connecting():
179181
continue
180182
for req_status in self._requests:

httpcore/backends/mock.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import anyio
77

88
from httpcore import ReadTimeout
9-
from .base import AsyncNetworkBackend, AsyncNetworkStream, NetworkBackend, NetworkStream
9+
1010
from .._exceptions import ReadError
11+
from .base import AsyncNetworkBackend, AsyncNetworkStream, NetworkBackend, NetworkStream
1112

1213

1314
class MockSSLObject:
@@ -51,18 +52,16 @@ def get_extra_info(self, info: str) -> typing.Any:
5152

5253
class HangingStream(MockStream):
5354
def read(self, max_bytes: int, timeout: Optional[float] = None) -> bytes:
54-
if self._closed:
55-
raise ReadError("Connection closed")
5655
time.sleep(timeout or 0.1)
5756
raise ReadTimeout
5857

5958

6059
class MockBackend(NetworkBackend):
6160
def __init__(
62-
self,
63-
buffer: typing.List[bytes],
64-
http2: bool = False,
65-
resp_stream_cls: Optional[Type[NetworkStream]] = None,
61+
self,
62+
buffer: typing.List[bytes],
63+
http2: bool = False,
64+
resp_stream_cls: Optional[Type[MockStream]] = None,
6665
) -> None:
6766
self._buffer = buffer
6867
self._http2 = http2
@@ -119,22 +118,22 @@ def get_extra_info(self, info: str) -> typing.Any:
119118

120119
class AsyncHangingStream(AsyncMockStream):
121120
async def read(self, max_bytes: int, timeout: Optional[float] = None) -> bytes:
122-
if self._closed:
123-
raise ReadError("Connection closed")
124121
await anyio.sleep(timeout or 0.1)
125122
raise ReadTimeout
126123

127124

128125
class AsyncMockBackend(AsyncNetworkBackend):
129126
def __init__(
130-
self,
131-
buffer: typing.List[bytes],
132-
http2: bool = False,
133-
resp_stream_cls: Optional[Type[AsyncNetworkStream]] = None,
127+
self,
128+
buffer: typing.List[bytes],
129+
http2: bool = False,
130+
resp_stream_cls: Optional[Type[AsyncMockStream]] = None,
134131
) -> None:
135132
self._buffer = buffer
136133
self._http2 = http2
137-
self._resp_stream_cls: Type[AsyncMockStream] = resp_stream_cls or AsyncMockStream
134+
self._resp_stream_cls: Type[AsyncMockStream] = (
135+
resp_stream_cls or AsyncMockStream
136+
)
138137

139138
async def connect_tcp(
140139
self,

tests/_async/test_connection_pool.py

+30-22
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
AsyncConnectionPool,
99
ConnectError,
1010
PoolTimeout,
11-
ReadTimeout,
1211
ReadError,
12+
ReadTimeout,
1313
UnsupportedProtocol,
1414
)
1515
from httpcore.backends.base import AsyncNetworkStream
16-
from httpcore.backends.mock import AsyncMockBackend, AsyncHangingStream
16+
from httpcore.backends.mock import AsyncHangingStream, AsyncMockBackend
1717

1818

1919
@pytest.mark.anyio
@@ -511,21 +511,22 @@ async def test_pool_under_load():
511511
"""
512512
network_backend = AsyncMockBackend([], resp_stream_cls=AsyncHangingStream)
513513

514-
async def fetch(_pool: AsyncConnectionPool, *exceptions: Type[BaseException]):
514+
async def fetch(
515+
_pool: AsyncConnectionPool, *exceptions: Type[BaseException]
516+
) -> None:
515517
with contextlib.suppress(*exceptions):
516-
async with pool.stream(
517-
"GET",
518-
"http://a.com/",
519-
extensions={
520-
"timeout": {
521-
"connect": 0.1,
522-
"read": 0.1,
523-
"pool": 0.1,
524-
"write": 0.1,
525-
},
518+
await pool.request(
519+
"GET",
520+
"http://a.com/",
521+
extensions={
522+
"timeout": {
523+
"connect": 0.1,
524+
"read": 0.1,
525+
"pool": 0.1,
526+
"write": 0.1,
526527
},
527-
) as response:
528-
await response.aread()
528+
},
529+
)
529530

530531
async with AsyncConnectionPool(
531532
max_connections=1, network_backend=network_backend
@@ -535,13 +536,13 @@ async def fetch(_pool: AsyncConnectionPool, *exceptions: Type[BaseException]):
535536
# Sending many requests to the same url. All of them but one will have PoolTimeout. One will
536537
# be finished with ReadTimeout
537538
nursery.start_soon(fetch, pool, PoolTimeout, ReadTimeout)
538-
if pool.connections: # There is one connection in pool in "CONNECTING" state
539-
assert pool.connections[0].is_connecting()
540-
with pytest.raises(ReadTimeout): # ReadTimeout indicates that connection could be retrieved
539+
assert pool.connections[0].is_connecting() if pool.connections else True
540+
with pytest.raises(
541+
ReadTimeout
542+
): # ReadTimeout indicates that connection could be retrieved
541543
await fetch(pool)
542544

543545

544-
545546
@pytest.mark.trio
546547
async def test_pool_timeout_connection_cleanup():
547548
"""
@@ -555,7 +556,8 @@ async def test_pool_timeout_connection_cleanup():
555556
b"Content-Length: 13\r\n",
556557
b"\r\n",
557558
b"Hello, world!",
558-
] * 2,
559+
]
560+
* 2,
559561
)
560562

561563
async with AsyncConnectionPool(
@@ -568,12 +570,18 @@ async def test_pool_timeout_connection_cleanup():
568570
"write": 0.1,
569571
}
570572
with contextlib.suppress(PoolTimeout):
571-
await pool.request("GET", "https://example.com/", extensions={"timeout": timeout})
573+
await pool.request(
574+
"GET", "https://example.com/", extensions={"timeout": timeout}
575+
)
572576

573577
# wait for a considerable amount of time to make sure all requests time out
574578
await concurrency.sleep(0.1)
575579

576-
await pool.request("GET", "https://example.com/", extensions={"timeout": {**timeout, 'pool': 0.1}})
580+
await pool.request(
581+
"GET",
582+
"https://example.com/",
583+
extensions={"timeout": {**timeout, "pool": 0.1}},
584+
)
577585

578586
if pool.connections:
579587
for conn in pool.connections:

tests/_sync/test_connection_pool.py

+34-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import contextlib
2-
import time
32
from typing import List, Optional, Type
43

54
import pytest
5+
from tests import concurrency
66

77
from httpcore import (
88
ConnectionPool,
99
ConnectError,
1010
PoolTimeout,
11-
ReadTimeout,
1211
ReadError,
12+
ReadTimeout,
1313
UnsupportedProtocol,
1414
)
1515
from httpcore.backends.base import NetworkStream
16-
from httpcore.backends.mock import MockBackend, HangingStream
17-
from tests import concurrency
16+
from httpcore.backends.mock import HangingStream, MockBackend
17+
1818

1919

2020
def test_connection_pool_with_keepalive():
@@ -511,21 +511,22 @@ def test_pool_under_load():
511511
"""
512512
network_backend = MockBackend([], resp_stream_cls=HangingStream)
513513

514-
def fetch(_pool: ConnectionPool, *exceptions: Type[BaseException]):
514+
def fetch(
515+
_pool: ConnectionPool, *exceptions: Type[BaseException]
516+
) -> None:
515517
with contextlib.suppress(*exceptions):
516-
with pool.stream(
517-
"GET",
518-
"http://a.com/",
519-
extensions={
520-
"timeout": {
521-
"connect": 0.1,
522-
"read": 0.1,
523-
"pool": 0.1,
524-
"write": 0.1,
525-
},
518+
pool.request(
519+
"GET",
520+
"http://a.com/",
521+
extensions={
522+
"timeout": {
523+
"connect": 0.1,
524+
"read": 0.1,
525+
"pool": 0.1,
526+
"write": 0.1,
526527
},
527-
) as response:
528-
response.read()
528+
},
529+
)
529530

530531
with ConnectionPool(
531532
max_connections=1, network_backend=network_backend
@@ -535,9 +536,10 @@ def fetch(_pool: ConnectionPool, *exceptions: Type[BaseException]):
535536
# Sending many requests to the same url. All of them but one will have PoolTimeout. One will
536537
# be finished with ReadTimeout
537538
nursery.start_soon(fetch, pool, PoolTimeout, ReadTimeout)
538-
if pool.connections: # There is one connection in pool in "CONNECTING" state
539-
assert pool.connections[0].is_connecting()
540-
with pytest.raises(ReadTimeout): # ReadTimeout indicates that connection could be retrieved
539+
assert pool.connections[0].is_connecting() if pool.connections else True
540+
with pytest.raises(
541+
ReadTimeout
542+
): # ReadTimeout indicates that connection could be retrieved
541543
fetch(pool)
542544

543545

@@ -554,11 +556,12 @@ def test_pool_timeout_connection_cleanup():
554556
b"Content-Length: 13\r\n",
555557
b"\r\n",
556558
b"Hello, world!",
557-
] * 2,
559+
]
560+
* 2,
558561
)
559562

560563
with ConnectionPool(
561-
network_backend=network_backend, max_connections=2
564+
network_backend=network_backend, max_connections=1
562565
) as pool:
563566
timeout = {
564567
"connect": 0.1,
@@ -567,12 +570,18 @@ def test_pool_timeout_connection_cleanup():
567570
"write": 0.1,
568571
}
569572
with contextlib.suppress(PoolTimeout):
570-
pool.request("GET", "https://example.com/", extensions={"timeout": timeout})
573+
pool.request(
574+
"GET", "https://example.com/", extensions={"timeout": timeout}
575+
)
571576

572577
# wait for a considerable amount of time to make sure all requests time out
573-
time.sleep(0.1)
578+
concurrency.sleep(0.1)
574579

575-
pool.request("GET", "https://example.com/", extensions={"timeout": {**timeout, 'pool': 0.1}})
580+
pool.request(
581+
"GET",
582+
"https://example.com/",
583+
extensions={"timeout": {**timeout, "pool": 0.1}},
584+
)
576585

577586
if pool.connections:
578587
for conn in pool.connections:

tests/concurrency.py

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
childen, because we don't need that for our use-case.
1010
"""
1111
import threading
12+
import time
1213
from types import TracebackType
1314
from typing import Any, Callable, List, Optional, Type
1415

@@ -38,3 +39,7 @@ def start_soon(self, func: Callable[..., object], *args: Any) -> None:
3839

3940
def open_nursery() -> Nursery:
4041
return Nursery()
42+
43+
44+
def sleep(seconds: float) -> None:
45+
time.sleep(seconds)

0 commit comments

Comments
 (0)