Exemple #1
0
def test_template_converter():
    @konfi.template()
    class Template:
        a: str
        b: str

    raw = [{"a": "lol", "b": 5}]

    v1, = konfi.convert_value(raw, List[Template])
    assert v1.a == "lol"
    assert v1.b == "5"
Exemple #2
0
def load_field_value(obj: Any, field: konfi.Field, value: Any) -> None:
    """Load the given value for a field into the object.

    Raises:
        FieldError: If the value couldn't be converted to the given field
    """
    try:
        if field.converter is None:
            converted = konfi.convert_value(value, field.value_type)
        else:
            converted = konfi.converter._call_converter(
                field.converter, value, field.value_type)
    except konfi.ConversionError as e:
        raise FieldError([field.key], field, str(e)) from e

    setattr(obj, field.attribute, converted)
Exemple #3
0
def location_converter(value: Any) -> Location:
    # this works! If you're not amazed by this then I don't know what's wrong
    # with you
    lat, long = konfi.convert_value(value, Tuple[float, float])
    return Location(lat, long)
Exemple #4
0
def test_union_converter():
    assert konfi.convert_value("5", Union[str, int]) == "5"
    assert konfi.convert_value("5", Union[int, float]) == 5
Exemple #5
0
def test_converters(inp: Any, typ: type, expected: Any):
    assert konfi.convert_value(inp, typ) == expected
Exemple #6
0
def test_tuple_converter():
    assert konfi.convert_value([1, 2], Tuple[int, ...]) == (1, 2)
    assert konfi.convert_value([1, 2], Tuple[int, int]) == (1, 2)
    assert konfi.convert_value([1, "str"], Tuple[Union[int, str], ...]) == (1, "str")
    assert konfi.convert_value([1, "str"], Tuple[int, str]) == (1, "str")