-
Notifications
You must be signed in to change notification settings - Fork 2
Add a base class for creating system-wide unique IDs #71
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
Conversation
|
This was already reviewed in frequenz-floss/frequenz-client-microgrid-python@6d494e1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Adds a new BaseId class to provide strongly typed, system-wide unique identifiers and accompanying tests.
- Introduces
BaseIdwith subclassing guards for unique string prefixes and optional name checks. - Implements comparison, hashing, and string/repr behavior for IDs.
- Adds tests covering instantiation, equality, ordering, hashing, and string representations.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/test_id.py | New tests for BaseId subclasses covering valid IDs, errors, comparison, hashing, and repr/str. |
| src/frequenz/core/id.py | Implements BaseId with __init_subclass__ checks, rich comparison, hashing, and string formatting. |
Comments suppressed due to low confidence (4)
tests/test_id.py:69
- Add a test case to verify that defining two subclasses with the same
str_prefixraises a ValueError, covering the uniqueness check inBaseId.__init_subclass__.
assert repr(id_obj) == "_TestId(42)"
tests/test_id.py:69
- Add a test to confirm that instantiating
BaseIddirectly raises a TypeError, covering the guard inBaseId.__new__.
assert repr(id_obj) == "_TestId(42)"
tests/test_id.py:14
- Add a test that defining a subclass without
allow_custom_name=Trueand with a name not ending in 'Id' raises a TypeError, covering the naming enforcement in__init_subclass__.
class _TestId(BaseId, str_prefix="TEST_ID"):
src/frequenz/core/id.py:68
- [nitpick] Consider decorating
BaseIdwith@functools.total_orderingso that all comparison methods (__le__,__gt__,__ge__) are automatically provided based on__eq__and__lt__.
class BaseId:
The new `BaseId` class provides strongly-typed unique identifiers for entities. This class can be subclassed to create distinct ID types for different components or concepts within a system. These IDs ensure type safety, meaning that an ID for one type of entity (e.g., a sensor) cannot be mistakenly used where an ID for another type (e.g., a microgrid) is expected. Signed-off-by: Leandro Lucarella <[email protected]>
The new
BaseIdclass provides strongly-typed unique identifiers for entities. This class can be subclassed to create distinct ID types for different components or concepts within a system.These IDs ensure type safety, meaning that an ID for one type of entity (e.g., a sensor) cannot be mistakenly used where an ID for another type (e.g., a microgrid) is expected.