Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0cbddce

Browse files
feat: emit a warning when using alist() method returns max
A common cause of issues filed and questions raised is that a userwill call a `list()` method and only get 20 items. As this is thedefault maximum of items that will be returned from a `list()` method.To help with this we now emit a warning when the result from a`list()` method is greater-than or equal to 20 (or the specified`per_page` value) and the user is not using either `all=True`,`as_list=False`, or `page=X`.
1 parent64d01ef commit0cbddce

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

‎gitlab/client.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
importos
2020
importtime
21+
importwarnings
2122
fromtypingimportAny,cast,Dict,List,Optional,Tuple,TYPE_CHECKING,Union
2223

2324
importrequests
2425
importrequests.utils
2526
fromrequests_toolbelt.multipart.encoderimportMultipartEncoder# type: ignore
2627

28+
importgitlab
2729
importgitlab.config
2830
importgitlab.const
2931
importgitlab.exceptions
@@ -35,6 +37,14 @@
3537
"{source!r} to {target!r}"
3638
)
3739

40+
# https://docs.gitlab.com/ee/api/#offset-based-pagination
41+
DEFAULT_PER_PAGE=20
42+
MAXIMUM_PER_PAGE=100
43+
_PAGINATION_URL= (
44+
f"https://python-gitlab.readthedocs.io/en/v{gitlab.__version__}/"
45+
f"api-usage.html#pagination"
46+
)
47+
3848

3949
classGitlab:
4050
"""Represents a GitLab server connection.
@@ -816,9 +826,22 @@ def http_list(
816826
ifget_allisTrueandas_listisTrue:
817827
returnlist(GitlabList(self,url,query_data,**kwargs))
818828

829+
per_page=min(kwargs.get("per_page",DEFAULT_PER_PAGE),MAXIMUM_PER_PAGE)
819830
ifpageoras_listisTrue:
820831
# pagination requested, we return a list
821-
returnlist(GitlabList(self,url,query_data,get_next=False,**kwargs))
832+
items=list(GitlabList(self,url,query_data,get_next=False,**kwargs))
833+
ifpageisNoneandlen(items)>=per_page:
834+
# Warn the user that they are only going to retrieve `per_page` maximum
835+
# items. This is a common cause of issues filed.
836+
warnings.warn(
837+
(
838+
f"Calling a `list()` method without specifying `all=True` or "
839+
f"`as_list=False` will return a maximum of{per_page} items. "
840+
f"See{_PAGINATION_URL} for more details"
841+
),
842+
UserWarning,
843+
)
844+
returnitems
822845

823846
# No pagination, generator requested
824847
returnGitlabList(self,url,query_data,**kwargs)

‎tests/unit/test_gitlab_http_methods.py‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
importwarnings
2+
13
importpytest
24
importrequests
35
importresponses
@@ -329,20 +331,83 @@ def test_list_request(gl):
329331
match=MATCH_EMPTY_QUERY_PARAMS,
330332
)
331333

332-
result=gl.http_list("/projects",as_list=True)
334+
withwarnings.catch_warnings(record=True)ascaught_warnings:
335+
result=gl.http_list("/projects",as_list=True)
336+
assertlen(caught_warnings)==0
333337
assertisinstance(result,list)
334338
assertlen(result)==1
335339

336340
result=gl.http_list("/projects",as_list=False)
337341
assertisinstance(result,GitlabList)
338-
assertlen(result)==1
342+
assertlen(list(result))==1
339343

340344
result=gl.http_list("/projects",all=True)
341345
assertisinstance(result,list)
342346
assertlen(result)==1
343347
assertresponses.assert_call_count(url,3)isTrue
344348

345349

350+
@responses.activate
351+
deftest_list_request_pagination_warning(gl):
352+
url="http://localhost/api/v4/projects"
353+
responses.add(
354+
method=responses.GET,
355+
url=url,
356+
json=[
357+
{"name":"project01"},
358+
{"name":"project02"},
359+
{"name":"project03"},
360+
{"name":"project04"},
361+
{"name":"project05"},
362+
{"name":"project06"},
363+
{"name":"project07"},
364+
{"name":"project08"},
365+
{"name":"project09"},
366+
{"name":"project10"},
367+
{"name":"project11"},
368+
{"name":"project12"},
369+
{"name":"project13"},
370+
{"name":"project14"},
371+
{"name":"project15"},
372+
{"name":"project16"},
373+
{"name":"project17"},
374+
{"name":"project18"},
375+
{"name":"project19"},
376+
{"name":"project20"},
377+
],
378+
headers={"X-Total":"1"},
379+
status=200,
380+
match=MATCH_EMPTY_QUERY_PARAMS,
381+
)
382+
383+
withwarnings.catch_warnings(record=True)ascaught_warnings:
384+
result=gl.http_list("/projects",as_list=True)
385+
assertlen(caught_warnings)==1
386+
warning=caught_warnings[0]
387+
assertisinstance(warning.message,UserWarning)
388+
message=str(caught_warnings[0].message)
389+
assert"Calling"inmessage
390+
assert"return a maximum of"inmessage
391+
assert"readthedocs"inmessage
392+
393+
assertisinstance(result,list)
394+
assertlen(result)==20
395+
396+
withwarnings.catch_warnings(record=True)ascaught_warnings:
397+
result=gl.http_list("/projects",as_list=False)
398+
assertlen(caught_warnings)==0
399+
assertisinstance(result,GitlabList)
400+
assertlen(list(result))==20
401+
402+
withwarnings.catch_warnings(record=True)ascaught_warnings:
403+
result=gl.http_list("/projects",all=True)
404+
assertlen(caught_warnings)==0
405+
406+
assertisinstance(result,list)
407+
assertlen(result)==20
408+
assertresponses.assert_call_count(url,3)isTrue
409+
410+
346411
@responses.activate
347412
deftest_list_request_404(gl):
348413
url="http://localhost/api/v4/not_there"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp