-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serialization): Add support for *args and **kwargs in to/from_di…
…ct methods
- Loading branch information
Showing
6 changed files
with
192 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "ezserialization" | ||
version = "0.2.11" | ||
version = "0.3.0" | ||
description = "Simple, easy to use & transparent python objects serialization & deserialization." | ||
authors = ["Matas Gumbinas <[email protected]>"] | ||
repository = "https://github.com/gMatas/ezserialization" | ||
|
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
66 changes: 66 additions & 0 deletions
66
tests/ezserialization_tests/test_serializable_decorator.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,66 @@ | ||
import json | ||
from typing import Mapping, cast | ||
|
||
from ezserialization import ( | ||
Serializable, | ||
deserialize, | ||
serializable, | ||
) | ||
|
||
|
||
@serializable # <- valid for serialization | ||
@serializable(name="A") | ||
@serializable(name="XXX") | ||
class _CaseAUsingAutoName(Serializable): | ||
def __init__(self, value: str): | ||
self.value = value | ||
|
||
def to_raw_dict(self) -> dict: | ||
return {"value": self.value} | ||
|
||
def to_dict(self) -> Mapping: | ||
return self.to_raw_dict() | ||
|
||
@classmethod | ||
def from_dict(cls, src: Mapping): | ||
return cls(value=src["value"]) | ||
|
||
@classmethod | ||
def abs_qualname(cls) -> str: | ||
return f"{cls.__module__}.{cls.__qualname__}" | ||
|
||
|
||
@serializable(name="B") # <- valid for serialization | ||
@serializable(name="YYY") | ||
@serializable | ||
@serializable(name="ZZZ") | ||
class _CaseBUsingNameAlias(Serializable): | ||
def __init__(self, value: str): | ||
self.value = value | ||
|
||
def to_dict(self) -> Mapping: | ||
return {"value": self.value} | ||
|
||
@classmethod | ||
def from_dict(cls, src: Mapping): | ||
return cls(value=src["value"]) | ||
|
||
|
||
def test_serialization_typenames_order(): | ||
""" | ||
Expected behaviour: Only the top typename is used to serialize instances. | ||
On the other hand, for deserialization all typenames are valid. | ||
""" | ||
|
||
a = _CaseAUsingAutoName("a") | ||
data = a.to_dict() | ||
|
||
a.from_dict(data) | ||
|
||
assert data["_type_"] == _CaseAUsingAutoName.abs_qualname() | ||
assert a.value == cast(_CaseAUsingAutoName, deserialize(json.loads(json.dumps(data)))).value | ||
|
||
b = _CaseBUsingNameAlias("b") | ||
data = b.to_dict() | ||
assert data["_type_"] == "B" | ||
assert b.value == cast(_CaseBUsingNameAlias, deserialize(json.loads(json.dumps(data)))).value |
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
63 changes: 63 additions & 0 deletions
63
tests/ezserialization_tests/test_to_and_from_dict_methods.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,63 @@ | ||
import json | ||
from abc import ABC | ||
from typing import Mapping, Type, cast, overload | ||
|
||
from ezserialization import ( | ||
Serializable, | ||
deserialize, | ||
serializable, | ||
) | ||
|
||
|
||
class _BaseTestCase(Serializable, ABC): | ||
def __init__(self, value: str): | ||
self.value = value | ||
|
||
def to_dict(self) -> Mapping: | ||
return self.to_raw_dict() | ||
|
||
def to_raw_dict(self) -> dict: | ||
return {"value": self.value} | ||
|
||
|
||
@serializable | ||
class _TestFromDictWithClassmethod(_BaseTestCase): | ||
@classmethod | ||
def from_dict(cls, src: Mapping) -> "_TestFromDictWithClassmethod": | ||
return cls(value=src["value"]) | ||
|
||
|
||
@serializable | ||
class _TestFromDictWithStaticmethod(_BaseTestCase): | ||
@staticmethod | ||
@overload | ||
def from_dict(cls: Type["_TestFromDictWithStaticmethod"], src: Mapping) -> "_TestFromDictWithStaticmethod": ... | ||
|
||
@staticmethod | ||
@overload | ||
def from_dict(*args) -> "_TestFromDictWithStaticmethod": ... | ||
|
||
@staticmethod | ||
def from_dict(*args, **kwargs) -> "_TestFromDictWithStaticmethod": | ||
obj = args[0](value=args[1]["value"]) | ||
assert isinstance(obj, _TestFromDictWithStaticmethod) | ||
return obj | ||
|
||
|
||
def test_from_dict_as_classmethod(): | ||
obj = _TestFromDictWithClassmethod("wow") | ||
obj_dict = obj.to_dict() | ||
assert obj.value == obj.from_dict(obj_dict).value | ||
assert obj.value == _TestFromDictWithClassmethod.from_dict(obj_dict).value | ||
|
||
assert obj.value == cast(_TestFromDictWithClassmethod, deserialize(json.loads(json.dumps(obj_dict)))).value | ||
|
||
|
||
def test_from_dict_as_staticmethod(): | ||
obj = _TestFromDictWithStaticmethod("wow") | ||
obj_dict = obj.to_dict() | ||
assert obj.value == obj.from_dict(obj_dict).value | ||
assert obj.value == _TestFromDictWithStaticmethod.from_dict(obj, obj_dict).value | ||
assert obj.value == _TestFromDictWithStaticmethod.from_dict(_TestFromDictWithStaticmethod, obj_dict).value | ||
|
||
assert obj.value == cast(_TestFromDictWithStaticmethod, deserialize(json.loads(json.dumps(obj_dict)))).value |