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_post_init():
@dataclass
class X:
s: str = field(init=False)
x = X()
x.s = 'test'
result = from_dict(X, {'s': 'test'})
assert result == x
def test_from_dict_with_nested_prefix():
@dataclass
class X:
i: int
@dataclass
class Y:
x: X
@dataclass
class Z:
y: Y
result = from_dict(Z, {'y': {'x_i': 1}}, Config(prefixed={'y.x': 'x_'}))
assert result == Z(y=Y(x=X(i=1)))
def test_from_dict_with_optional_nested_data_class():
@dataclass
class X:
i: int
@dataclass
class Y:
x: Optional[X]
result = from_dict(Y, {"x": {"i": 1}})
assert result == Y(x=X(i=1))
def test_from_dict_with_generic_collection():
@dataclass
class X:
l: List[int]
result = from_dict(X, {"l": [1]})
assert result == X(l=[1])
def test_from_dict_with_transform_of_missing_optional_field():
@dataclass
class X:
s: Optional[str]
result = from_dict(X, {}, Config(transform={'s': str.lower}))
assert result == X(s=None)
def test_from_dict_with_nested_data_class():
@dataclass
class X:
i: int
@dataclass
class Y:
s: str
x: X
result = from_dict(Y, {"s": "test", "x": {"i": 1}})
assert result == Y(s="test", x=X(i=1))
def test_from_dict_with_missing_optional_nested_data_class():
@dataclass
class X:
i: int
@dataclass
class Y:
x: Optional[X]
result = from_dict(Y, {})
assert result == Y(x=None)
def test_from_dict_with_optional_nested_data_class():
@dataclass
class X:
i: int
@dataclass
class Y:
x: Optional[X]
result = from_dict(Y, {'x': {'i': 1}})
assert result == Y(x=X(i=1))
def test_from_dict_with_transform_of_none_optional_field():
@dataclass
class X:
s: Optional[str]
result = from_dict(X, {'s': None}, Config(transform={'s': str.lower}))
assert result == X(s=None)
def test_from_dict_with_prefix():
@dataclass
class X:
i: int
@dataclass
class Y:
s: str
x: X
result = from_dict(Y, {'s': 'test', 'x_i': 1}, Config(prefixed={'x': 'x_'}))
assert result == Y(s='test', x=X(i=1))