Skip to content

Commit 111c13a

Browse files
committed
Use str instead of repr for values error messages
The default (and custom) messages of the accessor errors interpolated the offending value with `!r`. Switch the non-string values to plain `{value}` so wrapper values render through their compact `__str__` — e.g. `<invalid:[10.0,-10.0]>` instead of `InvalidBounds(lower=10.0, upper=-10.0)` — which surfaces the `<invalid:...>` markers in the message and keeps it readable. Covered: `UnrecognizedEnumValueError`, `InvalidLatitudeError`, `InvalidLongitudeError`, `InvalidLifetimeError`, `InvalidBoundsError`, `InvalidBoundsSetError` and `InvalidDeliveryAreaError`, plus the custom messages raised by `ElectricalComponent.provides_telemetry()`, `accepts_control()` and `get_metric_config_bounds()`, and `Microgrid.is_active()`. String values keep `!r`: an invalid `country_code` may carry surprising characters, and quoting keeps them visible. The `attr_name` likewise keeps `!r`, the idiomatic way to show an attribute name. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent d33e035 commit 111c13a

11 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/frequenz/client/common/_exception.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(
7070
(
7171
message
7272
if message is not None
73-
else f"unrecognized enum value {value!r} for attribute {attr_name!r} in {instance}"
73+
else f"unrecognized enum value {value} for attribute {attr_name!r} in {instance}"
7474
),
7575
)
7676

src/frequenz/client/common/grid/_delivery_area.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def __init__(
274274
"""The invalid delivery area instance that caused this error."""
275275

276276
message = (
277-
f"invalid delivery area {delivery_area!r} for attribute {attr_name!r} in {instance}"
277+
f"invalid delivery area {delivery_area} for attribute {attr_name!r} in {instance}"
278278
if message is None
279279
else message
280280
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def __init__(
169169
(
170170
message
171171
if message is not None
172-
else f"invalid bounds {bounds!r} for attribute {attr_name!r} in {instance}"
172+
else f"invalid bounds {bounds} for attribute {attr_name!r} in {instance}"
173173
),
174174
)
175175

@@ -419,6 +419,6 @@ def __init__(
419419
(
420420
message
421421
if message is not None
422-
else f"invalid bounds set {bounds_set!r} for attribute {attr_name!r} in {instance}"
422+
else f"invalid bounds set {bounds_set} for attribute {attr_name!r} in {instance}"
423423
),
424424
)

src/frequenz/client/common/microgrid/_lifetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,6 @@ def __init__(
150150
(
151151
message
152152
if message is not None
153-
else f"invalid lifetime {lifetime!r} for attribute {attr_name!r} in {instance}"
153+
else f"invalid lifetime {lifetime} for attribute {attr_name!r} in {instance}"
154154
),
155155
)

src/frequenz/client/common/microgrid/_microgrid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def is_active(self) -> bool:
117117
self,
118118
"_active",
119119
value,
120-
f"unrecognized status of microgrid {self}: {value!r}",
120+
f"unrecognized status of microgrid {self}: {value}",
121121
)
122122
case unknown:
123123
assert_never(unknown)

src/frequenz/client/common/microgrid/electrical_components/_electrical_component.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def provides_telemetry(self) -> bool:
162162
self,
163163
"_provides_telemetry",
164164
value,
165-
f"operational mode {value!r} of {self} is not a recognized "
165+
f"operational mode {value} of {self} is not a recognized "
166166
"ElectricalComponentOperationalMode; telemetry availability "
167167
"is unknown",
168168
)
@@ -195,7 +195,7 @@ def accepts_control(self) -> bool:
195195
self,
196196
"_accepts_control",
197197
value,
198-
f"operational mode {value!r} of {self} is not a recognized "
198+
f"operational mode {value} of {self} is not a recognized "
199199
"ElectricalComponentOperationalMode; control availability "
200200
"is unknown",
201201
)
@@ -257,7 +257,7 @@ def get_metric_config_bounds(
257257
self,
258258
"metric_config_bounds",
259259
invalid,
260-
f"invalid bounds {invalid!r} for metric {metric} in {self}",
260+
f"invalid bounds {invalid} for metric {metric} in {self}",
261261
)
262262
case Bounds() as valid:
263263
return valid

src/frequenz/client/common/types/_location.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(
4444
(
4545
message
4646
if message is not None
47-
else f"invalid latitude {value!r} for attribute {attr_name!r} in "
47+
else f"invalid latitude {value} for attribute {attr_name!r} in "
4848
f"{instance}; must be in [-90, 90]"
4949
),
5050
)
@@ -84,7 +84,7 @@ def __init__(
8484
(
8585
message
8686
if message is not None
87-
else f"invalid longitude {value!r} for attribute {attr_name!r} in "
87+
else f"invalid longitude {value} for attribute {attr_name!r} in "
8888
f"{instance}; must be in [-180, 180]"
8989
),
9090
)

tests/grid/_delivery_area/test_invalid_delivery_area_error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_default_message() -> None:
1515
error = InvalidDeliveryAreaError("some-instance", "delivery_area", invalid)
1616
assert error.delivery_area is invalid
1717
assert (
18-
"invalid delivery area InvalidDeliveryArea(code='', code_type=0) for "
18+
f"invalid delivery area {invalid} for "
1919
"attribute 'delivery_area' in some-instance" == str(error)
2020
)
2121

tests/metrics/_bounds/test_invalid_bounds_error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_default_message() -> None:
1414

1515
assert error.bounds is invalid
1616
assert (
17-
str(error) == f"invalid bounds {invalid!r} for attribute 'config_bounds' "
17+
str(error) == f"invalid bounds {invalid} for attribute 'config_bounds' "
1818
"in some-instance"
1919
)
2020

tests/metrics/_bounds/test_invalid_bounds_set_error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_default_message() -> None:
2121

2222
assert error.bounds_set is invalid
2323
assert (
24-
str(error) == f"invalid bounds set {invalid!r} for attribute 'bounds_set' "
24+
str(error) == f"invalid bounds set {invalid} for attribute 'bounds_set' "
2525
"in some-instance"
2626
)
2727

0 commit comments

Comments
 (0)