|
1 | 1 | # License: MIT |
2 | 2 | # Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
3 | 3 |
|
4 | | -"""Strongly typed IDs for microgrids, components and sensors.""" |
| 4 | +r'''Provides strongly-typed unique identifiers for entities. |
5 | 5 |
|
| 6 | +This module offers a base class, |
| 7 | +[`BaseId`][frequenz.client.microgrid.id.BaseId], which can be subclassed to |
| 8 | +create distinct ID types for different components or concepts within a system. |
| 9 | +These IDs ensure type safety, meaning that an ID for one type of entity (e.g., a |
| 10 | +sensor) cannot be mistakenly used where an ID for another type (e.g., a |
| 11 | +microgrid) is expected. |
6 | 12 |
|
7 | | -from typing import final |
| 13 | +# Creating Custom ID Types |
8 | 14 |
|
| 15 | +To define a new ID type, create a class that inherits from |
| 16 | +[`BaseId`][frequenz.client.microgrid.id.BaseId] and provide a unique |
| 17 | +`str_prefix` as a keyword argument in the class definition. This prefix is used |
| 18 | +in the string representation of the ID and must be unique across all ID types. |
9 | 19 |
|
10 | | -@final |
11 | | -class MicrogridId: |
12 | | - """A unique identifier for a microgrid.""" |
| 20 | +Note: |
| 21 | + The `str_prefix` must be unique across all ID types. If you try to use a |
| 22 | + prefix that is already registered, a `ValueError` will be raised when defining |
| 23 | + the class. |
13 | 24 |
|
14 | | - def __init__(self, id_: int, /) -> None: |
15 | | - """Initialize this instance. |
| 25 | +To encourage consistency, the class name must end with the suffix "Id" (e.g., |
| 26 | +`MyNewId`). This check can be bypassed by passing `allow_custom_name=True` when |
| 27 | +defining the class (e.g., `class MyCustomName(BaseId, str_prefix="MCN", |
| 28 | +allow_custom_name=True):`). |
16 | 29 |
|
17 | | - Args: |
18 | | - id_: The numeric unique identifier of the microgrid. |
| 30 | +Tip: |
| 31 | + Use the [`@typing.final`][typing.final] decorator to prevent subclassing of |
| 32 | + ID classes. |
19 | 33 |
|
20 | | - Raises: |
21 | | - ValueError: If the ID is negative. |
22 | | - """ |
23 | | - if id_ < 0: |
24 | | - raise ValueError("Microgrid ID can't be negative.") |
25 | | - self._id = id_ |
| 34 | +Example: Creating a standard ID type |
| 35 | + ```python |
| 36 | + from typing import final |
| 37 | + from frequenz.client.microgrid.id import BaseId |
26 | 38 |
|
27 | | - def __int__(self) -> int: |
28 | | - """Return the numeric ID of this instance.""" |
29 | | - return self._id |
| 39 | + @final |
| 40 | + class InverterId(BaseId, str_prefix="INV"): |
| 41 | + """A unique identifier for an inverter.""" |
30 | 42 |
|
31 | | - def __eq__(self, other: object) -> bool: |
32 | | - """Check if this instance is equal to another object.""" |
33 | | - # This is not an unidiomatic typecheck, that's an odd name for the check. |
34 | | - # isinstance() returns True for subclasses, which is not what we want here. |
35 | | - # pylint: disable-next=unidiomatic-typecheck |
36 | | - return type(other) is MicrogridId and self._id == other._id |
| 43 | + inv_id = InverterId(123) |
| 44 | + print(inv_id) # Output: INV123 |
| 45 | + print(int(inv_id)) # Output: 123 |
| 46 | + ``` |
37 | 47 |
|
38 | | - def __lt__(self, other: object) -> bool: |
39 | | - """Check if this instance is less than another object.""" |
40 | | - # pylint: disable-next=unidiomatic-typecheck |
41 | | - if type(other) is MicrogridId: |
42 | | - return self._id < other._id |
43 | | - return NotImplemented |
| 48 | +Example: Creating an ID type with a non-standard name |
| 49 | + ```python |
| 50 | + from typing import final |
| 51 | + from frequenz.client.microgrid.id import BaseId |
44 | 52 |
|
45 | | - def __hash__(self) -> int: |
46 | | - """Return the hash of this instance.""" |
47 | | - # We include the class because we explicitly want to avoid the same ID to give |
48 | | - # the same hash for different classes of IDs |
49 | | - return hash((MicrogridId, self._id)) |
| 53 | + @final |
| 54 | + class CustomNameForId(BaseId, str_prefix="CST", allow_custom_name=True): |
| 55 | + """An ID with a custom name, not ending in 'Id'.""" |
50 | 56 |
|
51 | | - def __repr__(self) -> str: |
52 | | - """Return the string representation of this instance.""" |
53 | | - return f"{type(self).__name__}({self._id!r})" |
| 57 | + custom_id = CustomNameForId(456) |
| 58 | + print(custom_id) # Output: CST456 |
| 59 | + print(int(custom_id)) # Output: 456 |
| 60 | + ``` |
54 | 61 |
|
55 | | - def __str__(self) -> str: |
56 | | - """Return the short string representation of this instance.""" |
57 | | - return f"MID{self._id}" |
| 62 | +# Predefined ID Types |
58 | 63 |
|
| 64 | +This module predefines the following ID types: |
59 | 65 |
|
60 | | -@final |
61 | | -class ComponentId: |
62 | | - """A unique identifier for a microgrid component.""" |
| 66 | +- [`ComponentId`][frequenz.client.microgrid.id.ComponentId]: For identifying |
| 67 | + generic components. |
| 68 | +- [`MicrogridId`][frequenz.client.microgrid.id.MicrogridId]: For identifying |
| 69 | + microgrids. |
| 70 | +- [`SensorId`][frequenz.client.microgrid.id.SensorId]: For identifying sensors. |
| 71 | +''' |
63 | 72 |
|
64 | | - def __init__(self, id_: int, /) -> None: |
65 | | - """Initialize this instance. |
66 | 73 |
|
67 | | - Args: |
68 | | - id_: The numeric unique identifier of the microgrid component. |
| 74 | +from typing import Any, ClassVar, Self, cast, final |
69 | 75 |
|
70 | | - Raises: |
71 | | - ValueError: If the ID is negative. |
72 | | - """ |
73 | | - if id_ < 0: |
74 | | - raise ValueError("Component ID can't be negative.") |
75 | | - self._id = id_ |
76 | 76 |
|
77 | | - def __int__(self) -> int: |
78 | | - """Return the numeric ID of this instance.""" |
79 | | - return self._id |
| 77 | +class BaseId: |
| 78 | + """A base class for unique identifiers. |
80 | 79 |
|
81 | | - def __eq__(self, other: object) -> bool: |
82 | | - """Check if this instance is equal to another object.""" |
83 | | - # This is not an unidiomatic typecheck, that's an odd name for the check. |
84 | | - # isinstance() returns True for subclasses, which is not what we want here. |
85 | | - # pylint: disable-next=unidiomatic-typecheck |
86 | | - return type(other) is ComponentId and self._id == other._id |
| 80 | + Subclasses must provide a unique `str_prefix` keyword argument during |
| 81 | + definition, which is used in the string representation of the ID. |
87 | 82 |
|
88 | | - def __lt__(self, other: object) -> bool: |
89 | | - """Check if this instance is less than another object.""" |
90 | | - # pylint: disable-next=unidiomatic-typecheck |
91 | | - if type(other) is ComponentId: |
92 | | - return self._id < other._id |
93 | | - return NotImplemented |
| 83 | + By default, subclass names must end with "Id". This can be overridden by |
| 84 | + passing `allow_custom_name=True` during class definition. |
94 | 85 |
|
95 | | - def __hash__(self) -> int: |
96 | | - """Return the hash of this instance.""" |
97 | | - # We include the class because we explicitly want to avoid the same ID to give |
98 | | - # the same hash for different classes of IDs |
99 | | - return hash((ComponentId, self._id)) |
| 86 | + For more information and examples, see the [module's |
| 87 | + documentation][frequenz.client.microgrid.id]. |
| 88 | + """ |
100 | 89 |
|
101 | | - def __repr__(self) -> str: |
102 | | - """Return the string representation of this instance.""" |
103 | | - return f"{type(self).__name__}({self._id!r})" |
| 90 | + _id: int |
| 91 | + _str_prefix: ClassVar[str] |
| 92 | + _registered_prefixes: ClassVar[set[str]] = set() |
104 | 93 |
|
105 | | - def __str__(self) -> str: |
106 | | - """Return the short string representation of this instance.""" |
107 | | - return f"CID{self._id}" |
| 94 | + def __new__(cls, *_: Any, **__: Any) -> Self: |
| 95 | + """Create a new instance of the ID class, only if it is a subclass of BaseId.""" |
| 96 | + if cls is BaseId: |
| 97 | + raise TypeError("BaseId cannot be instantiated directly. Use a subclass.") |
| 98 | + return super().__new__(cls) |
108 | 99 |
|
| 100 | + def __init_subclass__( |
| 101 | + cls, |
| 102 | + *, |
| 103 | + str_prefix: str, |
| 104 | + allow_custom_name: bool = False, |
| 105 | + **kwargs: Any, |
| 106 | + ) -> None: |
| 107 | + """Initialize a subclass, set its string prefix, and perform checks. |
109 | 108 |
|
110 | | -@final |
111 | | -class SensorId: |
112 | | - """A unique identifier for a microgrid sensor.""" |
| 109 | + Args: |
| 110 | + str_prefix: The string prefix for the ID type (e.g., "MID"). |
| 111 | + Must be unique across all ID types. |
| 112 | + allow_custom_name: If True, bypasses the check that the class name |
| 113 | + must end with "Id". Defaults to False. |
| 114 | + **kwargs: Forwarded to the parent's __init_subclass__. |
| 115 | +
|
| 116 | + Raises: |
| 117 | + ValueError: If the `str_prefix` is already registered by another |
| 118 | + ID type. |
| 119 | + TypeError: If `allow_custom_name` is False and the class name |
| 120 | + does not end with "Id". |
| 121 | + """ |
| 122 | + super().__init_subclass__(**kwargs) |
| 123 | + |
| 124 | + if str_prefix in BaseId._registered_prefixes: |
| 125 | + raise ValueError( |
| 126 | + f"Prefix '{str_prefix}' is already registered. " |
| 127 | + "ID prefixes must be unique." |
| 128 | + ) |
| 129 | + BaseId._registered_prefixes.add(str_prefix) |
| 130 | + |
| 131 | + if not allow_custom_name and not cls.__name__.endswith("Id"): |
| 132 | + raise TypeError( |
| 133 | + f"Class name '{cls.__name__}' for an ID class must end with 'Id' " |
| 134 | + "(e.g., 'SomeId'), or use `allow_custom_name=True`." |
| 135 | + ) |
| 136 | + |
| 137 | + cls._str_prefix = str_prefix |
113 | 138 |
|
114 | 139 | def __init__(self, id_: int, /) -> None: |
115 | 140 | """Initialize this instance. |
116 | 141 |
|
117 | 142 | Args: |
118 | | - id_: The numeric unique identifier of the microgrid sensor. |
| 143 | + id_: The numeric unique identifier. |
119 | 144 |
|
120 | 145 | Raises: |
121 | 146 | ValueError: If the ID is negative. |
122 | 147 | """ |
123 | 148 | if id_ < 0: |
124 | | - raise ValueError("Sensor ID can't be negative.") |
| 149 | + raise ValueError(f"{type(self).__name__} can't be negative.") |
125 | 150 | self._id = id_ |
126 | 151 |
|
| 152 | + @property |
| 153 | + def str_prefix(self) -> str: |
| 154 | + """The prefix used for the string representation of this ID.""" |
| 155 | + return self._str_prefix |
| 156 | + |
127 | 157 | def __int__(self) -> int: |
128 | 158 | """Return the numeric ID of this instance.""" |
129 | 159 | return self._id |
130 | 160 |
|
131 | 161 | def __eq__(self, other: object) -> bool: |
132 | | - """Check if this instance is equal to another object.""" |
133 | | - # This is not an unidiomatic typecheck, that's an odd name for the check. |
134 | | - # isinstance() returns True for subclasses, which is not what we want here. |
| 162 | + """Check if this instance is equal to another object. |
| 163 | +
|
| 164 | + Equality is defined as being of the exact same type and having the same |
| 165 | + underlying ID. |
| 166 | + """ |
| 167 | + # pylint thinks this is not an unidiomatic typecheck, but in this case |
| 168 | + # it is not. isinstance() returns True for subclasses, which is not |
| 169 | + # what we want here, as different ID types should never be equal. |
135 | 170 | # pylint: disable-next=unidiomatic-typecheck |
136 | | - return type(other) is SensorId and self._id == other._id |
| 171 | + if type(other) is not type(self): |
| 172 | + return NotImplemented |
| 173 | + # We already checked type(other) is type(self), but mypy doesn't |
| 174 | + # understand that, so we need to cast it to Self. |
| 175 | + other_id = cast(Self, other) |
| 176 | + return self._id == other_id._id |
137 | 177 |
|
138 | 178 | def __lt__(self, other: object) -> bool: |
139 | | - """Check if this instance is less than another object.""" |
| 179 | + """Check if this instance is less than another object. |
| 180 | +
|
| 181 | + Comparison is only defined between instances of the exact same type. |
| 182 | + """ |
140 | 183 | # pylint: disable-next=unidiomatic-typecheck |
141 | | - if type(other) is SensorId: |
142 | | - return self._id < other._id |
143 | | - return NotImplemented |
| 184 | + if type(other) is not type(self): |
| 185 | + return NotImplemented |
| 186 | + other_id = cast(Self, other) |
| 187 | + return self._id < other_id._id |
144 | 188 |
|
145 | 189 | def __hash__(self) -> int: |
146 | | - """Return the hash of this instance.""" |
147 | | - # We include the class because we explicitly want to avoid the same ID to give |
148 | | - # the same hash for different classes of IDs |
149 | | - return hash((SensorId, self._id)) |
| 190 | + """Return the hash of this instance. |
| 191 | +
|
| 192 | + The hash is based on the exact type and the underlying ID to ensure |
| 193 | + that IDs of different types but with the same numeric value have different hashes. |
| 194 | + """ |
| 195 | + return hash((type(self), self._id)) |
150 | 196 |
|
151 | 197 | def __repr__(self) -> str: |
152 | 198 | """Return the string representation of this instance.""" |
153 | 199 | return f"{type(self).__name__}({self._id!r})" |
154 | 200 |
|
155 | 201 | def __str__(self) -> str: |
156 | 202 | """Return the short string representation of this instance.""" |
157 | | - return f"SID{self._id}" |
| 203 | + return f"{type(self)._str_prefix}{self._id}" |
| 204 | + |
| 205 | + |
| 206 | +@final |
| 207 | +class MicrogridId(BaseId, str_prefix="MID"): |
| 208 | + """A unique identifier for a microgrid.""" |
| 209 | + |
| 210 | + |
| 211 | +@final |
| 212 | +class ComponentId(BaseId, str_prefix="CID"): |
| 213 | + """A unique identifier for a microgrid component.""" |
| 214 | + |
| 215 | + |
| 216 | +@final |
| 217 | +class SensorId(BaseId, str_prefix="SID"): |
| 218 | + """A unique identifier for a microgrid sensor.""" |
0 commit comments