Skip to content

Commit 130563d

Browse files
authored
BaseId: warn() instead of raise for duplicate prefixes (#73)
This is only necessary because to cope with frequenz-floss/frequenz-repo-config-python#421. The check can raise an exception again once that it fixed.
2 parents bfa3ef3 + fc7e174 commit 130563d

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

RELEASE_NOTES.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
## Summary
44

5-
This is the initial release of the Frequenz Core Library, which provides a set of fundamental tools and utilities for Python.
5+
<!-- Here goes a general summary of what this release is about -->
66

7-
The library currently includes:
7+
## Upgrading
88

9-
- `datetime`: For utilities related to dates and times.
10-
* `id`: For creating unique system-wide ID types.
11-
* `math`: For utilities related to math.
12-
* `typing`: For type annotations and type-checking utilities.
9+
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
1310

14-
But more tools will be added in the future.
11+
## New Features
12+
13+
<!-- Here goes the main new features and examples or instructions on how to use them -->
14+
15+
## Bug Fixes
16+
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.

src/frequenz/core/id.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class CustomNameForId(BaseId, str_prefix="CST", allow_custom_name=True):
6363

6464

6565
from typing import Any, ClassVar, Self, cast
66+
from warnings import warn
6667

6768

6869
class BaseId:
@@ -105,17 +106,18 @@ def __init_subclass__(
105106
**kwargs: Forwarded to the parent's __init_subclass__.
106107
107108
Raises:
108-
ValueError: If the `str_prefix` is already registered by another
109-
ID type.
110109
TypeError: If `allow_custom_name` is False and the class name
111110
does not end with "Id".
112111
"""
113112
super().__init_subclass__(**kwargs)
114113

115114
if str_prefix in BaseId._registered_prefixes:
116-
raise ValueError(
115+
# We want to raise an exception here, but currently can't due to
116+
# https://github.com/frequenz-floss/frequenz-repo-config-python/issues/421
117+
warn(
117118
f"Prefix '{str_prefix}' is already registered. "
118-
"ID prefixes must be unique."
119+
"ID prefixes must be unique.",
120+
stacklevel=2,
119121
)
120122
BaseId._registered_prefixes.add(str_prefix)
121123

tests/test_id.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ def test_valid() -> None:
2626
assert int(id_obj) == 42
2727

2828

29+
def test_use_a_subclass() -> None:
30+
"""Test that BaseId cannot be instantiated directly."""
31+
with pytest.raises(
32+
TypeError, match="BaseId cannot be instantiated directly. Use a subclass."
33+
):
34+
BaseId(42)
35+
36+
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+
2947
def test_negative_raises() -> None:
3048
"""Test that creating a negative ID raises ValueError."""
3149
with pytest.raises(ValueError, match="_TestId can't be negative"):

0 commit comments

Comments
 (0)