Closed KasperskyZiv closed 4 years ago
also:
if config.check_types and not is_instance(value, field.type) and not field.type is InitVar:
raise WrongTypeError(field_path=field.name, field_type=field.type, value=value)
it means disabling type check for init var but it's better then rewriting dataclass Init var to actually store value or something
Hi @KasperskyZiv - thank you for reporting this issue.
You have right, it's a bug or maybe I should say - missing feature. Currently dacite
doesn't support InitVar
at all - no matter if you use strict
mode or not.
Minimal example:
@dataclass
class X:
a: int
b: InitVar[int]
def __post_init__(self, b: int) -> None:
pass
result = dacite.from_dict(X, {"a": 1, "b": 2})
It will rise: TypeError: __init__() missing 1 required positional argument: 'b'
It was an attempt to tackle this problem here but it was long time ago and probably it will be easier to start from the scratch.
I will try to provide InitVar
support in the next release.
Yea, i solved it locally with my code, but it's a bit patchy haha.. Anyway thanks!
I user InitVar to initiate some enum from number but it's not in
fields()
so when i userstrict
it throws anUnexpectedDataError
.possible solution is to add a new field function:
and then edit the from_dict code: