How to use the dacite.MissingValueError function in dacite

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 / core / test_optional.py View on Github external
def test_from_dict_with_optional_nested_data_class_and_missing_value():
    @dataclass
    class X:
        i: int
        j: int

    @dataclass
    class Y:
        x: Optional[X]

    with pytest.raises(MissingValueError) as exception_info:
        from_dict(Y, {"x": {"i": 1}})

    assert exception_info.value.field_path == "x.j"
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_without_required_value():
    @dataclass
    class X:
        s: str
        i: int

    with pytest.raises(MissingValueError) as exception_info:
        from_dict(X, {'s': 'test'})

    assert exception_info.value.field.name == 'i'
github konradhalas / dacite / tests / core / test_base.py View on Github external
def test_from_dict_with_missing_value():
    @dataclass
    class X:
        s: str
        i: int

    with pytest.raises(MissingValueError) as exception_info:
        from_dict(X, {"s": "test"})

    assert str(exception_info.value) == 'missing value for field "i"'
    assert exception_info.value.field_path == "i"
github konradhalas / dacite / tests / core / test_base.py View on Github external
def test_from_dict_with_missing_value_of_nested_data_class():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        x: X

    with pytest.raises(MissingValueError) as exception_info:
        from_dict(Y, {"x": {}})

    assert exception_info.value.field_path == "x.i"
github konradhalas / dacite / dacite.py View on Github external
def _get_default_value_for_field(field: Field) -> Any:
    if field.default != MISSING:
        return field.default
    elif field.default_factory != MISSING:
        return field.default_factory()
    elif _is_optional(field.type):
        return None
    else:
        raise MissingValueError(field)