Skip to content

Commit 91ab0f0

Browse files
committed
Replace Metric enum values with plain ints
This ensures that enum values are not accidentally passed to protobuf functions without conversion. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 73674eb commit 91ab0f0

3 files changed

Lines changed: 84 additions & 103 deletions

File tree

RELEASE_NOTES.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44

55
<!-- Here goes a general summary of what this release is about -->
66

7-
## Deprecation
7+
## Upgrading
88

9-
- Converting `Metric` enums from/to protobuf directly is deprecated and will be dropped in the next breaking release.
10-
11-
You should switch to use the new conversion functions in `frequenz.client.common.metrics.proto.v1alpha8` to convert from/to protobuf.
12-
13-
Since we can't emit deprecation messages for this (as they will trigger every time a metric value is used), please consider using the new conversion functions as soon as possible so the migration to the next breaking release is smooth.
9+
- Converting `Metric` enums from/to protobuf directly is not supported anymore, you need to use explicit conversion functions, like the ones in `frequenz.client.common.metrics.proto.v1alpha8`.
1410

1511
## New Features
1612

17-
- A new module `frequenz.client.common.metrics.proto.v1alpha8` has been added to provide conversion functions for `Metric`s from/to protobuf version `v1alpha8`.
13+
<!-- Here goes the main new features and examples or instructions on how to use them -->
1814

1915
## Bug Fixes
2016

src/frequenz/client/common/metrics/_metric.py

Lines changed: 71 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33

44
"""Supported metrics for microgrid components."""
55

6-
76
import enum
87

9-
from frequenz.api.common.v1alpha8.metrics import metrics_pb2
10-
118

