Skip to content

Commit e8d1f3a

Browse files
committed
Allow passing a single configuration file
Both the `ConfigManager` and the `ConfigManagingActor` can now take a single configuration file, both as a `str` or a `Path`. This also fixes a bug where `str` was still accepted, but taken as a `Sequence[str]`. This is a known issue with Python typing: python/mypy#11001 And while we are it, we now also explicitly reject empty sequences, raising a `ValueError` when one is passed. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent b606d98 commit e8d1f3a

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/frequenz/sdk/config/_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ConfigManager(BackgroundService):
5454

5555
def __init__( # pylint: disable=too-many-arguments
5656
self,
57-
config_paths: Sequence[pathlib.Path],
57+
config_paths: str | pathlib.Path | Sequence[pathlib.Path | str],
5858
/,
5959
*,
6060
force_polling: bool = True,

src/frequenz/sdk/config/_managing_actor.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ConfigManagingActor(Actor):
7272
# pylint: disable-next=too-many-arguments
7373
def __init__(
7474
self,
75-
config_paths: abc.Sequence[pathlib.Path | str],
75+
config_paths: str | pathlib.Path | abc.Sequence[pathlib.Path | str],
7676
output: Sender[abc.Mapping[str, Any]],
7777
*,
7878
name: str | None = None,
@@ -93,16 +93,29 @@ def __init__(
9393
force_polling: Whether to force file polling to check for changes.
9494
polling_interval: The interval to poll for changes. Only relevant if
9595
polling is enabled.
96+
97+
Raises:
98+
ValueError: If no configuration path is provided.
9699
"""
97100
super().__init__(name=name)
98-
self._config_paths: list[pathlib.Path] = [
99-
(
100-
config_path
101-
if isinstance(config_path, pathlib.Path)
102-
else pathlib.Path(config_path)
103-
)
104-
for config_path in config_paths
105-
]
101+
match config_paths:
102+
case str():
103+
self._config_paths = [pathlib.Path(config_paths)]
104+
case pathlib.Path():
105+
self._config_paths = [config_paths]
106+
case abc.Sequence() as seq if len(seq) == 0:
107+
raise ValueError("At least one config path is required.")
108+
case abc.Sequence():
109+
self._config_paths = [
110+
(
111+
config_path
112+
if isinstance(config_path, pathlib.Path)
113+
else pathlib.Path(config_path)
114+
)
115+
for config_path in config_paths
116+
]
117+
case unexpected:
118+
assert_never(unexpected)
106119
self._output: Sender[abc.Mapping[str, Any]] = output
107120
self._force_polling: bool = force_polling
108121
self._polling_interval: timedelta = polling_interval

0 commit comments

Comments
 (0)