Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_from_dict_with_optional_nested_data_class_and_missing_value():
@dataclass
class X:
i: int
j: int
@dataclass
class Y:
x: Optional[X]
with pytest.raises(MissingValueError) as exception_info:
from_dict(Y, {"x": {"i": 1}})
assert exception_info.value.field_path == "x.j"
def test_from_dict_without_required_value():
@dataclass
class X:
s: str
i: int
with pytest.raises(MissingValueError) as exception_info:
from_dict(X, {'s': 'test'})
assert exception_info.value.field.name == 'i'
def test_from_dict_with_missing_value():
@dataclass
class X:
s: str
i: int
with pytest.raises(MissingValueError) as exception_info:
from_dict(X, {"s": "test"})
assert str(exception_info.value) == 'missing value for field "i"'
assert exception_info.value.field_path == "i"
def test_from_dict_with_missing_value_of_nested_data_class():
@dataclass
class X:
i: int
@dataclass
class Y:
x: X
with pytest.raises(MissingValueError) as exception_info:
from_dict(Y, {"x": {}})
assert exception_info.value.field_path == "x.i"
def _get_default_value_for_field(field: Field) -> Any:
if field.default != MISSING:
return field.default
elif field.default_factory != MISSING:
return field.default_factory()
elif _is_optional(field.type):
return None
else:
raise MissingValueError(field)