How to use the dacite.types.transform_value 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 / test_types.py View on Github external
def test_transform_value_without_matching_type():
    assert transform_value({}, [], str, 1) == 1
github konradhalas / dacite / tests / test_types.py View on Github external
def test_transform_value_with_generic_abstract_collection():
    assert transform_value({str: str}, [], Collection[str], [1]) == ["1"]
github konradhalas / dacite / tests / test_types.py View on Github external
def test_transform_value_with_nested_generic_mapping():
    assert transform_value({str: str, int: int}, [], Dict[str, Dict[str, int]], {1: {2: "3"}}) == {"1": {"2": 3}}
github konradhalas / dacite / tests / test_types.py View on Github external
def test_transform_value_with_matching_type():
    assert transform_value({int: lambda x: x + 1}, [], int, 1) == 2
github konradhalas / dacite / tests / test_types.py View on Github external
def test_transform_value_with_generic_sequence_and_matching_both_item_and_sequence():
    assert transform_value({List[int]: lambda x: list(reversed(x)), int: int}, [], List[int], ["1", "2"]) == [2, 1]
github konradhalas / dacite / tests / test_types.py View on Github external
def test_transform_value_with_generic_mapping():
    assert transform_value({str: str, int: int}, [], Dict[str, int], {1: "2"}) == {"1": 2}
github konradhalas / dacite / tests / test_types.py View on Github external
def test_transform_value_without_matching_generic_sequence():
    assert transform_value({}, [], List[int], {1}) == {1}
github konradhalas / dacite / tests / test_types.py View on Github external
def test_transform_value_with_new_type():
    MyStr = NewType("MyStr", str)

    assert transform_value({MyStr: str.upper, str: str.lower}, [], MyStr, "Test") == "TEST"
github konradhalas / dacite / tests / test_types.py View on Github external
def test_transform_value_with_nested_generic_sequence():
    assert transform_value({str: str}, [], List[List[str]], [[1]]) == [["1"]]
github konradhalas / dacite / dacite / core.py View on Github external
try:
        data_class_hints = get_type_hints(data_class, globalns=config.forward_references)
    except NameError as error:
        raise ForwardReferenceError(str(error))
    data_class_fields = fields(data_class)
    if config.strict:
        extra_fields = set(data.keys()) - {f.name for f in data_class_fields}
        if extra_fields:
            raise UnexpectedDataError(keys=extra_fields)
    for field in data_class_fields:
        field = copy.copy(field)
        field.type = data_class_hints[field.name]
        try:
            try:
                field_data = data[field.name]
                transformed_value = transform_value(
                    type_hooks=config.type_hooks, cast=config.cast, target_type=field.type, value=field_data
                )
                value = _build_value(type_=field.type, data=transformed_value, config=config)
            except DaciteFieldError as error:
                error.update_path(field.name)
                raise
            if config.check_types and not is_instance(value, field.type):
                raise WrongTypeError(field_path=field.name, field_type=field.type, value=value)
        except KeyError:
            try:
                value = get_default_value_for_field(field)
            except DefaultValueNotFoundError:
                if not field.init:
                    continue
                raise MissingValueError(field.name)
        if field.init: