Skip to content

Commit e9bf91a

Browse files
Change type of TargetComponents to return ComponentId
* `frequenz.client.dispatch.TargetComponents` is now public * its type has changed from `list[int] | list[ComponentCategory]` to `list[ComponentId] | list[ComponentCategory]`. * This change introduces a new dependency on `frequenz-client-microgrid` (>= v0.7.0, < 0.8.0) for the `frequenz.client.microgrid.ComponentId` type. Signed-off-by: Elzbieta Kotulska <[email protected]>
1 parent d3fbfe1 commit e9bf91a

File tree

7 files changed

+41
-35
lines changed

7 files changed

+41
-35
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@
22

33
## Summary
44

5-
<!-- Here goes a general summary of what this release is about -->
6-
7-
## Upgrading
8-
9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
10-
115
## New Features
126

137
* `dispatch-cli` supports now the parameter `--type` and `--running` to filter the list of running services by type and status, respectively.
148
* Every call now has a default timeout of 60 seconds, streams terminate after five minutes. This can be influenced by the two new parameters for`DispatchApiClient.__init__()`:
159
* `default_timeout: timedelta` (default: 60 seconds)
1610
* `stream_timeout: timedelta` (default: 5 minutes)
1711

18-
## Bug Fixes
19-
20-
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
12+
* `frequenz.client.dispatch.TargetComponents` is now public, and its type has changed from `list[int] | list[ComponentCategory]` to `list[ComponentId] | list[ComponentCategory]`. This change introduces a new dependency on `frequenz-client-microgrid` (>= v0.7.0, < 0.8.0) for the `frequenz.client.microgrid.ComponentId` type.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies = [
4040
"frequenz-api-dispatch == 1.0.0-rc1",
4141
"frequenz-client-base >= 0.8.0, < 0.10.0",
4242
"frequenz-client-common >= 0.1.0, < 0.4.0",
43+
"frequenz-client-microgrid >= 0.7.0, < 0.8.0",
4344
"grpcio >= 1.66.1, < 2",
4445
"python-dateutil >= 2.8.2, < 3.0",
4546
]

src/frequenz/client/dispatch/_cli_types.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from tzlocal import get_localzone
1313

1414
from frequenz.client.common.microgrid.components import ComponentCategory
15+
from frequenz.client.microgrid import ComponentId
1516

1617
# Disable a false positive from pylint
1718
# pylint: disable=inconsistent-return-statements
@@ -140,7 +141,7 @@ class TargetComponentParamType(click.ParamType):
140141

141142
def convert(
142143
self, value: Any, param: click.Parameter | None, ctx: click.Context | None
143-
) -> list[ComponentCategory] | list[int]:
144+
) -> list[ComponentCategory] | list[ComponentId]:
144145
"""Convert the input value into a list of ComponentCategory or IDs.
145146
146147
Args:
@@ -152,6 +153,8 @@ def convert(
152153
A list of component ids or component categories.
153154
"""
154155
if isinstance(value, list): # Already a list
156+
if all(isinstance(item, int) for item in value):
157+
return list(map(ComponentId, value))
155158
return value
156159

157160
values = value.split(",")
@@ -162,7 +165,7 @@ def convert(
162165
error: Exception | None = None
163166
# Attempt to parse component ids
164167
try:
165-
return [int(id) for id in values]
168+
return [ComponentId(int(id)) for id in values]
166169
except ValueError as e:
167170
error = e
168171

src/frequenz/client/dispatch/test/generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import datetime, timedelta, timezone
88

99
from frequenz.client.common.microgrid.components import ComponentCategory
10+
from frequenz.client.microgrid import ComponentId
1011

1112
from .._internal_types import rounded_start_time
1213
from ..recurrence import EndCriteria, Frequency, RecurrenceRule, Weekday
@@ -99,7 +100,7 @@ def generate_dispatch(self) -> Dispatch:
99100
for _ in range(self._rng.randint(1, 10))
100101
],
101102
[
102-
self._rng.randint(1, 100)
103+
ComponentId(self._rng.randint(1, 100))
103104
for _ in range(self._rng.randint(1, 10))
104105
],
105106
]

src/frequenz/client/dispatch/types.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424

2525
# pylint: enable=no-name-in-module
2626
from frequenz.client.common.microgrid.components import ComponentCategory
27+
from frequenz.client.microgrid import ComponentId
2728

