]> git.ipfire.org Git - thirdparty/python-drafthorse.git/commitdiff
Avoid user assigning unimplemented fields on containers. (#102)
authorJulien Palard <julien@palard.fr>
Thu, 27 Nov 2025 19:54:03 +0000 (20:54 +0100)
committerGitHub <noreply@github.com>
Thu, 27 Nov 2025 19:54:03 +0000 (20:54 +0100)
Before:

    doc.trade.settlement.payment_means.type_code = "30"

was allowed but had no effect as payemnt_means is a container.

Now it gives:

    Traceback (most recent call last):
      File "/home/mdk/src/python-drafthorse/test.py", line 54, in <module>
        doc.trade.settlement.payment_means.type_code = "30"  # Virement
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: 'Container' object has no attribute 'type_code' and no __dict__ for setting new attributes

drafthorse/models/container.py

index f5ec87e7a304cb217ebf3efcf56d03f59bffbc18..30cdfe3c518ac304600ea0cfc27ea2a63177fea2 100644 (file)
@@ -1,4 +1,6 @@
 class Container:
+    __slots__ = ("children", "child_type")
+
     def __init__(self, child_type):
         super().__init__()
         self.children = []
@@ -26,6 +28,8 @@ class Container:
 
 
 class SimpleContainer(Container):
+    __slots__ = ("children", "child_type", "namespace", "tag")
+
     def __init__(self, child_type, namespace, tag):
         super().__init__(child_type)
         self.namespace = namespace
@@ -51,6 +55,8 @@ class SimpleContainer(Container):
 
 
 class CurrencyContainer(SimpleContainer):
+    __slots__ = ("children", "child_type", "namespace", "tag")
+
     def empty_element(self):
         from .elements import CurrencyElement
 
@@ -65,6 +71,8 @@ class CurrencyContainer(SimpleContainer):
 
 
 class IDContainer(SimpleContainer):
+    __slots__ = ("children", "child_type", "namespace", "tag")
+
     def empty_element(self):
         from .elements import IDElement
 
@@ -79,6 +87,8 @@ class IDContainer(SimpleContainer):
 
 
 class StringContainer(SimpleContainer):
+    __slots__ = ("children", "child_type", "namespace", "tag")
+
     def empty_element(self):
         from .elements import StringElement