Skip to content

Commit 0975678

Browse files
feat: add an asdict() method to the Gitlab Objects
Add an `asdict()` method that returns a dictionary representation copy of the Gitlab Object. This is a copy and changes made to it will have no impact on the Gitlab Object. The `asdict()` method name was chosen as both the `dataclasses` and `attrs` libraries have an `asdict()` function which has the similar purpose of creating a dictionary represenation of an object.
1 parent 64d01ef commit 0975678

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

docs/api-usage.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ You can print a Gitlab Object. For example:
192192
# Or explicitly via `pformat()`. This is equivalent to the above.
193193
print(project.pformat())
194194
195+
You can get a dictionary representation copy of the Gitlab Object. Modifications made to
196+
the dictionary will have no impact on the GitLab Object.
197+
198+
.. code-block:: python
199+
200+
project = gl.projects.get(1)
201+
project_dict = project.asdict()
202+
195203
196204
Base types
197205
==========

gitlab/base.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
import copy
1819
import importlib
1920
import pprint
2021
import textwrap
@@ -144,15 +145,16 @@ def __getattr__(self, name: str) -> Any:
144145
def __setattr__(self, name: str, value: Any) -> None:
145146
self.__dict__["_updated_attrs"][name] = value
146147

147-
def __str__(self) -> str:
148-
data = self._attrs.copy()
148+
def asdict(self) -> Dict[str, Any]:
149+
data = copy.deepcopy(self._attrs)
149150
data.update(self._updated_attrs)
150-
return f"{type(self)} => {data}"
151+
return data
152+
153+
def __str__(self) -> str:
154+
return f"{type(self)} => {self.asdict()}"
151155

152156
def pformat(self) -> str:
153-
data = self._attrs.copy()
154-
data.update(self._updated_attrs)
155-
return f"{type(self)} => \n{pprint.pformat(data)}"
157+
return f"{type(self)} => \n{pprint.pformat(self.asdict())}"
156158

157159
def pprint(self) -> None:
158160
print(self.pformat())

tests/unit/test_base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,13 @@ def test_pprint(self, capfd, fake_manager):
249249
" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n"
250250
)
251251
assert stderr == ""
252+
253+
def test_asdict(self, fake_manager):
254+
fake_object = FakeObject(fake_manager, {"attr1": "foo"})
255+
assert fake_object.attr1 == "foo"
256+
result = fake_object.asdict()
257+
assert result == {"attr1": "foo"}
258+
259+
result["attr1"] = "testing"
260+
assert result == {"attr1": "testing"}
261+
assert fake_object.attr1 == "foo"

0 commit comments

Comments
 (0)