Пример #1
0
 def get_value(self, field: Field, data: Data) -> Any:
     if field.name in self.flattened or field.name in self.prefixed:
         if field.name in self.flattened:
             value = data
         else:
             value = self._extract_nested_dict_for_prefix(
                 self.prefixed[field.name], data)
     else:
         try:
             key_name = self.remap.get(field.name, field.name)
             value = data[key_name]
         except KeyError:
             raise ValueNotFoundError()
         if field.name in self.transform:
             value = self.transform[field.name](value)
         if field.name in self.cast:
             value = cast_value(field.type, value)
     return value
Пример #2
0
def test_cast_value_with_generic_mapping():
    assert cast_value(Dict[str, int], {1: "1"}) == {"1": 1}
Пример #3
0
def test_cast_value_with_generic_sequence():
    assert cast_value(List[int], ["1"]) == [1]
Пример #4
0
def test_cast_value_with_optional():
    assert cast_value(Optional[int], "1") == 1
Пример #5
0
def test_cast_value_with_built_in_type():
    assert cast_value(int, "1") == 1