Skip to content

Commit 6a48ed7

Browse files
committed
Use hex() instead of str() to build the default unique_id
This makes of a more compact and easier to compare default unique ID. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 4d884b8 commit 6a48ed7

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/frequenz/core/asyncio.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ def __init__(self, *, unique_id: str | None = None) -> None:
105105
106106
Args:
107107
unique_id: The string to uniquely identify this background service instance.
108-
If `None`, `str(id(self))` will be used. This is used in `__repr__` and
109-
`__str__` methods, so it is used mainly for debugging purposes, to
110-
identify a particular instance of a background service.
108+
If `None`, a string based on `hex(id(self))` will be used. This is
109+
used in `__repr__` and `__str__` methods, mainly for debugging
110+
purposes, to identify a particular instance of a background service.
111111
"""
112-
self._unique_id: str = str(id(self)) if unique_id is None else unique_id
112+
# [2:] is used to remove the '0x' prefix from the hex representation of the id,
113+
# as it doesn't add any uniqueness to the string.
114+
self._unique_id: str = hex(id(self))[2:] if unique_id is None else unique_id
113115
self._tasks: set[asyncio.Task[Any]] = set()
114116

115117
@abc.abstractmethod

tests/test_asyncio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def nop() -> None:
4949
async def test_construction_defaults() -> None:
5050
"""Test the construction of a background service with default arguments."""
5151
fake_service = FakeService()
52-
assert fake_service.unique_id == str(id(fake_service))
52+
assert fake_service.unique_id == hex(id(fake_service))[2:]
5353
assert fake_service.tasks == set()
5454
assert fake_service.is_running is False
5555
assert str(fake_service) == f"FakeService[{fake_service.unique_id}]"

0 commit comments

Comments
 (0)