-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Cylic data dependencies cause infinite recursion #62
Comments
Hi @buckley-w-david, thanks for opening this issue. I had thought about it much earlier for a while, like really thought about it, and I'm hopeful because I think the fix should be really simple. I feel like I was overcomplicating it in my head, because I've always struggled with understanding recursion in any form. Just a stop-gap solution for now, I think once I look into the performance milestones I created, that will involve dynamically generating the (de)serialization code like for The simple workaround I had alluded to involves a few things:
Sorry if that all sounds confusing, but the good news is that I've effectively put together a hotfix for this in under half an hour (not completely there), so I can confirm that the recursion error is able to be resolved at least. I'll see if I can put together something soon that will hopefully shed a little more light on the direction I had in mind to solve the (tricky) self-referential problem. |
A little bump - I am facing the exact same issue. Happy to test a fix if you have one. |
Also a quick question. A work around for me would be to add a custom from_dict for a particular field. I ve read the docs a bit, but am unable to understand how to do this.
|
I've checked in a WIP branch with the working changes. I was able to test with enabling a new meta field FWIW, I've also updated the first comment; probably a typo, but I think the expected
|
@trulite I'll have to dig a bit deeper to confirm, but my initial suspicion is that it won't be possible currently to set a per-field parser to use in from_dict. This is probably a good idea for a feature request though - not saying that it would be needed in this particular case, but I imagine there are other situations where it could be quite useful. |
Thanks for now - I am leaving it as a dict and doing a post process to convert to the class I want |
Is there any update? It looks like a basic issue : recursion should stop when there a from dataclasses import dataclass, field
from typing import List, Type
from dataclass_wizard import JSONWizard
from dataclass_wizard.abstractions import W
from dataclass_wizard.type_def import JSONObject
@dataclass
class A(JSONWizard):
value: int
children: List["A"] = field(default_factory=list)
@classmethod
def from_dict(cls: Type[W], o: JSONObject) -> W:
value = o.pop("value")
children = [A.from_dict(oo) for oo in o.pop("children", [])]
return A(value=value, children=children)
@dataclass
class AA(JSONWizard):
a: A
nested_dict = {
"value": 1,
"children": [{"value": 2, "children": []}, {"value": 3, "children": []}],
}
a_obj = A.from_dict(nested_dict)
aa_obj = AA.from_dict({"a": nested_dict})
print(a_obj)
print(aa_obj) It would be great to have a solution soon! Do you have any updates @rnag ? |
@alexander-belikov Yes I haven't had time to work on it, but the WIP branch seems to fix this issue. I'll need to revisit it with fresh eyes, see what I was doing, and then create a PR once I have resolved any merge conflicts of course! 😅 |
First of all, thanks for this amazing package! I found my way here thanks to this StackOverflow answer, and have been happily using I checked out the old https://github.com/rnag/dataclass-wizard/tree/WIP-support-cyclic-references branch created by @rnag in 2022.
🆕 also rebased this onto the latest |
Done! These changes are now live as of v0.27.0, which adds support for cyclic data dependencies. Also see Release Notes. 🙌 🎉 I also updated the documentation on usage, check out the section on Cyclic or “Recursive” Dataclasses. |
Description
I am modeling some data that is self-referential, as in it may contain references to other object of its own type. In attempting to use
dataclass-wizard
to parse adict
of this data I received the following errorWhat I Did
Here is a simple script to reproduce the issue
Any potential cyclic data dependency will trigger this issue, not just strictly self-referential ones.
I did a bit of digging in the code before submitting the issue to get an understanding of why this was happening, and I'm not sure it's an easy fix 😬
Not really expecting speedy resolution assuming you even want to support this kind of use case.
The text was updated successfully, but these errors were encountered: