Added support for lists.

parent eda5934b
from typing import Optional, Union
from typing import Optional, Union, List
from typeguard import check_type
......@@ -114,7 +114,20 @@ def deserialize(rule: Rule, data, try_all=False, key=None):
return deserialize(Rule(arg), data, try_all, key)
except TypeError:
raise TypeError('{} did not match any of {}.'.format(type(data), rule.type.__args__))
raise TypeError('{} did not match any of {} for key {}.'.format(type(data), rule.type.__args__, key))
if type(rule.type) is type(List):
if len(rule.type.__args__) != 1:
raise TypeError(
'Cannot handle list with 0 or more than 1 type arguments.')
if type(data) != list:
raise TypeError(
'Cannot deserialize non-list into list.')
t = rule.type.__args__[0]
result = []
for i, v in enumerate(data):
result.append(deserialize(Rule(t), v, try_all, i))
return result
if issubclass(rule.type, Deserializable):
if not isinstance(data, dict):
from time import sleep
from typing import Optional
from typing import Optional, List
from serializer_utils.deserializer import Deserializable, Rule
from serializer_utils.annotations import abstract, discriminate
......@@ -32,12 +32,13 @@ class RefundLine(ReceiptLine):
return 'RefundLine(super={})'.format(super(RefundLine, self).__repr__())
class Test(Deserializable):
tf: Optional[ReceiptLine]
ns: List[int]
def __repr__(self):
return 'Test(tf={})'.format(
return 'Test(tf={},ns={})'\
.format(, self.ns.__repr__())
if __name__ == '__main__':
......@@ -59,7 +60,8 @@ if __name__ == '__main__':
[Rule(ReceiptLine), {'type': 'other', 'name': 'asdf'}],
[Rule(ReceiptLine), {'type': 'transaction'}],
[Rule(ReceiptLine), {'type': 'transaction', 'name': 'asdf', 'pk': 'no'}],
[Rule(Test), {'tf': {'type': 'refund', 'name': 'asdf', 'pk': 5}}]
[Rule(Test), {'tf': {'type': 'refund', 'name': 'asdf', 'pk': 5},
'ns': [1, 2, 4, 7, 9]}]
[print_result(deserialize, *entry) for entry in fns]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment