How to use the dacite.types.is_optional 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_is_optional_with_optional():
    assert is_optional(Optional[int])
github konradhalas / dacite / tests / test_types.py View on Github external
def test_is_optional_with_optional_of_union():
    assert is_optional(Optional[Union[int, float]])
github konradhalas / dacite / tests / test_types.py View on Github external
def test_is_optional_with_non_optional():
    assert not is_optional(int)
github konradhalas / dacite / dacite / core.py View on Github external
def _build_value_for_union(union: Type, data: Any, config: Config) -> Any:
    types = extract_generic(union)
    if is_optional(union) and len(types) == 2:
        return _build_value(type_=types[0], data=data, config=config)
    for inner_type in types:
        try:
            value = _build_value(type_=inner_type, data=data, config=config)
            if is_instance(value, inner_type):
                return value
        except DaciteError:
            pass
    if not config.check_types:
        return data
    raise UnionMatchError(field_type=union, value=data)
github konradhalas / dacite / dacite / dataclasses.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:  # type: ignore
        return field.default_factory()  # type: ignore
    elif is_optional(field.type):
        return None
    raise DefaultValueNotFoundError()