Closed electrocnic closed 3 years ago
Hi @electrocnic - thank you for reporting this issue. I didn't support Type
fields at all. I pushed simple fix to master
so I hope it will help in your case. 🎉
Hi @electrocnic - thank you for reporting this issue. I didn't support
Type
fields at all. I pushed simple fix tomaster
so I hope it will help in your case. 🎉
Wohoo nice! Thanks a lot. I will probably use it sometime in the next year!
Are Forward References also supported now together with the typing library? Like impl_class: typing.Type['TestImpl']
instead of impl_class: typing.Type[TestImpl]
? That could resolve the circular dependency issue... (Didn't try it, just asking)
I think so :)
You have to pass forward_references
argument to Config
class - sth like this:
@dataclass
class X:
t: Type["Y"]
class Y:
pass
result = from_dict(X, {"t": Y}, config=Config(forward_references={"Y": Y}))
assert result == X(t=Y)
perfect :)
Hi, nice tool!
I ran into a few problems where I am not sure whether I am using it wrong or whether dacite does not support it:
I want to define in my configuration jsons which concrete implementation classes my program should use upon loading:
Therefore I was trying to have my dataclass expecting a type instead of a string or something:
Then I load the config file like this:
however I get this error:
dacite.exceptions.WrongTypeError: wrong value type for field "impl_class" - should be "typing.Type[config.TestImpl]" instead of value "<class 'config.TestImpl'>" of type "type"
My current (inconvenient) workaround is to directly instantiate "empty" classes by dacite like this:
... but I do have to provide default "None" arguments for the init() function of my TestImpl class and would seperately make a new instance by calling
config.impl_class.__class__(arg1='not_none', arg2='also_not_none')
So much for this, tried to keep it as short as possible. Maybe you could tell me whether you see a "better" approach than mine? Thanks!
PS: Two more problems which might belong to their own thread, but short: