Skip to content

Commit e8a3585

Browse files
authored
Merge pull request #860 from lachmanfrantisek/fix-mutable-default-arguments
Fix mutable default arguments
2 parents edb3359 + 6e204ce commit e8a3585

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

gitlab/__init__.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def http_request(
443443
self,
444444
verb,
445445
path,
446-
query_data={},
446+
query_data=None,
447447
post_data=None,
448448
streamed=False,
449449
files=None,
@@ -469,7 +469,7 @@ def http_request(
469469
Raises:
470470
GitlabHttpError: When the return code is not 2xx
471471
"""
472-
472+
query_data = query_data or {}
473473
url = self._build_url(path)
474474

475475
params = {}
@@ -564,7 +564,7 @@ def http_request(
564564
response_body=result.content,
565565
)
566566

567-
def http_get(self, path, query_data={}, streamed=False, raw=False, **kwargs):
567+
def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
568568
"""Make a GET request to the Gitlab server.
569569
570570
Args:
@@ -584,6 +584,7 @@ def http_get(self, path, query_data={}, streamed=False, raw=False, **kwargs):
584584
GitlabHttpError: When the return code is not 2xx
585585
GitlabParsingError: If the json data could not be parsed
586586
"""
587+
query_data = query_data or {}
587588
result = self.http_request(
588589
"get", path, query_data=query_data, streamed=streamed, **kwargs
589590
)
@@ -602,7 +603,7 @@ def http_get(self, path, query_data={}, streamed=False, raw=False, **kwargs):
602603
else:
603604
return result
604605

605-
def http_list(self, path, query_data={}, as_list=None, **kwargs):
606+
def http_list(self, path, query_data=None, as_list=None, **kwargs):
606607
"""Make a GET request to the Gitlab server for list-oriented queries.
607608
608609
Args:
@@ -623,6 +624,7 @@ def http_list(self, path, query_data={}, as_list=None, **kwargs):
623624
GitlabHttpError: When the return code is not 2xx
624625
GitlabParsingError: If the json data could not be parsed
625626
"""
627+
query_data = query_data or {}
626628

627629
# In case we want to change the default behavior at some point
628630
as_list = True if as_list is None else as_list
@@ -640,7 +642,7 @@ def http_list(self, path, query_data={}, as_list=None, **kwargs):
640642
# No pagination, generator requested
641643
return GitlabList(self, url, query_data, **kwargs)
642644

643-
def http_post(self, path, query_data={}, post_data={}, files=None, **kwargs):
645+
def http_post(self, path, query_data=None, post_data=None, files=None, **kwargs):
644646
"""Make a POST request to the Gitlab server.
645647
646648
Args:
@@ -660,6 +662,9 @@ def http_post(self, path, query_data={}, post_data={}, files=None, **kwargs):
660662
GitlabHttpError: When the return code is not 2xx
661663
GitlabParsingError: If the json data could not be parsed
662664
"""
665+
query_data = query_data or {}
666+
post_data = post_data or {}
667+
663668
result = self.http_request(
664669
"post",
665670
path,
@@ -675,7 +680,7 @@ def http_post(self, path, query_data={}, post_data={}, files=None, **kwargs):
675680
raise GitlabParsingError(error_message="Failed to parse the server message")
676681
return result
677682

678-
def http_put(self, path, query_data={}, post_data={}, files=None, **kwargs):
683+
def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
679684
"""Make a PUT request to the Gitlab server.
680685
681686
Args:
@@ -694,6 +699,9 @@ def http_put(self, path, query_data={}, post_data={}, files=None, **kwargs):
694699
GitlabHttpError: When the return code is not 2xx
695700
GitlabParsingError: If the json data could not be parsed
696701
"""
702+
query_data = query_data or {}
703+
post_data = post_data or {}
704+
697705
result = self.http_request(
698706
"put",
699707
path,
@@ -755,7 +763,8 @@ def __init__(self, gl, url, query_data, get_next=True, **kwargs):
755763
self._query(url, query_data, **kwargs)
756764
self._get_next = get_next
757765

758-
def _query(self, url, query_data={}, **kwargs):
766+
def _query(self, url, query_data=None, **kwargs):
767+
query_data = query_data or {}
759768
result = self._gl.http_request("get", url, query_data=query_data, **kwargs)
760769
try:
761770
self._next_url = result.links["next"]["url"]

gitlab/mixins.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def _get_update_method(self):
240240
return http_method
241241

242242
@exc.on_http_error(exc.GitlabUpdateError)
243-
def update(self, id=None, new_data={}, **kwargs):
243+
def update(self, id=None, new_data=None, **kwargs):
244244
"""Update an object on the server.
245245
246246
Args:
@@ -255,6 +255,7 @@ def update(self, id=None, new_data={}, **kwargs):
255255
GitlabAuthenticationError: If authentication is not correct
256256
GitlabUpdateError: If the server cannot perform the request
257257
"""
258+
new_data = new_data or {}
258259

259260
if id is None:
260261
path = self.path

gitlab/v4/objects.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ class ApplicationSettingsManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
530530
)
531531

532532
@exc.on_http_error(exc.GitlabUpdateError)
533-
def update(self, id=None, new_data={}, **kwargs):
533+
def update(self, id=None, new_data=None, **kwargs):
534534
"""Update an object on the server.
535535
536536
Args:
@@ -545,7 +545,7 @@ def update(self, id=None, new_data={}, **kwargs):
545545
GitlabAuthenticationError: If authentication is not correct
546546
GitlabUpdateError: If the server cannot perform the request
547547
"""
548-
548+
new_data = new_data or {}
549549
data = new_data.copy()
550550
if "domain_whitelist" in data and data["domain_whitelist"] is None:
551551
data.pop("domain_whitelist")
@@ -865,13 +865,14 @@ class GroupLabelManager(ListMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTMa
865865
_update_attrs = (("name",), ("new_name", "color", "description", "priority"))
866866

867867
# Update without ID.
868-
def update(self, name, new_data={}, **kwargs):
868+
def update(self, name, new_data=None, **kwargs):
869869
"""Update a Label on the server.
870870
871871
Args:
872872
name: The name of the label
873873
**kwargs: Extra options to send to the server (e.g. sudo)
874874
"""
875+
new_data = new_data or {}
875876
new_data["name"] = name
876877
super().update(id=None, new_data=new_data, **kwargs)
877878

@@ -2489,7 +2490,7 @@ class ProjectMergeRequestApprovalManager(GetWithoutIdMixin, UpdateMixin, RESTMan
24892490
_update_uses_post = True
24902491

24912492
@exc.on_http_error(exc.GitlabUpdateError)
2492-
def set_approvers(self, approver_ids=[], approver_group_ids=[], **kwargs):
2493+
def set_approvers(self, approver_ids=None, approver_group_ids=None, **kwargs):
24932494
"""Change MR-level allowed approvers and approver groups.
24942495
24952496
Args:
@@ -2500,6 +2501,9 @@ def set_approvers(self, approver_ids=[], approver_group_ids=[], **kwargs):
25002501
GitlabAuthenticationError: If authentication is not correct
25012502
GitlabUpdateError: If the server failed to perform the request
25022503
"""
2504+
approver_ids = approver_ids or []
2505+
approver_group_ids = approver_group_ids or []
2506+
25032507
path = "%s/%s/approvers" % (self._parent.manager.path, self._parent.get_id())
25042508
data = {"approver_ids": approver_ids, "approver_group_ids": approver_group_ids}
25052509
self.gitlab.http_put(path, post_data=data, **kwargs)
@@ -2994,13 +2998,14 @@ class ProjectLabelManager(
29942998
_update_attrs = (("name",), ("new_name", "color", "description", "priority"))
29952999

29963000
# Update without ID.
2997-
def update(self, name, new_data={}, **kwargs):
3001+
def update(self, name, new_data=None, **kwargs):
29983002
"""Update a Label on the server.
29993003
30003004
Args:
30013005
name: The name of the label
30023006
**kwargs: Extra options to send to the server (e.g. sudo)
30033007
"""
3008+
new_data = new_data or {}
30043009
new_data["name"] = name
30053010
super().update(id=None, new_data=new_data, **kwargs)
30063011

@@ -3130,7 +3135,7 @@ def create(self, data, **kwargs):
31303135
return self._obj_cls(self, server_data)
31313136

31323137
@exc.on_http_error(exc.GitlabUpdateError)
3133-
def update(self, file_path, new_data={}, **kwargs):
3138+
def update(self, file_path, new_data=None, **kwargs):
31343139
"""Update an object on the server.
31353140
31363141
Args:
@@ -3145,7 +3150,7 @@ def update(self, file_path, new_data={}, **kwargs):
31453150
GitlabAuthenticationError: If authentication is not correct
31463151
GitlabUpdateError: If the server cannot perform the request
31473152
"""
3148-
3153+
new_data = new_data or {}
31493154
data = new_data.copy()
31503155
file_path = file_path.replace("/", "%2F")
31513156
data["file_path"] = file_path
@@ -3632,7 +3637,7 @@ def get(self, id, **kwargs):
36323637
obj.id = id
36333638
return obj
36343639

3635-
def update(self, id=None, new_data={}, **kwargs):
3640+
def update(self, id=None, new_data=None, **kwargs):
36363641
"""Update an object on the server.
36373642
36383643
Args:
@@ -3647,6 +3652,7 @@ def update(self, id=None, new_data={}, **kwargs):
36473652
GitlabAuthenticationError: If authentication is not correct
36483653
GitlabUpdateError: If the server cannot perform the request
36493654
"""
3655+
new_data = new_data or {}
36503656
super(ProjectServiceManager, self).update(id, new_data, **kwargs)
36513657
self.id = id
36523658

@@ -3689,7 +3695,7 @@ class ProjectApprovalManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
36893695
_update_uses_post = True
36903696

36913697
@exc.on_http_error(exc.GitlabUpdateError)
3692-
def set_approvers(self, approver_ids=[], approver_group_ids=[], **kwargs):
3698+
def set_approvers(self, approver_ids=None, approver_group_ids=None, **kwargs):
36933699
"""Change project-level allowed approvers and approver groups.
36943700
36953701
Args:
@@ -3700,6 +3706,8 @@ def set_approvers(self, approver_ids=[], approver_group_ids=[], **kwargs):
37003706
GitlabAuthenticationError: If authentication is not correct
37013707
GitlabUpdateError: If the server failed to perform the request
37023708
"""
3709+
approver_ids = approver_ids or []
3710+
approver_group_ids = approver_group_ids or []
37033711

37043712
path = "/projects/%s/approvers" % self._parent.get_id()
37053713
data = {"approver_ids": approver_ids, "approver_group_ids": approver_group_ids}
@@ -4182,7 +4190,7 @@ def unshare(self, group_id, **kwargs):
41824190
# variables not supported in CLI
41834191
@cli.register_custom_action("Project", ("ref", "token"))
41844192
@exc.on_http_error(exc.GitlabCreateError)
4185-
def trigger_pipeline(self, ref, token, variables={}, **kwargs):
4193+
def trigger_pipeline(self, ref, token, variables=None, **kwargs):
41864194
"""Trigger a CI build.
41874195
41884196
See https://gitlab.com/help/ci/triggers/README.md#trigger-a-build
@@ -4197,6 +4205,7 @@ def trigger_pipeline(self, ref, token, variables={}, **kwargs):
41974205
GitlabAuthenticationError: If authentication is not correct
41984206
GitlabCreateError: If the server failed to perform the request
41994207
"""
4208+
variables = variables or {}
42004209
path = "/projects/%s/trigger/pipeline" % self.get_id()
42014210
post_data = {"ref": ref, "token": token, "variables": variables}
42024211
attrs = self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)

0 commit comments

Comments
 (0)