Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Nikitin committed Apr 4, 2024
1 parent 28f1fd8 commit d3c6951
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
21 changes: 13 additions & 8 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ def __init__(
max_field_size: int = 8190,
fallback_charset_resolver: _CharsetResolver = lambda r, b: "utf-8",
) -> None:
if timeout is sentinel or timeout is None:
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
if base_url is None or isinstance(base_url, URL):
self._base_url: Optional[URL] = base_url
else:
Expand Down Expand Up @@ -281,14 +289,7 @@ def __init__(
self._default_auth = auth
self._version = version
self._json_serialize = json_serialize
if timeout is sentinel or timeout is None:
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 Expand Up @@ -325,6 +326,10 @@ def __init_subclass__(cls: Type["ClientSession"]) -> None:
)

def __del__(self, _warnings: Any = warnings) -> None:
if not hasattr(self, 'closed'):
# has not been initialized, nothing to do
return

if not self.closed:
_warnings.warn(
f"Unclosed client session {self!r}",
Expand Down
10 changes: 10 additions & 0 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,13 @@ async def test_build_url_returns_expected_url(
) -> None:
session = await create_session(base_url)
assert session._build_url(url) == expected_url


async def test_instantiation_with_invalid_timeout_value(loop):
loop.set_debug(False)
logs = []
loop.set_exception_handler(lambda loop, ctx: logs.append(ctx))
with pytest.raises(ValueError, match="timeout parameter cannot be .*"):
ClientSession(timeout=1)
# should not have "Unclosed client session" warning
assert not logs

0 comments on commit d3c6951

Please sign in to comment.