Skip to content

Commit

Permalink
Add runtime type check for ClientSession timeout param (#8022)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorvoltaic committed Jan 12, 2024
1 parent 63bea9d commit 43f3e23
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES/8021.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add runtime type check for ``ClientSession`` ``timeout`` parameter.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Hugo Hromic
Hugo van Kemenade
Hynek Schlawack
Igor Alexandrov
Igor Bolshakov
Igor Davydenko
Igor Mozharovsky
Igor Pavlov
Expand Down
10 changes: 7 additions & 3 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,13 @@ def __init__(
self._version = version
self._json_serialize = json_serialize
if timeout is sentinel or timeout is None:
self._timeout = DEFAULT_TIMEOUT
else:
self._timeout = timeout
timeout = DEFAULT_TIMEOUT
if not isinstance(timeout, ClientTimeout):
raise ValueError(
f"timeout parameter cannot be of {type(timeout)} type, "
"please use 'timeout=ClientTimeout(...)'",
)
self._timeout = timeout
self._raise_for_status = raise_for_status
self._auto_decompress = auto_decompress
self._trust_env = trust_env
Expand Down
13 changes: 7 additions & 6 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,6 @@ async def test_client_session_timeout_default_args(loop: Any) -> None:
await session1.close()


async def test_client_session_timeout_argument() -> None:
session = ClientSession(timeout=500)
assert session.timeout == 500
await session.close()


async def test_client_session_timeout_zero() -> None:
timeout = client.ClientTimeout(total=10, connect=0, sock_connect=0, sock_read=0)
try:
Expand All @@ -794,6 +788,13 @@ async def test_client_session_timeout_zero() -> None:
pytest.fail("0 should disable timeout.")


async def test_client_session_timeout_bad_argument() -> None:
with pytest.raises(ValueError):
ClientSession(timeout="test_bad_argumnet")
with pytest.raises(ValueError):
ClientSession(timeout=100)


async def test_requote_redirect_url_default() -> None:
session = ClientSession()
assert session.requote_redirect_url
Expand Down

0 comments on commit 43f3e23

Please sign in to comment.