Repository: josuebrunel/pysxm

Issue:
List of objects in class definition does not serialize
Hi I am new to Python: I have the code


```
class TheSystem(ComplexType):
    tagname='SYSTEM'
    def __init__(self) -> None:
        self.NAME = None
        self.PROPERTIES: list[Property] = None

class Property(ComplexType):
    tagname='PROPERTY'
    def __init__(self) -> None:
        self.NAME = None

```

The XML output I expect when I build up an object of this class and attempt to print it is:

```
<SYSTEM>
  <NAME> Example Name </NAME>
  <PROPERTIES>
    <PROPERTY>
       <NAME>Property 1</NAME>
    </PROPERTY>
     <PROPERTY>
       <NAME>Property 2</NAME>
    </PROPERTY>
     <PROPERTY>
      <NAME>Property 3</NAME>
    </PROPERTY>
      etc..
  </PROPERTIES>
</SYSTEM>
```

How can I achieve this? 

It works If I change list[Property] to Property (self.PROPERTIES: Property = None) e.g. only assign a single property object rather than a list but then I would not see an entire list Properties that I want in my XML. Is this possible?

Currently I get the error 
```
Exception has occurred: AttributeError
'list' object has no attribute 'xml'
```


Discussion:

Patch:
diff --git a/pysxm/pysxm.py b/pysxm/pysxm.py
index 91368b6..36c07c3 100644
--- a/pysxm/pysxm.py
+++ b/pysxm/pysxm.py
@@ -90,6 +90,16 @@ class BaseType(object):
                     continue
                 if is_safe_type(attr):
                     element.append(self.make_element(subelt, attr, nsmap=self.klass.nsmap))
+                elif isinstance(attr, (list,)):
+                    list_element = self.make_element(subelt, None, nsmap=self.klass.nsmap)
+                    for e in attr:
+                        if not isinstance(e, (ComplexType, SimpleType)):
+                            raise Exception("list ({}) values ({}) must be <ComplexType> or <SimpleType>: {} is {} ".format(subelt, attr, e, type(e)))
+                        exml = e.xml
+                        if not is_clean(exml):
+                            continue
+                        list_element.append(exml)            
+                    element.append(list_element)
                 else:
                     xml = attr.xml
                     if not is_clean(xml):


Test patch:
diff --git a/tests/test_pysxm.py b/tests/test_pysxm.py
index a7e0e6c..b3c162c 100644
--- a/tests/test_pysxm.py
+++ b/tests/test_pysxm.py
@@ -441,3 +441,28 @@ def test_element_with_attr_value_0():
     xml = wh.xml
     assert xml.user == 'token'
     assert xml.points == 0
+
+def test_list_subelement():
+
+    from typing import List
+
+    class Property(ComplexType):
+        def __init__(self, name: str, kind: int) -> None:
+            self.name = name
+            self.kind = kind
+
+    class System(ComplexType):
+        def __init__(self, name: str, properties: List[Property]) -> None:
+            self.name = name
+            self.properties: list[Property] =  properties
+
+    s = System("Main", [Property("A", 2), Property("B", 5)])
+    print(s)
+    xml = s.xml
+    assert xml.tag == "system"
+    assert xml.name == "Main"
+    p = xml.properties
+    assert len(p.getchildren()) == 2
+    for sp in p.getchildren():
+        assert sp.name in ("A", "B")
+        assert sp.kind in (2, 5)