-
Notifications
You must be signed in to change notification settings - Fork 4
Add backwards-compatible marshmallow 4.x support #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v1.x.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,10 @@ | |
|
|
||
| """Test marshmallow fields and schema.""" | ||
|
|
||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any, Self, cast | ||
|
|
||
| import pytest | ||
| from marshmallow_dataclass import class_schema | ||
|
|
||
| from frequenz.quantities import ( | ||
|
|
@@ -246,3 +246,51 @@ def test_config_schema_dump_default_string() -> None: | |
| "voltage_always_string": "250 kV", | ||
| "temp_never_string": 10.0, | ||
| } | ||
|
|
||
|
|
||
| def test_deprecated_constructor_api() -> None: | ||
| """Test that the deprecated constructor API still works but emits a warning.""" | ||
|
|
||
| @dataclass | ||
| class _SimpleConfig: | ||
| """Test config dataclass.""" | ||
|
|
||
| pct: Percentage = field(default_factory=lambda: Percentage.from_percent(75.0)) | ||
|
|
||
| schema_cls = class_schema(_SimpleConfig, base_schema=QuantitySchema) | ||
|
|
||
| with pytest.warns( | ||
| DeprecationWarning, | ||
| match="Passing 'serialize_as_string_default' to QuantitySchema constructor is deprecated", | ||
| ): | ||
| schema = schema_cls(serialize_as_string_default=True) # type: ignore[call-arg] | ||
|
|
||
| try: | ||
| # Verify it still works | ||
| result = schema.dump(_SimpleConfig()) | ||
| assert result["pct"] == "75 %" | ||
| finally: | ||
| # Reset the context variable to avoid affecting other tests | ||
| serialize_as_string_default.set(False) | ||
|
|
||
|
Comment on lines
252
to
275
|
||
|
|
||
| def test_deprecated_constructor_api_false() -> None: | ||
| """Test that the deprecated constructor API works with False value.""" | ||
|
|
||
| @dataclass | ||
| class _SimpleConfig: | ||
| """Test config dataclass.""" | ||
|
|
||
| pct: Percentage = field(default_factory=lambda: Percentage.from_percent(75.0)) | ||
|
|
||
| schema_cls = class_schema(_SimpleConfig, base_schema=QuantitySchema) | ||
|
|
||
| with pytest.warns( | ||
| DeprecationWarning, | ||
| match="Passing 'serialize_as_string_default' to QuantitySchema constructor is deprecated", | ||
| ): | ||
| schema = schema_cls(serialize_as_string_default=False) # type: ignore[call-arg] | ||
|
|
||
| # Verify it still works (no need to reset since we're setting to False which is the default) | ||
| result = schema.dump(_SimpleConfig()) | ||
| assert result["pct"] == 75.0 | ||
Uh oh!
There was an error while loading. Please reload this page.