Skip to content

Commit 0642d3a

Browse files
committed
Drop support for using marshmallow_dataclas.dataclass directly
`marshmallow_dataclass.dataclass` is intended to be used only when using `my_dataclass.Schema` to get the schema. But using this is not very convenient when using type hints as they are not well supported by `marshmallow`, as the `load()` function can't have hints. This is actually why `load_config()` exists in the first place, so we are using `class_schema()` instead, so we don't really need that our types are decorated with `marshmallow_dataclass`, we can use the built-in `dataclass` instead, we just need to add the appropriate metadata if we want more complex validation. Using `class_shema()` is also necessary to be able to pass a `base_schema`, which we'll need when we want to use schemas with custom fields, like the ones provided by `frequenz.quantities`. Because of this, we just drop support for `marshmallow_dataclass.dataclass` and we'll require that built-in dataclasses are used in the future. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 694ce64 commit 0642d3a

File tree

2 files changed

+3
-23
lines changed

2 files changed

+3
-23
lines changed

RELEASE_NOTES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## Upgrading
88

9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
9+
- `frequenz.sdk.config.load_config()` doesn't accept classes decorated with `marshmallow_dataclass.dataclass` anymore. You should use the built-in `dataclasses.dataclass` directly instead, no other changes should be needed, the metadata in the `dataclass` fields will still be used.
1010

1111
## New Features
1212

tests/config/test_util.py

+2-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import Any
88

99
import marshmallow
10-
import marshmallow_dataclass
1110
import pytest
1211
from pytest_mock import MockerFixture
1312

@@ -18,14 +17,6 @@
1817
class SimpleConfig:
1918
"""A simple configuration class for testing."""
2019

21-
name: str
22-
value: int
23-
24-
25-
@marshmallow_dataclass.dataclass
26-
class MmSimpleConfig:
27-
"""A simple configuration class for testing."""
28-
2920
name: str = dataclasses.field(metadata={"validate": lambda s: s.startswith("test")})
3021
value: int
3122

@@ -37,20 +28,9 @@ def test_load_config_dataclass() -> None:
3728
loaded_config = load_config(SimpleConfig, config)
3829
assert loaded_config == SimpleConfig(name="test", value=42)
3930

40-
config["name"] = "not test"
41-
loaded_config = load_config(SimpleConfig, config)
42-
assert loaded_config == SimpleConfig(name="not test", value=42)
43-
44-
45-
def test_load_config_marshmallow_dataclass() -> None:
46-
"""Test that load_config loads a configuration into a configuration class."""
47-
config: dict[str, Any] = {"name": "test", "value": 42}
48-
loaded_config = load_config(MmSimpleConfig, config)
49-
assert loaded_config == MmSimpleConfig(name="test", value=42)
50-
5131
config["name"] = "not test"
5232
with pytest.raises(marshmallow.ValidationError):
53-
_ = load_config(MmSimpleConfig, config)
33+
_ = load_config(SimpleConfig, config)
5434

5535

5636
def test_load_config_load_None() -> None:
@@ -70,7 +50,7 @@ def test_load_config_type_hints(mocker: MockerFixture) -> None:
7050
config: dict[str, Any] = {}
7151

7252
# We add the type hint to test that the return type (hint) is correct
73-
_: MmSimpleConfig = load_config(MmSimpleConfig, config, marshmallow_arg=1)
53+
_: SimpleConfig = load_config(SimpleConfig, config, marshmallow_arg=1)
7454
mock_class_schema.return_value.load.assert_called_once_with(
7555
config, marshmallow_arg=1
7656
)

0 commit comments

Comments
 (0)