Skip to content

Commit f5576e1

Browse files
authored
Log instead of warn when registering an ID prefix that already exists (#75)
From https://github.com/frequenz-floss/frequenz-client-dispatch-python/pull/181/files#r2154829282
2 parents 130563d + ac73bc5 commit f5576e1

File tree

3 files changed

+7
-16
lines changed

3 files changed

+7
-16
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
## Bug Fixes
1616

17-
* `BaseId` will now warn instead of raising an exception when a duplicate prefix is detected. This is to fix [a problem with code examples](https://github.com/frequenz-floss/frequenz-repo-config-python/issues/421) being tested using sybil and the class being imported multiple times, which caused the exception to be raised.
17+
* `BaseId` will now log instead of raising a warning when a duplicate prefix is detected. This is to fix [a problem with code examples](https://github.com/frequenz-floss/frequenz-repo-config-python/issues/421) being tested using sybil and the class being imported multiple times, which caused the exception to be raised. We first tried to use `warn()` but that complicated the building process for all downstream projects, requiring them to add an exception for exactly this warning.

src/frequenz/core/id.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ class CustomNameForId(BaseId, str_prefix="CST", allow_custom_name=True):
6262
'''
6363

6464

65+
import logging
6566
from typing import Any, ClassVar, Self, cast
66-
from warnings import warn
67+
68+
_logger = logging.getLogger(__name__)
6769

6870

6971
class BaseId:
@@ -114,10 +116,9 @@ def __init_subclass__(
114116
if str_prefix in BaseId._registered_prefixes:
115117
# We want to raise an exception here, but currently can't due to
116118
# https://github.com/frequenz-floss/frequenz-repo-config-python/issues/421
117-
warn(
118-
f"Prefix '{str_prefix}' is already registered. "
119-
"ID prefixes must be unique.",
120-
stacklevel=2,
119+
_logger.warning(
120+
"Prefix '%s' is already registered. ID prefixes must be unique.",
121+
str_prefix,
121122
)
122123
BaseId._registered_prefixes.add(str_prefix)
123124

tests/test_id.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ def test_use_a_subclass() -> None:
3434
BaseId(42)
3535

3636

37-
def test_warn_non_unique_prefix() -> None:
38-
"""Test that using a non-unique prefix raises a warning."""
39-
with pytest.warns(UserWarning, match="Prefix 'TEST_ID' is already registered"):
40-
41-
class _TestDuplicateId(BaseId, str_prefix="TEST_ID"):
42-
"""A duplicate test ID class with the same prefix as _TestId."""
43-
44-
_TestDuplicateId(1)
45-
46-
4737
def test_negative_raises() -> None:
4838
"""Test that creating a negative ID raises ValueError."""
4939
with pytest.raises(ValueError, match="_TestId can't be negative"):

0 commit comments

Comments
 (0)