Skip to content

chore: add pprint() and pformat() methods to RESTObject #1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/api-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
==========

Expand Down
9 changes: 9 additions & 0 deletions gitlab/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import importlib
import pprint
import textwrap
from types import ModuleType
from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type
Expand Down Expand Up @@ -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()}>"
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) == (
"<class 'tests.unit.test_base.FakeObject'> => {'attr1': 'foo'}"
)

def test_pformat(self, fake_manager):
fake_object = FakeObject(
fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15}
)
assert fake_object.pformat() == (
"<class 'tests.unit.test_base.FakeObject'> => "
"\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 == (
"<class 'tests.unit.test_base.FakeObject'> => "
"\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n"
" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n"
)
assert stderr == ""