konradhalas / dacite

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

fixed from_dict in case of frozen dataclass with non-init default #196

Closed das-intensity closed 1 year ago

das-intensity commented 1 year ago

Small fix to from_dict to fix the case where:

Test tests/core/test_frozen_non_init_var::test_from_dict_with_frozen_non_init_var_with_default added

closes #195

konradhalas commented 1 year ago

@das-intensity thank you for your PR. It looks very good, but it doesn't work with this case:

def test_from_dict_with_optional_non_init_field():
    @dataclass
    class X:
        s: Optional[str] = field(init=False)

    x = X()
    x.s = None

    result = from_dict(X, {})

    assert result == x

My solution (https://github.com/konradhalas/dacite/commit/667dfbae0df04a963f5b64db5cdd86ba38d9695e) covers both cases.