Skip to content

Commit 3d93250

Browse files
Validate client timeout input types
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent db10193 commit 3d93250

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

hyperbrowser/client/async_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from numbers import Real
12
from typing import Mapping, Optional
23

34
from ..exceptions import HyperbrowserError
@@ -27,8 +28,11 @@ def __init__(
2728
headers: Optional[Mapping[str, str]] = None,
2829
timeout: Optional[float] = 30,
2930
):
30-
if timeout is not None and timeout < 0:
31-
raise HyperbrowserError("timeout must be non-negative")
31+
if timeout is not None:
32+
if isinstance(timeout, bool) or not isinstance(timeout, Real):
33+
raise HyperbrowserError("timeout must be a number")
34+
if timeout < 0:
35+
raise HyperbrowserError("timeout must be non-negative")
3236
super().__init__(AsyncTransport, config, api_key, base_url, headers)
3337
self.transport.client.timeout = timeout
3438
self.sessions = SessionManager(self)

hyperbrowser/client/sync.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from numbers import Real
12
from typing import Mapping, Optional
23

34
from ..exceptions import HyperbrowserError
@@ -27,8 +28,11 @@ def __init__(
2728
headers: Optional[Mapping[str, str]] = None,
2829
timeout: Optional[float] = 30,
2930
):
30-
if timeout is not None and timeout < 0:
31-
raise HyperbrowserError("timeout must be non-negative")
31+
if timeout is not None:
32+
if isinstance(timeout, bool) or not isinstance(timeout, Real):
33+
raise HyperbrowserError("timeout must be a number")
34+
if timeout < 0:
35+
raise HyperbrowserError("timeout must be non-negative")
3236
super().__init__(SyncTransport, config, api_key, base_url, headers)
3337
self.transport.client.timeout = timeout
3438
self.sessions = SessionManager(self)

tests/test_client_timeout.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,23 @@ async def run() -> None:
2727
await client.close()
2828

2929
asyncio.run(run())
30+
31+
32+
def test_sync_client_rejects_non_numeric_timeout():
33+
with pytest.raises(HyperbrowserError, match="timeout must be a number"):
34+
Hyperbrowser(api_key="test-key", timeout="30") # type: ignore[arg-type]
35+
36+
37+
def test_async_client_rejects_non_numeric_timeout():
38+
with pytest.raises(HyperbrowserError, match="timeout must be a number"):
39+
AsyncHyperbrowser(api_key="test-key", timeout="30") # type: ignore[arg-type]
40+
41+
42+
def test_sync_client_rejects_boolean_timeout():
43+
with pytest.raises(HyperbrowserError, match="timeout must be a number"):
44+
Hyperbrowser(api_key="test-key", timeout=True)
45+
46+
47+
def test_async_client_rejects_boolean_timeout():
48+
with pytest.raises(HyperbrowserError, match="timeout must be a number"):
49+
AsyncHyperbrowser(api_key="test-key", timeout=False)

0 commit comments

Comments
 (0)