Skip to content

Commit 2d0888d

Browse files
committed
Add a base config schema that provides quantities support
This schema is provided to use as a default, and might be extended in the future to support more commonly used fields that are not provided by marshmallow by default. To use the quantity schema we need to bump the `frequenz-quantities` dependency and add the optional `marshmallow` dependency. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 7db4f74 commit 2d0888d

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies = [
3131
# (plugins.mkdocstrings.handlers.python.import)
3232
"frequenz-client-microgrid >= 0.6.0, < 0.7.0",
3333
"frequenz-channels >= 1.4.0, < 2.0.0",
34-
"frequenz-quantities >= 1.0.0rc3, < 2.0.0",
34+
"frequenz-quantities[marshmallow] >= 1.0.0, < 2.0.0",
3535
"networkx >= 2.8, < 4",
3636
"numpy >= 1.26.4, < 2",
3737
"typing_extensions >= 4.6.1, < 5",

src/frequenz/sdk/config/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
"""Configuration management."""
55

6+
from ._base_schema import BaseConfigSchema
67
from ._logging_actor import LoggerConfig, LoggingConfig, LoggingConfigUpdatingActor
78
from ._manager import ConfigManager, InvalidValueForKeyError
89
from ._managing_actor import ConfigManagingActor
910
from ._util import load_config
1011

1112
__all__ = [
13+
"BaseConfigSchema",
1214
"ConfigManager",
1315
"ConfigManagingActor",
1416
"InvalidValueForKeyError",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# License: MIT
2+
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Base schema for configuration classes."""
5+
6+
from frequenz.quantities.experimental.marshmallow import QuantitySchema
7+
8+
9+
class BaseConfigSchema(QuantitySchema):
10+
"""A base schema for configuration classes."""

src/frequenz/sdk/config/_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing_extensions import override
1717

1818
from ..actor._background_service import BackgroundService
19+
from ._base_schema import BaseConfigSchema
1920
from ._managing_actor import ConfigManagingActor
2021
from ._util import DataclassT, load_config
2122

@@ -139,7 +140,7 @@ def new_receiver( # pylint: disable=too-many-arguments
139140
/,
140141
*,
141142
skip_unchanged: bool = True,
142-
base_schema: type[Schema] | None = None,
143+
base_schema: type[Schema] | None = BaseConfigSchema,
143144
marshmallow_load_kwargs: dict[str, Any] | None = None,
144145
) -> Receiver[DataclassT | Exception | None]:
145146
"""Create a new receiver for receiving the configuration for a particular key.
@@ -387,7 +388,7 @@ def _load_config(
387388
config_class: type[DataclassT],
388389
*,
389390
key: str | Sequence[str],
390-
base_schema: type[Schema] | None = None,
391+
base_schema: type[Schema] | None = BaseConfigSchema,
391392
marshmallow_load_kwargs: dict[str, Any] | None = None,
392393
) -> DataclassT | InvalidValueForKeyError | ValidationError | None:
393394
"""Try to load a configuration and log any validation errors."""

0 commit comments

Comments
 (0)