Skip to content

Commit 4cea8eb

Browse files
authored
Make DispatchInfo c'tor accept components (#201)
To stay backwards compatible.
2 parents 4930eee + 456d72c commit 4cea8eb

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/frequenz/dispatch/_actor_dispatcher.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from dataclasses import dataclass
1010
from datetime import timedelta
1111
from typing import Any, Awaitable
12+
from warnings import warn
1213

1314
from frequenz.channels import Broadcast, Receiver, Sender, select
1415
from frequenz.channels.timer import SkipMissedAndDrift, Timer
@@ -60,6 +61,46 @@ def components(self) -> TargetComponents:
6061
_src: Dispatch
6162
"""The dispatch that triggered this update."""
6263

64+
def __init__(
65+
self,
66+
*,
67+
target: TargetComponents | None = None,
68+
components: TargetComponents | None = None,
69+
dry_run: bool,
70+
options: dict[str, Any],
71+
_src: Dispatch,
72+
) -> None:
73+
"""Initialize the DispatchInfo.
74+
75+
Args:
76+
target: Target components to be used.
77+
components: Deprecated alias for `target`.
78+
dry_run: Whether this is a dry run.
79+
options: Additional options.
80+
_src: The dispatch that triggered this update.
81+
82+
Raises:
83+
ValueError: If both `target` and `components` are set, or if neither is set.
84+
"""
85+
if target is not None and components is not None:
86+
raise ValueError("Only one of 'target' or 'components' can be set.")
87+
88+
# Use components if target is not provided (backwards compatibility)
89+
if target is None:
90+
if components is None:
91+
raise ValueError("One of 'target' or 'components' must be set.")
92+
target = components
93+
warn(
94+
"'components' is deprecated, use 'target' instead.",
95+
DeprecationWarning,
96+
stacklevel=2,
97+
)
98+
99+
object.__setattr__(self, "target", target)
100+
object.__setattr__(self, "dry_run", dry_run)
101+
object.__setattr__(self, "options", options)
102+
object.__setattr__(self, "_src", _src)
103+
63104

64105
class ActorDispatcher(BackgroundService):
65106
"""Helper class to manage actors based on dispatches.

0 commit comments

Comments
 (0)