How to use the dacite.UnionMatchError 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_union.py View on Github external
def test_from_dict_with_union_and_wrong_data():
    @dataclass
    class X:
        i: Union[int, str]

    with pytest.raises(UnionMatchError) as exception_info:
        from_dict(X, {"i": 1.0})

    assert str(exception_info.value) == 'can not match type "float" to any type of "i" union: typing.Union[int, str]'
    assert exception_info.value.field_path == "i"
    assert exception_info.value.field_type == Union[int, str]
    assert exception_info.value.value == 1.0
github konradhalas / dacite / tests / core / test_union.py View on Github external
def test_from_dict_with_union_and_optional_and_wrong_value():
    @dataclass
    class X:
        i: Union[int, Optional[str]]

    with pytest.raises(UnionMatchError) as exception_info:
        from_dict(X, {"i": 1.0})

    assert exception_info.value.field_path == "i"
    assert exception_info.value.field_type == Union[int, str, None]
    assert exception_info.value.value == 1.0
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_union_of_data_classes_and_wrong_data():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        s: str

    @dataclass
    class Z:
        x_or_y: Union[X, Y]

    with pytest.raises(UnionMatchError) as exception_info:
        from_dict(Z, {'x_or_y': {'f': 2.0}})

    assert exception_info.value.field.name == 'x_or_y'
github konradhalas / dacite / dacite.py View on Github external
data=data,
                    outer_config=outer_config,
                    field=field,
                )
            elif _is_data_class_collection(t) and _is_instance(t, data):
                return _inner_from_dict_for_collection(
                    collection=t,
                    data=data,
                    outer_config=outer_config,
                    field=field,
                )
            elif _is_instance(t, data):
                return data
        except DaciteError:
            pass
    raise UnionMatchError(field)