How to use the dacite.WrongTypeError 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_none_for_non_optional_field():
    @dataclass
    class X:
        s: str

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

    assert exception_info.value.field_path == "s"
    assert exception_info.value.field_type == str
    assert exception_info.value.value is None
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_with_none_for_non_optional_field():
    @dataclass
    class X:
        s: str

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

    assert exception_info.value.field.name == 's'
    assert exception_info.value.value is None
github konradhalas / dacite / tests / core / test_optional.py View on Github external
def test_from_dict_with_wrong_type_of_optional_value():
    @dataclass
    class X:
        s: Optional[str]
        i: int

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"s": 1, "i": 1})

    assert exception_info.value.field_path == "s"
    assert exception_info.value.field_type == Optional[str]
github konradhalas / dacite / tests / core / test_collection.py View on Github external
def test_from_dict_with_wrong_type_of_dict_value():
    @dataclass
    class X:
        d: Dict[str, int]

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"d": {"a": "test"}})

    assert exception_info.value.field_path == "d"
    assert exception_info.value.field_type == Dict[str, int]
github konradhalas / dacite / tests / core / test_base.py View on Github external
def test_from_dict_with_wrong_type():
    @dataclass
    class X:
        s: str
        i: int

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

    assert str(exception_info.value) == 'wrong type for field "i" - should be "int" instead of "str"'
    assert exception_info.value.field_path == "i"
    assert exception_info.value.field_type == int
    assert exception_info.value.value == "wrong"
github konradhalas / dacite / tests / core / test_collection.py View on Github external
def test_from_dict_with_wrong_type_of_collection_item():
    @dataclass
    class X:
        l: List[int]

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"l": ["1"]})

    assert exception_info.value.field_path == "l"
    assert exception_info.value.field_type == List[int]
github konradhalas / dacite / tests.py View on Github external
def test_from_dict_from_incorrect_data():
    @dataclass
    class X:
        s: str
        i: int

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

    assert exception_info.value.field.name == 'i'
    assert exception_info.value.value == 'wrong'
github konradhalas / dacite / dacite.py View on Github external
collection=_extract_data_class_collection(field.type),
                        data=value,
                        outer_config=config,
                        field=field,
                    )
                elif _has_data_class(field.type):
                    value = _inner_from_dict_for_dataclass(
                        data_class=_extract_data_class(field.type),
                        data=value,
                        outer_config=config,
                        field=field,
                    )
                if field.name in config.cast:
                    value = _cast_value(field.type, value)
            if not _is_instance(field.type, value):
                raise WrongTypeError(field, value)
        if field.init:
            init_values[field.name] = value
        else:
            post_init_values[field.name] = value

    return _create_instance(
        data_class=data_class,
        init_values=init_values,
        post_init_values=post_init_values,
    )