diff --git a/docs/api-usage.rst b/docs/api-usage.rst
index 66e58873a..8befc5633 100644
--- a/docs/api-usage.rst
+++ b/docs/api-usage.rst
@@ -179,6 +179,20 @@ resources. For example:
project = gl.projects.get(1)
project.star()
+You can print a Gitlab Object. For example:
+
+.. code-block:: python
+
+ project = gl.projects.get(1)
+ print(project)
+
+ # Or in a prettier format.
+ project.pprint()
+
+ # Or explicitly via `pformat()`. This is equivalent to the above.
+ print(project.pformat())
+
+
Base types
==========
diff --git a/gitlab/base.py b/gitlab/base.py
index 50f09c596..6a6e99212 100644
--- a/gitlab/base.py
+++ b/gitlab/base.py
@@ -16,6 +16,7 @@
# along with this program. If not, see .
import importlib
+import pprint
import textwrap
from types import ModuleType
from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type
@@ -147,6 +148,14 @@ def __str__(self) -> str:
data.update(self._updated_attrs)
return f"{type(self)} => {data}"
+ def pformat(self) -> str:
+ data = self._attrs.copy()
+ data.update(self._updated_attrs)
+ return f"{type(self)} => \n{pprint.pformat(data)}"
+
+ def pprint(self) -> None:
+ print(self.pformat())
+
def __repr__(self) -> str:
if self._id_attr:
return f"<{self.__class__.__name__} {self._id_attr}:{self.get_id()}>"
diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py
index 3ca020636..fa9f6aa7d 100644
--- a/tests/unit/test_base.py
+++ b/tests/unit/test_base.py
@@ -201,3 +201,33 @@ def test_inequality_no_id(self, fake_manager):
obj1 = FakeObject(fake_manager, {"attr1": "foo"})
obj2 = FakeObject(fake_manager, {"attr1": "bar"})
assert obj1 != obj2
+
+ def test_dunder_str(self, fake_manager):
+ fake_object = FakeObject(fake_manager, {"attr1": "foo"})
+ assert str(fake_object) == (
+ " => {'attr1': 'foo'}"
+ )
+
+ def test_pformat(self, fake_manager):
+ fake_object = FakeObject(
+ fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15}
+ )
+ assert fake_object.pformat() == (
+ " => "
+ "\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n"
+ " 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}"
+ )
+
+ def test_pprint(self, capfd, fake_manager):
+ fake_object = FakeObject(
+ fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15}
+ )
+ result = fake_object.pprint()
+ assert result is None
+ stdout, stderr = capfd.readouterr()
+ assert stdout == (
+ " => "
+ "\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n"
+ " 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n"
+ )
+ assert stderr == ""