2829
from .recurrence import Frequency, RecurrenceRule, Weekday
2930

30-
TargetComponents = list[int] | list[ComponentCategory]
31+
TargetComponents = list[ComponentId] | list[ComponentCategory]
3132
"""One or more target components specifying which components a dispatch targets.
3233
33-
It can be a list of component IDs or a list of categories.
34+
It can be a list of `ComponentId` instances (representing component IDs)
35+
or a list of `ComponentCategory` instances (representing categories).
3436
"""
3537

3638

@@ -50,7 +52,9 @@ def _target_components_from_protobuf(
5052
"""
5153
match pb_target.WhichOneof("components"):
5254
case "component_ids":
53-
id_list: list[int] = list(pb_target.component_ids.ids)
55+
id_list: list[ComponentId] = list(
56+
map(ComponentId, pb_target.component_ids.ids)
57+
)
5458
return id_list
5559
case "component_categories":
5660
category_list: list[ComponentCategory] = list(
@@ -80,8 +84,11 @@ def _target_components_to_protobuf(
8084
"""
8185
pb_target = PBTargetComponents()
8286
match target:
83-
case list(component_ids) if all(isinstance(id, int) for id in component_ids):
84-
pb_target.component_ids.ids.extend(cast(list[int], component_ids))
87+
case list(component_ids) if all(
88+
isinstance(id, ComponentId) for id in component_ids
89+
):
90+
# pylint: disable-next=unnecessary-lambda
91+
pb_target.component_ids.ids.extend(map(lambda x: int(x), component_ids))
8592
case list(categories) if all(
8693
isinstance(cat, ComponentCategory) for cat in categories
8794
):

tests/test_cli.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from frequenz.client.dispatch.test.client import ALL_KEY, FakeClient
2424
from frequenz.client.dispatch.types import Dispatch
25+
from frequenz.client.microgrid import ComponentId
2526

2627
TEST_NOW = datetime(2023, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
2728
"""Arbitrary time used as NOW for testing."""
@@ -68,7 +69,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
6869
type="test",
6970
start_time=datetime(2023, 1, 1, 0, 0, 0),
7071
duration=timedelta(seconds=3600),
71-
target=[1, 2, 3],
72+
target=list(map(ComponentId, [1, 2, 3])),
7273
active=True,
7374
dry_run=False,
7475
payload={},
@@ -91,7 +92,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
9192
type="test",
9293
start_time=datetime(2023, 1, 1, 0, 0, 0),
9394
duration=timedelta(seconds=3600),
94-
target=[1, 2, 3],
95+
target=list(map(ComponentId, [1, 2, 3])),
9596
active=True,
9697
dry_run=False,
9798
payload={},
@@ -113,7 +114,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
113114
type="test",
114115
start_time=datetime(2023, 1, 1, 0, 0, 0),
115116
duration=timedelta(seconds=3600),
116-
target=[1, 2, 3],
117+
target=list(map(ComponentId, [1, 2, 3])),
117118
active=True,
118119
dry_run=False,
119120
payload={},
@@ -128,7 +129,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
128129
type="test",
129130
start_time=datetime(2023, 1, 1, 0, 0, 0),
130131
duration=timedelta(seconds=3600),
131-
target=[1, 2, 3],
132+
target=list(map(ComponentId, [1, 2, 3])),
132133
active=True,
133134
dry_run=False,
134135
payload={},
@@ -156,7 +157,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
156157
type="test",
157158
start_time=datetime(2023, 1, 1, 0, 0, 0),
158159
duration=timedelta(seconds=3600),
159-
target=[1, 2, 3],
160+
target=list(map(ComponentId, [1, 2, 3])),
160161
active=True,
161162
dry_run=False,
162163
payload={},
@@ -169,7 +170,7 @@ def mock_client(fake_client: FakeClient) -> Generator[None, None, None]:
169170
type="filtered",
170171
start_time=datetime(2023, 1, 1, 0, 0, 0),
171172
duration=timedelta(seconds=1800),
172-
target=[3],
173+
target=list(map(ComponentId, [3])),
173174
active=True,
174175
dry_run=False,
175176
payload={},
@@ -247,7 +248,7 @@ async def test_list_command(
247248
"test",
248249
timedelta(hours=2),
249250
timedelta(seconds=3600),
250-
[1, 2, 3],
251+
list(map(ComponentId, [1, 2, 3])),
251252
{"dry_run": True},
252253
RecurrenceRule(),
253254
0,
@@ -376,7 +377,7 @@ async def test_create_command(
376377
expected_type: str,
377378
expected_start_time_delta: timedelta | Literal["NOW"],
378379
expected_duration: timedelta,
379-
expected_target: list[int] | list[ComponentCategory],
380+
expected_target: list[ComponentId] | list[ComponentCategory],
380381
expected_options: dict[str, Any],
381382
expected_reccurence: RecurrenceRule | None,
382383
expected_return_code: int,
@@ -528,7 +529,7 @@ async def test_create_command(
528529
type="test",
529530
start_time=datetime(2023, 1, 1, 0, 0, 0),
530531
duration=timedelta(seconds=3600),
531-
target=[500, 501],
532+
target=list(map(ComponentId, [500, 501])),
532533
active=True,
533534
dry_run=False,
534535
payload={},
@@ -558,7 +559,7 @@ async def test_create_command(
558559
'{"key": "value"}',
559560
],
560561
{
561-
"target": [400, 401],
562+
"target": list(map(ComponentId, [400, 401])),
562563
"recurrence": RecurrenceRule(
563564
frequency=Frequency.DAILY,
564565
interval=5,
@@ -574,7 +575,7 @@ async def test_create_command(
574575
"payload": {"key": "value"},
575576
},
576577
0,
577-
""" target=[400, 401],
578+
""" target=[ComponentId(400), ComponentId(401)],
578579
active=True,
579580
dry_run=False,
580581
payload={'key': 'value'},
@@ -633,7 +634,7 @@ async def test_update_command(
633634
type="test",
634635
start_time=datetime(2023, 1, 1, 0, 0, 0),
635636
duration=timedelta(seconds=3600),
636-
target=[1, 2, 3],
637+
target=list(map(ComponentId, [1, 2, 3])),
637638
active=True,
638639
dry_run=False,
639640
payload={},
@@ -680,7 +681,7 @@ async def test_get_command(
680681
type="test",
681682
start_time=datetime(2023, 1, 1, 0, 0, 0),
682683
duration=timedelta(seconds=3600),
683-
target=[1, 2, 3],
684+
target=list(map(ComponentId, [1, 2, 3])),
684685
active=True,
685686
dry_run=False,
686687
payload={},

tests/test_proto.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
_target_components_from_protobuf,
1919
_target_components_to_protobuf,
2020
)
21+
from frequenz.client.microgrid import ComponentId
2122

2223

2324
def test_target_components() -> None:
2425
"""Test the target components."""
2526
for components in (
26-
[1, 2, 3],
27-
[10, 20, 30],
27+
list(map(ComponentId, [1, 2, 3])),
28+
list(map(ComponentId, [10, 20, 30])),
2829
[ComponentCategory.BATTERY],
2930
[ComponentCategory.GRID],
3031
[ComponentCategory.METER],
@@ -99,7 +100,7 @@ def test_dispatch() -> None:
99100
start_time=datetime(2024, 10, 10, tzinfo=timezone.utc),
100101
end_time=datetime(2024, 10, 20, tzinfo=timezone.utc),
101102
duration=timedelta(days=10),
102-
target=[1, 2, 3],
103+
target=list(map(ComponentId, [1, 2, 3])),
103104
active=True,
104105
dry_run=False,
105106
payload={"key": "value"},
@@ -163,7 +164,7 @@ def test_dispatch_create_request_with_no_recurrence() -> None:
163164
type="test",
164165
start_time=datetime(2024, 10, 10, tzinfo=timezone.utc),
165166
duration=timedelta(days=10),
166-
target=[1, 2, 3],
167+
target=list(map(ComponentId, [1, 2, 3])),
167168
active=True,
168169
dry_run=False,
169170
payload={"key": "value"},
@@ -180,7 +181,7 @@ def test_dispatch_create_start_immediately() -> None:
180181
type="test",
181182
start_time="NOW",
182183
duration=timedelta(days=10),
183-
target=[1, 2, 3],
184+
target=list(map(ComponentId, [1, 2, 3])),
184185
active=True,
185186
dry_run=False,
186187
payload={"key": "value"},

0 commit comments

Comments
 (0)