diff --git a/kformat/kclass.py b/kformat/kclass.py index 2c68988..b6cc87d 100644 --- a/kformat/kclass.py +++ b/kformat/kclass.py @@ -20,6 +20,12 @@ def init(self, *args): assert isinstance(v, prop), \ f'{type(v).__name__} is not type of {prop.__name__}' prop_bytes.append(prop.bytes) + elif hasattr(prop, '__origin__') and prop.__origin__ == list: + assert isinstance(v, list), f'{type(v).__name__} is not List' + assert all( + getattr(item, KCLASS_ANNOTATION, False) for item in v + ), f'All of list items should be type of K-Class' + prop_bytes.extend(c.bytes for c in v) else: assert isinstance(prop, kproperty.KProperty), \ f'{prop.__name__} is not subtype of KProperty' diff --git a/tests/test_kclass.py b/tests/test_kclass.py index e1d047d..7b40b47 100644 --- a/tests/test_kclass.py +++ b/tests/test_kclass.py @@ -1,4 +1,5 @@ import unittest +from typing import List from kformat.kclass import kclass from kformat.kproperty import AN, N @@ -18,12 +19,14 @@ class Something: n: N(10) an: AN(20) other: Other + others: List[Other] filler: AN(100) sth = Something( 123, 'k-class', Other(456, 'subclass'), + [], None ) self.assertIsNotNone(sth) @@ -42,11 +45,12 @@ class Something: a: N(10) b: AN(20) other: Other + others: List[Other] c: N(5) d: AN(10) - sth = Something(1, 2, Other(3, 4), 5, 6) - self.assertEqual(sth.bytes, b'NANANNNAN') + sth = Something(1, 2, Other(3, 4), [Other(1, 1), Other(1, 1)], 5, 6) + self.assertEqual(sth.bytes, b'NANANNANNANNNAN') # Reset to_bytes funcs to default N.to_bytes = _n