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

Commitf229f7f

Browse files
fix: results returned byattributes property to show updates
Previously the `attributes` method would show the original values in aGitlab Object even if they had been updated. Correct this so that theupdated value will be returned.Also use copy.deepcopy() to ensure that modifying the dictionary returned cannot also modify the object.
1 parent7220c55 commitf229f7f

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

‎docs/api-usage.rst‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,9 @@ the dictionary will have no impact on the GitLab Object. This can also be used
197197
to create a JSON representation of the object. There are two ways to retrieve a
198198
dictionary representation of the Gitlab Object.
199199

200-
* `asdict()` method. Returns a dictionary with updated attributes having precedence.
201-
* `attributes` property. Returns a dictionary with original attributes having
202-
precedence and then updated attributes. Also returns any relevant parent object
203-
attributes.
200+
* `asdict()` method. Returns a dictionary representation of the Gitlab object.
201+
* `attributes` property. Returns a dictionary representation of the Gitlab object.
202+
Also returns any relevant parent object attributes.
204203

205204
..note::
206205

‎gitlab/base.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def __setattr__(self, name: str, value: Any) -> None:
146146
self.__dict__["_updated_attrs"][name]=value
147147

148148
defasdict(self)->Dict[str,Any]:
149-
data=copy.deepcopy(self._attrs)
149+
data= {}
150+
data.update(copy.deepcopy(self._attrs))
150151
data.update(copy.deepcopy(self._updated_attrs))
151152
returndata
152153

@@ -231,10 +232,10 @@ def encoded_id(self) -> Optional[Union[int, str]]:
231232

232233
@property
233234
defattributes(self)->Dict[str,Any]:
234-
d=self.__dict__["_updated_attrs"].copy()
235-
d.update(self.__dict__["_attrs"])
236-
d.update(self.__dict__["_parent_attrs"])
237-
returnd
235+
data={}
236+
data.update(copy.deepcopy(self._parent_attrs))
237+
data.update(copy.deepcopy(self.asdict()))
238+
returndata
238239

239240

240241
classRESTObjectList:

‎tests/unit/test_base.py‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,20 @@ def test_attributes(self, fake_manager):
284284
result=fake_object.attributes
285285
assertresult== {"attr1": [1,2,3]}
286286

287-
# Updated attribute value isnotreflected in `attributes`
287+
# Updated attribute value is reflected in `attributes`
288288
fake_object.attr1="hello"
289-
assertfake_object.attributes== {"attr1":[1,2,3]}
289+
assertfake_object.attributes== {"attr1":"hello"}
290290
assertfake_object.attr1=="hello"
291291
# New attribute is in `attributes`
292292
fake_object.new_attrib="spam"
293-
assertfake_object.attributes== {"attr1":[1,2,3],"new_attrib":"spam"}
293+
assertfake_object.attributes== {"attr1":"hello","new_attrib":"spam"}
294294

295-
# Modifying the dictionary can cause modification to the object :(
295+
# Modifying the dictionary does not cause modifications to the object
296+
fake_object=FakeObject(fake_manager, {"attr1": [1,2,3]})
296297
result=fake_object.attributes
297298
result["attr1"].append(10)
298-
assertresult== {"attr1": [1,2,3,10],"new_attrib":"spam"}
299-
assertfake_object.attributes== {"attr1": [1,2,3,10],"new_attrib":"spam"}
300-
assertfake_object.attr1=="hello"
301-
299+
assertresult== {"attr1": [1,2,3,10]}
300+
assertfake_object.attributes== {"attr1": [1,2,3]}
302301

303302
deftest_asdict_vs_attributes(self,fake_manager):
304303
fake_object=FakeObject(fake_manager, {"attr1":"foo"})
@@ -310,10 +309,6 @@ def test_asdict_vs_attributes(self, fake_manager):
310309
assertfake_object.attributes==fake_object.asdict()
311310
fake_object.attr2="eggs"
312311
assertfake_object.attributes==fake_object.asdict()
313-
# Update attribute, returndifferent result
312+
# Update attribute, returnsame result
314313
fake_object.attr1="hello"
315-
assertfake_object.attributes!=fake_object.asdict()
316-
# asdict() returns the updated value
317-
assertfake_object.asdict()== {"attr1":"hello","attr2":"eggs"}
318-
# `attributes` returns original value
319-
assertfake_object.attributes== {"attr1":"foo","attr2":"eggs"}
314+
assertfake_object.attributes==fake_object.asdict()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp