Skip to content

Commit 7d1e106

Browse files
Restore support for marshmallow_dataclass (#1106)
It seems to work with `class_shema()` and new Dataclass protocol recognize it as dataclass.
2 parents 4484c2b + 123258a commit 7d1e106

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

RELEASE_NOTES.md

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
## Upgrading
88

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.
10-
119
## New Features
1210

1311

tests/config/test_util.py

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

99
import marshmallow
10+
import marshmallow_dataclass
1011
import pytest
1112
from pytest_mock import MockerFixture
1213

@@ -21,26 +22,55 @@ class SimpleConfig:
2122
value: int
2223

2324

24-
def test_load_config_dataclass() -> None:
25+
@marshmallow_dataclass.dataclass
26+
class MmSimpleConfig:
27+
"""A simple marshmallow_dataclass configuration class for testing."""
28+
29+
name: str = dataclasses.field(metadata={"validate": lambda s: s.startswith("test")})
30+
value: int
31+
32+
33+
@pytest.mark.parametrize(
34+
"config_class",
35+
[SimpleConfig, MmSimpleConfig],
36+
ids=["dataclass", "marshmallow_dataclass"],
37+
)
38+
def test_load_config_dataclass(
39+
config_class: type[SimpleConfig] | type[MmSimpleConfig],
40+
) -> None:
2541
"""Test that load_config loads a configuration into a configuration class."""
2642
config: dict[str, Any] = {"name": "test", "value": 42}
2743

28-
loaded_config = load_config(SimpleConfig, config)
29-
assert loaded_config == SimpleConfig(name="test", value=42)
44+
loaded_config = load_config(config_class, config)
45+
assert loaded_config == config_class(name="test", value=42)
3046

3147
config["name"] = "not test"
3248
with pytest.raises(marshmallow.ValidationError):
33-
_ = load_config(SimpleConfig, config)
49+
_ = load_config(config_class, config)
3450

3551

36-
def test_load_config_load_None() -> None:
52+
@pytest.mark.parametrize(
53+
"config_class",
54+
[SimpleConfig, MmSimpleConfig],
55+
ids=["dataclass", "marshmallow_dataclass"],
56+
)
57+
def test_load_config_load_None(
58+
config_class: type[SimpleConfig] | type[MmSimpleConfig],
59+
) -> None:
3760
"""Test that load_config raises ValidationError if the configuration is None."""
3861
config: dict[str, Any] = {}
3962
with pytest.raises(marshmallow.ValidationError):
40-
_ = load_config(SimpleConfig, config.get("loggers", None))
63+
_ = load_config(config_class, config.get("loggers", None))
4164

4265

43-
def test_load_config_with_base_schema() -> None:
66+
@pytest.mark.parametrize(
67+
"config_class",
68+
[SimpleConfig, MmSimpleConfig],
69+
ids=["dataclass", "marshmallow_dataclass"],
70+
)
71+
def test_load_config_with_base_schema(
72+
config_class: type[SimpleConfig] | type[MmSimpleConfig],
73+
) -> None:
4474
"""Test that load_config loads a configuration using a base schema."""
4575

4676
class _MyBaseSchema(marshmallow.Schema):
@@ -53,11 +83,11 @@ class Meta:
5383

5484
config: dict[str, Any] = {"name": "test", "value": 42, "extra": "extra"}
5585

56-
loaded_config = load_config(SimpleConfig, config, base_schema=_MyBaseSchema)
57-
assert loaded_config == SimpleConfig(name="test", value=42)
86+
loaded_config = load_config(config_class, config, base_schema=_MyBaseSchema)
87+
assert loaded_config == config_class(name="test", value=42)
5888

5989
with pytest.raises(marshmallow.ValidationError):
60-
_ = load_config(SimpleConfig, config)
90+
_ = load_config(config_class, config)
6191

6292

6393
def test_load_config_type_hints(mocker: MockerFixture) -> None:

0 commit comments

Comments
 (0)