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

Commit92da850

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 parent59df432 commit92da850

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

‎gitlab/client.py‎

Lines changed: 28 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,12 @@
3537
"{source!r} to {target!r}"
3638
)
3739

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

3947
classGitlab:
4048
"""Represents a GitLab server connection.
@@ -818,7 +826,26 @@ def http_list(
818826

819827
ifpageoras_listisTrue:
820828
# pagination requested, we return a list
821-
returnlist(GitlabList(self,url,query_data,get_next=False,**kwargs))
829+
gl_list=GitlabList(self,url,query_data,get_next=False,**kwargs)
830+
items=list(gl_list)
831+
if (
832+
pageisNone
833+
and (gl_list.per_pageisnotNoneandlen(items)>=gl_list.per_page)
834+
and (gl_list.totalisNoneorlen(items)<gl_list.total)
835+
):
836+
total_items="10,000+"ifgl_list.totalisNoneelsegl_list.total
837+
# Warn the user that they are only going to retrieve `per_page` maximum
838+
# items. This is a common cause of issues filed.
839+
warnings.warn(
840+
(
841+
f"Calling a `list()` method without specifying `all=True` or "
842+
f"`as_list=False` will return a maximum of{gl_list.per_page} "
843+
f"items. Your query returned{len(items)} of{total_items} "
844+
f"items. See{_PAGINATION_URL} for more details"
845+
),
846+
UserWarning,
847+
)
848+
returnitems
822849

823850
# No pagination, generator requested
824851
returnGitlabList(self,url,query_data,**kwargs)

‎tests/functional/api/test_gitlab.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ def test_template_dockerfile(gl):
8181

8282

8383
deftest_template_gitignore(gl):
84-
assertgl.gitignores.list()
84+
assertgl.gitignores.list(all=True)
8585
gitignore=gl.gitignores.get("Node")
8686
assertgitignore.contentisnotNone
8787

8888

8989
deftest_template_gitlabciyml(gl):
90-
assertgl.gitlabciymls.list()
90+
assertgl.gitlabciymls.list(all=True)
9191
gitlabciyml=gl.gitlabciymls.get("Nodejs")
9292
assertgitlabciyml.contentisnotNone
9393

‎tests/unit/test_gitlab_http_methods.py‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
importwarnings
2+
13
importpytest
24
importrequests
35
importresponses
@@ -324,25 +326,88 @@ def test_list_request(gl):
324326
method=responses.GET,
325327
url=url,
326328
json=[{"name":"project1"}],
327-
headers={"X-Total":"1"},
329+
headers={"X-Total":"1","x-per-page":"20"},
328330
status=200,
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":"30","x-per-page":"20"},
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