konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 107 forks source link

Unable to Deserialize Circular Dependencies #63

Closed micah-williamson closed 4 years ago

micah-williamson commented 5 years ago

Python supports circular dependencies when an entire package is included through the wildcard. But dacite is unable to deserialize circular dependencies. Example:

from .b import *

@dataclass
class A:
  b: Optional[B]
from .a import *

@dataclass
class B:
  a: Optional[A]

Then when I try to load this:

from models.a import A
dacite.from_dict(data_class=A, data={
  'b': {}
})

I get-

dacite.exceptions.ForwardReferenceError: can not resolve forward reference: name 'B' is not defined

Because it can't find B in the from .b import * statement?

What can I do to get around this issue?

konradhalas commented 4 years ago

hi @iamchairs - thank you for reporting this issue.

TBH I can not reproduce your case - in particular circular dependencies within a package.

Nevertheless you can probably use forward_references parameter to handle this issue - please check example in https://github.com/konradhalas/dacite#forward-references