-
-
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.
Merge pull request #178 from Fatal1ty/inspect-signature-with-forward-ref
Use a ForwardRef object as a result of inspect.signature annotations
- Loading branch information
Showing
6 changed files
with
127 additions
and
14 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
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
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
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
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,40 @@ | ||
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 | ||
|
||
|
||
@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 |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
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 | ||
|
||
|
||
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 |