-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from datetime import date | ||
from typing import Dict, Generic, TypeVar | ||
|
||
from mashumaro import DataClassDictMixin | ||
from mashumaro.types import SerializableType | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Foo(Generic[T], SerializableType, use_annotations=True): | ||
a: T | ||
|
||
def __init__(self, a: T) -> None: | ||
self.a = a | ||
|
||
@classmethod | ||
def _deserialize(cls, value: Dict[str, T]) -> Foo[T]: | ||
return cls(**value) | ||
|
||
def _serialize(self) -> Dict[str, T]: | ||
return {"a": self.a} | ||
|
||
def __eq__(self, other: Foo) -> bool: | ||
return self.a == other.a | ||
|
||
def __repr__(self): | ||
return f"Foo(a={self.a})" | ||
|
||
|
||
@dataclass | ||
class Bar(DataClassDictMixin): | ||
x_str: Foo[str] | ||
x_date: Foo[date] | ||
|
||
|
||
def test_generic_serializable_type(): | ||
data = {"x_str": {"a": "2023-11-14"}, "x_date": {"a": "2023-11-14"}} | ||
obj = Bar(Foo("2023-11-14"), Foo(date(2023, 11, 14))) | ||
assert obj.to_dict() == data | ||
assert Bar.from_dict(data) == obj |
48 changes: 48 additions & 0 deletions
48
tests/test_forward_refs/test_generic_serialization_strategy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from datetime import date | ||
from typing import Dict, Generic, TypeVar | ||
|
||
from mashumaro import DataClassDictMixin | ||
from mashumaro.config import BaseConfig | ||
from mashumaro.types import SerializationStrategy | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Foo(Generic[T]): | ||
a: T | ||
|
||
def __init__(self, a: T) -> None: | ||
self.a = a | ||
|
||
def __eq__(self, other: Foo) -> bool: | ||
return self.a == other.a | ||
|
||
def __repr__(self): | ||
return f"Foo(a={self.a})" | ||
|
||
|
||
class FooStrategy(Generic[T], SerializationStrategy): | ||
def deserialize(self, value: Dict[str, T]) -> Foo[T]: | ||
return Foo(**value) | ||
|
||
def serialize(self, value: Foo[T]) -> Dict[str, T]: | ||
return {"a": value.a} | ||
|
||
|
||
@dataclass | ||
class Bar(DataClassDictMixin): | ||
x_str: Foo[str] | ||
x_date: Foo[date] | ||
|
||
class Config(BaseConfig): | ||
serialization_strategy = {Foo: FooStrategy()} | ||
|
||
|
||
def test_generic_serialization_strategy(): | ||
data = {"x_str": {"a": "2023-11-14"}, "x_date": {"a": "2023-11-14"}} | ||
obj = Bar(Foo("2023-11-14"), Foo(date(2023, 11, 14))) | ||
assert obj.to_dict() == data | ||
assert Bar.from_dict(data) == obj |