Loading serializer_utils/deserializer.py +15 −2 Original line number Diff line number Diff line from typing import Optional, Union from typing import Optional, Union, List from typeguard import check_type Loading Loading @@ -114,7 +114,20 @@ def deserialize(rule: Rule, data, try_all=False, key=None): return deserialize(Rule(arg), data, try_all, key) except TypeError: pass 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): Loading test.py +6 −4 Original line number Diff line number Diff line 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 Loading Loading @@ -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(self.tf.__repr__()) return 'Test(tf={},ns={})'\ .format(self.tf.__repr__(), self.ns.__repr__()) if __name__ == '__main__': Loading @@ -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] Loading
serializer_utils/deserializer.py +15 −2 Original line number Diff line number Diff line from typing import Optional, Union from typing import Optional, Union, List from typeguard import check_type Loading Loading @@ -114,7 +114,20 @@ def deserialize(rule: Rule, data, try_all=False, key=None): return deserialize(Rule(arg), data, try_all, key) except TypeError: pass 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): Loading
test.py +6 −4 Original line number Diff line number Diff line 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 Loading Loading @@ -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(self.tf.__repr__()) return 'Test(tf={},ns={})'\ .format(self.tf.__repr__(), self.ns.__repr__()) if __name__ == '__main__': Loading @@ -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]