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_json_with_custom_decoder():
@dataclass
class DataClass(DataClassJSONMixin):
x: List[int]
x_count: int
def decoder(s):
d = json.loads(s)
d['x_count'] = len(d.get('x', []))
return d
instance = DataClass(x=[1, 2, 3], x_count=3)
dumped = json.dumps({'x': [1, 2, 3]})
assert DataClass.from_json(dumped, decoder=decoder) == instance
with pytest.raises(MissingField):
assert DataClass.from_json(dumped)