Skip to content

Commit 8e85791

Browse files
chore: add a UserWarning if both iterator=True and page=X are used (python-gitlab#2462)
If a caller calls a `list()` method with both `iterator=True` (or `as_list=False`) and `page=X` then emit a `UserWarning` as the options are mutually exclusive.
1 parent 585e3a8 commit 8e85791

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

gitlab/client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,19 @@ def http_list(
929929

930930
page = kwargs.get("page")
931931

932+
if iterator and page is not None:
933+
arg_used_message = f"iterator={iterator}"
934+
if as_list is not None:
935+
arg_used_message = f"as_list={as_list}"
936+
utils.warn(
937+
message=(
938+
f"`{arg_used_message}` and `page={page}` were both specified. "
939+
f"`{arg_used_message}` will be ignored and a `list` will be "
940+
f"returned."
941+
),
942+
category=UserWarning,
943+
)
944+
932945
if iterator and page is None:
933946
# Generator requested
934947
return GitlabList(self, url, query_data, **kwargs)

tests/unit/test_gitlab_http_methods.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,22 @@ def test_list_request_page_and_iterator(gl):
548548
response_dict["match"] = [responses.matchers.query_param_matcher({"page": "1"})]
549549
responses.add(**response_dict)
550550

551-
result = gl.http_list("/projects", iterator=True, page=1)
551+
with pytest.warns(
552+
UserWarning, match="`iterator=True` and `page=1` were both specified"
553+
):
554+
result = gl.http_list("/projects", iterator=True, page=1)
552555
assert isinstance(result, list)
553556
assert len(result) == 20
554557
assert len(responses.calls) == 1
555558

559+
with pytest.warns(
560+
UserWarning, match="`as_list=False` and `page=1` were both specified"
561+
):
562+
result = gl.http_list("/projects", as_list=False, page=1)
563+
assert isinstance(result, list)
564+
assert len(result) == 20
565+
assert len(responses.calls) == 2
566+
556567

557568
large_list_response = {
558569
"method": responses.GET,

0 commit comments

Comments
 (0)