-
Notifications
You must be signed in to change notification settings - Fork 40
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
feat: Allow recursive types #290
Conversation
Tried to support recursive data structures. Can you please confirm it works on your code? |
@yukinarit Output
Specifically, it works well with non-recursive data, but seems to raise errors when the data is recursive. from_dict(Foo, {'f': None}) # No Erorr
from_dict(Foo, {'f': {'f': None}}) # Error |
84e9783
to
3ff1cde
Compare
Like this. ``` @DataClass class Foo: f: Optional['Foo'] serde(Foo) ```
3ff1cde
to
debbe15
Compare
Codecov ReportBase: 90.77% // Head: 90.82% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #290 +/- ##
==========================================
+ Coverage 90.77% 90.82% +0.04%
==========================================
Files 11 11
Lines 1648 1678 +30
Branches 349 355 +6
==========================================
+ Hits 1496 1524 +28
- Misses 109 110 +1
- Partials 43 44 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
@masinc Can you try it again? Should be ok now 👍 |
I tried a pattern that I think is commonly used in recursive data structures ( from dataclasses import dataclass
from typing import Optional, Union, Dict, List, Literal
from serde import serde, Untagged, Strict, field, from_dict, to_dict
@dataclass
class JsonSchemaLike:
id: Optional[str] = field(rename="$id")
items: Optional[List["JsonSchemaLike"]] = field()
properties: Optional[Dict[str, "JsonSchemaLike"]] = field()
serde(JsonSchemaLike, type_check=Strict)
print(
to_dict(
JsonSchemaLike(
None,
items=[
JsonSchemaLike(
None,
items=None,
properties={
"a": JsonSchemaLike(
"a",
items=[
JsonSchemaLike("a1", None, None),
JsonSchemaLike("a2", None, None),
],
properties={
"aa": JsonSchemaLike("aa", None, None),
"ab": JsonSchemaLike("ab", None, None),
},
),
"b": JsonSchemaLike("b", None, None),
},
)
],
properties=None,
)
)
)
print(
from_dict(
JsonSchemaLike,
{
"items": [
{
"properties": {
"a": {
"$id": "a",
"items": [{"$id": "a1"}, {"$id": "a2"}],
"properties": {"aa": {"$id": "aa"}, "ab": {"$id": "ab"}},
},
"b": {"$id": "b"},
}
}
]
},
)
)
@dataclass
class ItemA:
type_: Literal["A"] = field(rename="type")
children: Optional[List[Union["ItemA", "ItemB", "ItemC"]]] = field()
@dataclass
class ItemB:
type_: Literal["B"] = field(rename="type")
children: Optional[List[Union["ItemA", "ItemB", "ItemC"]]] = field()
@dataclass
class ItemC:
type_: Literal["C"] = field(rename="type")
serde(ItemA, type_check=Strict, tagging=Untagged)
serde(ItemB, type_check=Strict, tagging=Untagged)
serde(ItemC, type_check=Strict, tagging=Untagged)
print(to_dict(ItemA("A", [ItemB("B", [ItemC("C"), ItemA("A", [ItemB("B", None)])])])))
print(
from_dict(
Union[ItemA, ItemB, ItemC],
{
"type": "A",
"children": [
{
"type": "B",
"children": [
{"type": "C"},
{"type": "A", "children": [{"type": "B"}]},
],
}
],
},
),
) it seems good! |
Awesome 🙂 |
Like this.
Close #287