Skip to content

Commit 6ddcd59

Browse files
Validate polling status callbacks return strings
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 264ca18 commit 6ddcd59

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

hyperbrowser/client/polling.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def _ensure_boolean_terminal_result(result: object, *, operation_name: str) -> b
5454
return result
5555

5656

57+
def _ensure_status_string(status: object, *, operation_name: str) -> str:
58+
if not isinstance(status, str):
59+
raise HyperbrowserError(f"get_status must return a string for {operation_name}")
60+
return status
61+
62+
5763
def _validate_retry_config(
5864
*,
5965
max_attempts: int,
@@ -147,7 +153,7 @@ def poll_until_terminal_status(
147153

148154
while True:
149155
try:
150-
status = get_status()
156+
status = _ensure_status_string(get_status(), operation_name=operation_name)
151157
failures = 0
152158
except Exception as exc:
153159
failures += 1
@@ -220,7 +226,9 @@ async def poll_until_terminal_status_async(
220226

221227
while True:
222228
try:
223-
status = await get_status()
229+
status = _ensure_status_string(
230+
await get_status(), operation_name=operation_name
231+
)
224232
failures = 0
225233
except Exception as exc:
226234
failures += 1

tests/test_polling.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,14 @@ def test_polling_helpers_validate_retry_and_interval_configuration():
772772
poll_interval_seconds=0.0,
773773
max_wait_seconds=1.0,
774774
)
775+
with pytest.raises(HyperbrowserError, match="get_status must return a string"):
776+
poll_until_terminal_status(
777+
operation_name="invalid-status-value",
778+
get_status=lambda: 123, # type: ignore[return-value]
779+
is_terminal_status=lambda value: value == "completed",
780+
poll_interval_seconds=0.0,
781+
max_wait_seconds=1.0,
782+
)
775783

776784
with pytest.raises(
777785
HyperbrowserError, match="max_wait_seconds must be non-negative"
@@ -878,6 +886,14 @@ async def validate_async_operation_name() -> None:
878886
poll_interval_seconds=0.0,
879887
max_wait_seconds=1.0,
880888
)
889+
with pytest.raises(HyperbrowserError, match="get_status must return a string"):
890+
await poll_until_terminal_status_async(
891+
operation_name="invalid-status-value-async",
892+
get_status=lambda: asyncio.sleep(0, result=123), # type: ignore[arg-type]
893+
is_terminal_status=lambda value: value == "completed",
894+
poll_interval_seconds=0.0,
895+
max_wait_seconds=1.0,
896+
)
881897
with pytest.raises(
882898
HyperbrowserError, match="operation_name must be 200 characters or fewer"
883899
):

0 commit comments

Comments
 (0)