From 4c04eaeabb64b73bc4467659d144e0c56629ebce Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Fri, 21 Nov 2025 17:05:29 +0100 Subject: [PATCH] Avoid user assigning unimplemented fields on containers. 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 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 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drafthorse/models/container.py b/drafthorse/models/container.py index f5ec87e..30cdfe3 100644 --- a/drafthorse/models/container.py +++ b/drafthorse/models/container.py @@ -1,4 +1,6 @@ class Container: + __slots__ = ("children", "child_type") + def __init__(self, child_type): super().__init__() self.children = [] @@ -26,6 +28,8 @@ def add_from_etree(self, root, strict=True): 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 @@ def add_from_etree(self, root, strict=True): class CurrencyContainer(SimpleContainer): + __slots__ = ("children", "child_type", "namespace", "tag") + def empty_element(self): from .elements import CurrencyElement @@ -65,6 +71,8 @@ def add_from_etree(self, root, strict=True): class IDContainer(SimpleContainer): + __slots__ = ("children", "child_type", "namespace", "tag") + def empty_element(self): from .elements import IDElement @@ -79,6 +87,8 @@ def add_from_etree(self, root, strict=True): class StringContainer(SimpleContainer): + __slots__ = ("children", "child_type", "namespace", "tag") + def empty_element(self): from .elements import StringElement