129
@enum.unique
1310
class Metric(enum.Enum):
@@ -42,237 +39,215 @@ class Metric(enum.Enum):
4239
period, and therefore can be inconsistent.
4340
"""
4441

45-
UNSPECIFIED = metrics_pb2.METRIC_UNSPECIFIED
42+
UNSPECIFIED = 0
4643
"""The metric is unspecified (this should not be used)."""
4744

48-
DC_VOLTAGE = metrics_pb2.METRIC_DC_VOLTAGE
45+
DC_VOLTAGE = 1
4946
"""The DC voltage."""
5047

51-
DC_CURRENT = metrics_pb2.METRIC_DC_CURRENT
48+
DC_CURRENT = 2
5249
"""The DC current."""
5350

54-
DC_POWER = metrics_pb2.METRIC_DC_POWER
51+
DC_POWER = 3
5552
"""The DC power."""
5653

57-
AC_FREQUENCY = metrics_pb2.METRIC_AC_FREQUENCY
54+
AC_FREQUENCY = 10
5855
"""The AC frequency."""
5956

60-
AC_VOLTAGE = metrics_pb2.METRIC_AC_VOLTAGE
57+
AC_VOLTAGE = 11
6158
"""The AC electric potential difference."""
6259

63-
AC_VOLTAGE_PHASE_1_N = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_1_N
60+
AC_VOLTAGE_PHASE_1_N = 12
6461
"""The AC electric potential difference between phase 1 and neutral."""
6562

66-
AC_VOLTAGE_PHASE_2_N = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_2_N
63+
AC_VOLTAGE_PHASE_2_N = 13
6764
"""The AC electric potential difference between phase 2 and neutral."""
6865

69-
AC_VOLTAGE_PHASE_3_N = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_3_N
66+
AC_VOLTAGE_PHASE_3_N = 14
7067
"""The AC electric potential difference between phase 3 and neutral."""
7168

72-
AC_VOLTAGE_PHASE_1_PHASE_2 = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_1_PHASE_2
69+
AC_VOLTAGE_PHASE_1_PHASE_2 = 15
7370
"""The AC electric potential difference between phase 1 and phase 2."""
7471

75-
AC_VOLTAGE_PHASE_2_PHASE_3 = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_2_PHASE_3
72+
AC_VOLTAGE_PHASE_2_PHASE_3 = 16
7673
"""The AC electric potential difference between phase 2 and phase 3."""
7774

78-
AC_VOLTAGE_PHASE_3_PHASE_1 = metrics_pb2.METRIC_AC_VOLTAGE_PHASE_3_PHASE_1
75+
AC_VOLTAGE_PHASE_3_PHASE_1 = 17
7976
"""The AC electric potential difference between phase 3 and phase 1."""
8077

81-
AC_CURRENT = metrics_pb2.METRIC_AC_CURRENT
78+
AC_CURRENT = 18
8279
"""The AC current."""
8380

84-
AC_CURRENT_PHASE_1 = metrics_pb2.METRIC_AC_CURRENT_PHASE_1
81+
AC_CURRENT_PHASE_1 = 19
8582
"""The AC current in phase 1."""
8683

87-
AC_CURRENT_PHASE_2 = metrics_pb2.METRIC_AC_CURRENT_PHASE_2
84+
AC_CURRENT_PHASE_2 = 20
8885
"""The AC current in phase 2."""
8986

90-
AC_CURRENT_PHASE_3 = metrics_pb2.METRIC_AC_CURRENT_PHASE_3
87+
AC_CURRENT_PHASE_3 = 21
9188
"""The AC current in phase 3."""
9289

93-
AC_POWER_APPARENT = metrics_pb2.METRIC_AC_POWER_APPARENT
90+
AC_POWER_APPARENT = 22
9491
"""The AC apparent power."""
9592

96-
AC_POWER_APPARENT_PHASE_1 = metrics_pb2.METRIC_AC_POWER_APPARENT_PHASE_1
93+
AC_POWER_APPARENT_PHASE_1 = 23
9794
"""The AC apparent power in phase 1."""
9895

99-
AC_POWER_APPARENT_PHASE_2 = metrics_pb2.METRIC_AC_POWER_APPARENT_PHASE_2
96+
AC_POWER_APPARENT_PHASE_2 = 24
10097
"""The AC apparent power in phase 2."""
10198

102-
AC_POWER_APPARENT_PHASE_3 = metrics_pb2.METRIC_AC_POWER_APPARENT_PHASE_3
99+
AC_POWER_APPARENT_PHASE_3 = 25
103100
"""The AC apparent power in phase 3."""
104101

105-
AC_POWER_ACTIVE = metrics_pb2.METRIC_AC_POWER_ACTIVE
102+
AC_POWER_ACTIVE = 26
106103
"""The AC active power."""
107104

108-
AC_POWER_ACTIVE_PHASE_1 = metrics_pb2.METRIC_AC_POWER_ACTIVE_PHASE_1
105+
AC_POWER_ACTIVE_PHASE_1 = 27
109106
"""The AC active power in phase 1."""
110107

111-
AC_POWER_ACTIVE_PHASE_2 = metrics_pb2.METRIC_AC_POWER_ACTIVE_PHASE_2
108+
AC_POWER_ACTIVE_PHASE_2 = 28
112109
"""The AC active power in phase 2."""
113110

114-
AC_POWER_ACTIVE_PHASE_3 = metrics_pb2.METRIC_AC_POWER_ACTIVE_PHASE_3
111+
AC_POWER_ACTIVE_PHASE_3 = 29
115112
"""The AC active power in phase 3."""
116113

117-
AC_POWER_REACTIVE = metrics_pb2.METRIC_AC_POWER_REACTIVE
114+
AC_POWER_REACTIVE = 30
118115
"""The AC reactive power."""
119116

120-
AC_POWER_REACTIVE_PHASE_1 = metrics_pb2.METRIC_AC_POWER_REACTIVE_PHASE_1
117+
AC_POWER_REACTIVE_PHASE_1 = 31
121118
"""The AC reactive power in phase 1."""
122119

123-
AC_POWER_REACTIVE_PHASE_2 = metrics_pb2.METRIC_AC_POWER_REACTIVE_PHASE_2
120+
AC_POWER_REACTIVE_PHASE_2 = 32
124121
"""The AC reactive power in phase 2."""
125122

126-
AC_POWER_REACTIVE_PHASE_3 = metrics_pb2.METRIC_AC_POWER_REACTIVE_PHASE_3
123+
AC_POWER_REACTIVE_PHASE_3 = 33
127124
"""The AC reactive power in phase 3."""
128125

129-
AC_POWER_FACTOR = metrics_pb2.METRIC_AC_POWER_FACTOR
126+
AC_POWER_FACTOR = 40
130127
"""The AC power factor."""
131128

132-
AC_POWER_FACTOR_PHASE_1 = metrics_pb2.METRIC_AC_POWER_FACTOR_PHASE_1
129+
AC_POWER_FACTOR_PHASE_1 = 41
133130
"""The AC power factor in phase 1."""
134131

135-
AC_POWER_FACTOR_PHASE_2 = metrics_pb2.METRIC_AC_POWER_FACTOR_PHASE_2
132+
AC_POWER_FACTOR_PHASE_2 = 42
136133
"""The AC power factor in phase 2."""
137134

138-
AC_POWER_FACTOR_PHASE_3 = metrics_pb2.METRIC_AC_POWER_FACTOR_PHASE_3
135+
AC_POWER_FACTOR_PHASE_3 = 43
139136
"""The AC power factor in phase 3."""
140137

141-
AC_ENERGY_APPARENT = metrics_pb2.METRIC_AC_ENERGY_APPARENT
138+
AC_ENERGY_APPARENT = 50
142139
"""The AC apparent energy."""
143140

144-
AC_ENERGY_APPARENT_PHASE_1 = metrics_pb2.METRIC_AC_ENERGY_APPARENT_PHASE_1
141+
AC_ENERGY_APPARENT_PHASE_1 = 51
145142
"""The AC apparent energy in phase 1."""
146143

147-
AC_ENERGY_APPARENT_PHASE_2 = metrics_pb2.METRIC_AC_ENERGY_APPARENT_PHASE_2
144+
AC_ENERGY_APPARENT_PHASE_2 = 52
148145
"""The AC apparent energy in phase 2."""
149146

150-
AC_ENERGY_APPARENT_PHASE_3 = metrics_pb2.METRIC_AC_ENERGY_APPARENT_PHASE_3
147+
AC_ENERGY_APPARENT_PHASE_3 = 53
151148
"""The AC apparent energy in phase 3."""
152149

153-
AC_ENERGY_ACTIVE = metrics_pb2.METRIC_AC_ENERGY_ACTIVE
150+
AC_ENERGY_ACTIVE = 54
154151
"""The AC active energy."""
155152

156-
AC_ENERGY_ACTIVE_PHASE_1 = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_PHASE_1
153+
AC_ENERGY_ACTIVE_PHASE_1 = 55
157154
"""The AC active energy in phase 1."""
158155

159-
AC_ENERGY_ACTIVE_PHASE_2 = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_PHASE_2
156+
AC_ENERGY_ACTIVE_PHASE_2 = 56
160157
"""The AC active energy in phase 2."""
161158

162-
AC_ENERGY_ACTIVE_PHASE_3 = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_PHASE_3
159+
AC_ENERGY_ACTIVE_PHASE_3 = 57
163160
"""The AC active energy in phase 3."""
164161

165-
AC_ENERGY_ACTIVE_CONSUMED = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_CONSUMED
162+
AC_ENERGY_ACTIVE_CONSUMED = 58
166163
"""The AC active energy consumed."""
167164

168-
AC_ENERGY_ACTIVE_CONSUMED_PHASE_1 = (
169-
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_CONSUMED_PHASE_1
170-
)
165+
AC_ENERGY_ACTIVE_CONSUMED_PHASE_1 = 59
171166
"""The AC active energy consumed in phase 1."""
172167

173-
AC_ENERGY_ACTIVE_CONSUMED_PHASE_2 = (
174-
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_CONSUMED_PHASE_2
175-
)
168+
AC_ENERGY_ACTIVE_CONSUMED_PHASE_2 = 60
176169
"""The AC active energy consumed in phase 2."""
177170

178-
AC_ENERGY_ACTIVE_CONSUMED_PHASE_3 = (
179-
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_CONSUMED_PHASE_3
180-
)
171+
AC_ENERGY_ACTIVE_CONSUMED_PHASE_3 = 61
181172
"""The AC active energy consumed in phase 3."""
182173

183-
AC_ENERGY_ACTIVE_DELIVERED = metrics_pb2.METRIC_AC_ENERGY_ACTIVE_DELIVERED
174+
AC_ENERGY_ACTIVE_DELIVERED = 62
184175
"""The AC active energy delivered."""
185176

186-
AC_ENERGY_ACTIVE_DELIVERED_PHASE_1 = (
187-
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_DELIVERED_PHASE_1
188-
)
177+
AC_ENERGY_ACTIVE_DELIVERED_PHASE_1 = 63
189178
"""The AC active energy delivered in phase 1."""
190179

191-
AC_ENERGY_ACTIVE_DELIVERED_PHASE_2 = (
192-
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_DELIVERED_PHASE_2
193-
)
180+
AC_ENERGY_ACTIVE_DELIVERED_PHASE_2 = 64
194181
"""The AC active energy delivered in phase 2."""
195182

196-
AC_ENERGY_ACTIVE_DELIVERED_PHASE_3 = (
197-
metrics_pb2.METRIC_AC_ENERGY_ACTIVE_DELIVERED_PHASE_3
198-
)
183+
AC_ENERGY_ACTIVE_DELIVERED_PHASE_3 = 65
199184
"""The AC active energy delivered in phase 3."""
200185

201-
AC_ENERGY_REACTIVE = metrics_pb2.METRIC_AC_ENERGY_REACTIVE
186+
AC_ENERGY_REACTIVE = 66
202187
"""The AC reactive energy."""
203188

204-
AC_ENERGY_REACTIVE_PHASE_1 = metrics_pb2.METRIC_AC_ENERGY_REACTIVE_PHASE_1
189+
AC_ENERGY_REACTIVE_PHASE_1 = 67
205190
"""The AC reactive energy in phase 1."""
206191

207-
AC_ENERGY_REACTIVE_PHASE_2 = metrics_pb2.METRIC_AC_ENERGY_REACTIVE_PHASE_2
192+
AC_ENERGY_REACTIVE_PHASE_2 = 68
208193
"""The AC reactive energy in phase 2."""
209194

210-
AC_ENERGY_REACTIVE_PHASE_3 = metrics_pb2.METRIC_AC_ENERGY_REACTIVE_PHASE_3
195+
AC_ENERGY_REACTIVE_PHASE_3 = 69
211196
"""The AC reactive energy in phase 3."""
212197

213-
AC_TOTAL_HARMONIC_DISTORTION_CURRENT = (
214-
metrics_pb2.METRIC_AC_TOTAL_HARMONIC_DISTORTION_CURRENT
215-
)
198+
AC_TOTAL_HARMONIC_DISTORTION_CURRENT = 80
216199
"""The AC total harmonic distortion current."""
217200

218-
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_1 = (
219-
metrics_pb2.METRIC_AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_1
220-
)
201+
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_1 = 81
221202
"""The AC total harmonic distortion current in phase 1."""
222203

223-
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_2 = (
224-
metrics_pb2.METRIC_AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_2
225-
)
204+
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_2 = 82
226205
"""The AC total harmonic distortion current in phase 2."""
227206

228-
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_3 = (
229-
metrics_pb2.METRIC_AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_3
230-
)
207+
AC_TOTAL_HARMONIC_DISTORTION_CURRENT_PHASE_3 = 83
231208
"""The AC total harmonic distortion current in phase 3."""
232209

233-
BATTERY_CAPACITY = metrics_pb2.METRIC_BATTERY_CAPACITY
210+
BATTERY_CAPACITY = 100
234211
"""The capacity of the battery."""
235212

236-
BATTERY_SOC_PCT = metrics_pb2.METRIC_BATTERY_SOC_PCT
213+
BATTERY_SOC_PCT = 101
237214
"""The state of charge of the battery as a percentage."""
238215

239-
BATTERY_TEMPERATURE = metrics_pb2.METRIC_BATTERY_TEMPERATURE
216+
BATTERY_TEMPERATURE = 102
240217
"""The temperature of the battery."""
241218

242-
INVERTER_TEMPERATURE = metrics_pb2.METRIC_INVERTER_TEMPERATURE
219+
INVERTER_TEMPERATURE = 120
243220
"""The temperature of the inverter."""
244221

245-
INVERTER_TEMPERATURE_CABINET = metrics_pb2.METRIC_INVERTER_TEMPERATURE_CABINET
222+
INVERTER_TEMPERATURE_CABINET = 121
246223
"""The temperature of the inverter cabinet."""
247224

248-
INVERTER_TEMPERATURE_HEATSINK = metrics_pb2.METRIC_INVERTER_TEMPERATURE_HEATSINK
225+
INVERTER_TEMPERATURE_HEATSINK = 122
249226
"""The temperature of the inverter heatsink."""
250227

251-
INVERTER_TEMPERATURE_TRANSFORMER = (
252-
metrics_pb2.METRIC_INVERTER_TEMPERATURE_TRANSFORMER
253-
)
228+
INVERTER_TEMPERATURE_TRANSFORMER = 123
254229
"""The temperature of the inverter transformer."""
255230

256-
EV_CHARGER_TEMPERATURE = metrics_pb2.METRIC_EV_CHARGER_TEMPERATURE
231+
EV_CHARGER_TEMPERATURE = 140
257232
"""The temperature of the EV charger."""
258233

259-
SENSOR_WIND_SPEED = metrics_pb2.METRIC_SENSOR_WIND_SPEED
234+
SENSOR_WIND_SPEED = 160
260235
"""The speed of the wind measured."""
261236

262-
SENSOR_WIND_DIRECTION = metrics_pb2.METRIC_SENSOR_WIND_DIRECTION
237+
SENSOR_WIND_DIRECTION = 161
263238
"""The direction of the wind measured."""
264239

265-
SENSOR_TEMPERATURE = metrics_pb2.METRIC_SENSOR_TEMPERATURE
240+
SENSOR_TEMPERATURE = 162
266241
"""The temperature measured."""
267242

268-
SENSOR_RELATIVE_HUMIDITY = metrics_pb2.METRIC_SENSOR_RELATIVE_HUMIDITY
243+
SENSOR_RELATIVE_HUMIDITY = 163
269244
"""The relative humidity measured."""
270245

271-
SENSOR_DEW_POINT = metrics_pb2.METRIC_SENSOR_DEW_POINT
246+
SENSOR_DEW_POINT = 164
272247
"""The dew point measured."""
273248

274-
SENSOR_AIR_PRESSURE = metrics_pb2.METRIC_SENSOR_AIR_PRESSURE
249+
SENSOR_AIR_PRESSURE = 165
275250
"""The air pressure measured."""
276251

277-
SENSOR_IRRADIANCE = metrics_pb2.METRIC_SENSOR_IRRADIANCE
252+
SENSOR_IRRADIANCE = 166
278253
"""The irradiance measured."""

tests/metrics/proto/v1alpha8/test_metric.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
UNKNOWN_PB_VALUE = metrics_pb2.Metric.ValueType(max(m.value for m in Metric) + 1)
2222

2323

24+
def test_no_implicit_to_proto_conversion() -> None:
25+
"""Test that protobuf enum values are not implicitly convertible to Metric enum members."""
26+
# mypy should complain about this assignment, so we ignore the type check here.
27+
# If mypy doesn't find an issue with this conversion, it should complain about
28+
# the ignore comment having no effect.
29+
metric = list(Metric)[0]
30+
_: metrics_pb2.Metric.ValueType = metric.value # type: ignore[assignment]
31+
metrics_pb2.Metric.Name(metric.value) # type: ignore[arg-type]
32+
33+
2434
@pytest.mark.parametrize("pb_name", PB_NAMES)
2535
def test_proto_enum_matches_enum_name(pb_name: str) -> None:
2636
"""Test that all known protobuf enum names have a matching Metric enum member."""

0 commit comments

Comments
 (0)