How to use dacite - 10 common examples

To help you get started, we’ve selected a few dacite examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_post_init():
    @dataclass
    class X:
        s: str = field(init=False)

    x = X()
    x.s = 'test'

    result = from_dict(X, {'s': 'test'})

    assert result == x
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_nested_prefix():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        x: X

    @dataclass
    class Z:
        y: Y

    result = from_dict(Z, {'y': {'x_i': 1}}, Config(prefixed={'y.x': 'x_'}))

    assert result == Z(y=Y(x=X(i=1)))
github konradhalas / dacite / tests / core / test_optional.py View on Github external
def test_from_dict_with_optional_nested_data_class():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        x: Optional[X]

    result = from_dict(Y, {"x": {"i": 1}})

    assert result == Y(x=X(i=1))
github konradhalas / dacite / tests / core / test_collection.py View on Github external
def test_from_dict_with_generic_collection():
    @dataclass
    class X:
        l: List[int]

    result = from_dict(X, {"l": [1]})

    assert result == X(l=[1])
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_transform_of_missing_optional_field():
    @dataclass
    class X:
        s: Optional[str]

    result = from_dict(X, {}, Config(transform={'s': str.lower}))

    assert result == X(s=None)
github konradhalas / dacite / tests / core / test_base.py View on Github external
def test_from_dict_with_nested_data_class():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        s: str
        x: X

    result = from_dict(Y, {"s": "test", "x": {"i": 1}})

    assert result == Y(s="test", x=X(i=1))
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_missing_optional_nested_data_class():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        x: Optional[X]

    result = from_dict(Y, {})

    assert result == Y(x=None)
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_optional_nested_data_class():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        x: Optional[X]

    result = from_dict(Y, {'x': {'i': 1}})

    assert result == Y(x=X(i=1))
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_transform_of_none_optional_field():
    @dataclass
    class X:
        s: Optional[str]

    result = from_dict(X, {'s': None}, Config(transform={'s': str.lower}))

    assert result == X(s=None)
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_prefix():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        s: str
        x: X

    result = from_dict(Y, {'s': 'test', 'x_i': 1}, Config(prefixed={'x': 'x_'}))

    assert result == Y(s='test', x=X(i=1))