Skip to content

Commit 0c8c758

Browse files
Validate page params constructor return type
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent ab35b3b commit 0c8c758

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

hyperbrowser/client/managers/page_params_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ def build_page_batch_params(
2323
if batch_size <= 0:
2424
raise HyperbrowserError("batch_size must be a positive integer")
2525
try:
26-
return cast(T, params_model(page=page, batch_size=batch_size))
26+
params_obj = params_model(page=page, batch_size=batch_size)
2727
except HyperbrowserError:
2828
raise
2929
except Exception as exc:
3030
raise HyperbrowserError(
3131
"Failed to build paginated page params",
3232
original_error=exc,
3333
) from exc
34+
if type(params_obj) is not params_model:
35+
raise HyperbrowserError(
36+
"Paginated page params model constructor returned invalid type"
37+
)
38+
return cast(T, params_obj)

tests/test_page_params_helper_usage.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ def test_paginated_managers_use_shared_page_params_helper():
2222
module_text = Path(module_path).read_text(encoding="utf-8")
2323
assert "build_page_batch_params(" in module_text
2424
assert "batch_size=100" not in module_text
25+
assert "GetBatchScrapeJobParams(page=page" not in module_text
26+
assert "GetCrawlJobParams(page=page" not in module_text
27+
assert "GetBatchFetchJobParams(page=page" not in module_text
28+
assert "GetWebCrawlJobParams(page=page" not in module_text

tests/test_page_params_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,15 @@ def __init__(self, *, page, batch_size): # noqa: ARG002
7474
build_page_batch_params(_BrokenParams, page=1)
7575

7676
assert exc_info.value.original_error is None
77+
78+
79+
def test_build_page_batch_params_rejects_constructor_returning_wrong_type():
80+
class _BrokenParams:
81+
def __new__(cls, *, page, batch_size): # noqa: ARG003
82+
return {"page": page, "batch_size": batch_size}
83+
84+
with pytest.raises(
85+
HyperbrowserError,
86+
match="Paginated page params model constructor returned invalid type",
87+
):
88+
build_page_batch_params(_BrokenParams, page=1)

0 commit comments

Comments
 (0)