From 2b926f790676b274287163b9afef99fdf00cab76 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Fri, 26 Apr 2024 18:16:38 +0500 Subject: [PATCH] more descriptive error setting non-optional attributes with None Example: ``` import ifcopenshell import ifcopenshell.api.owner.settings ifc_file = ifcopenshell.file(schema="IFC2X3") ifc_file.createIfcOwnerHistory(OwningUser=None) # Before: # Traceback (most recent call last): # File "\test.py", line 16, in # ifcopenshell.api.run("owner.create_owner_history", ifc_file) # File "\ifcopenshell\api\__init__.py", line 172, in run # result = usecase_class(ifc_file, **settings).execute() # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # File "\ifcopenshell\api\owner\create_owner_history.py", line 111, in execute # return self.file.create_entity( # ^^^^^^^^^^^^^^^^^^^^^^^^ # File "\ifcopenshell\file.py", line 350, in create_entity # e[idx] = arg # ~^^^^^ # File "\ifcopenshell\entity_instance.py", line 305, in __setitem__ # self.wrapped_data.setArgumentAsNull(idx) # File "\ifcopenshell\ifcopenshell_wrapper.py", line 5285, in setArgumentAsNull # return _ifcopenshell_wrapper.entity_instance_setArgumentAsNull(self, i) # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # RuntimeError: Attribute not set # After: # Traceback (most recent call last): # File "\test.py", line 16, in # ifcopenshell.api.run("owner.create_owner_history", ifc_file) # File "\ifcopenshell\api\__init__.py", line 172, in run # result = usecase_class(ifc_file, **settings).execute() # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # File "\ifcopenshell\api\owner\create_owner_history.py", line 111, in execute # return self.file.create_entity( # ^^^^^^^^^^^^^^^^^^^^^^^^ # File "\ifcopenshell\file.py", line 350, in create_entity # e[idx] = arg # ~^^^^^ # File "\ifcopenshell\entity_instance.py", line 316, in __setitem__ # raise ValueError( # ValueError: attribute 'OwningUser' is not optional for entity instance of type 'IFC2X3.IfcOwnerHistory' ``` --- .../ifcopenshell/entity_instance.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ifcopenshell-python/ifcopenshell/entity_instance.py b/src/ifcopenshell-python/ifcopenshell/entity_instance.py index 6fba487e6c..b94aedac41 100644 --- a/src/ifcopenshell-python/ifcopenshell/entity_instance.py +++ b/src/ifcopenshell-python/ifcopenshell/entity_instance.py @@ -303,7 +303,15 @@ class entity_instance(object): if value is None: if method is not set_derived_attribute: - self.wrapped_data.setArgumentAsNull(idx) + try: + self.wrapped_data.setArgumentAsNull(idx) + except RuntimeError as e: + if e.args == ("Attribute not set",): + raise ValueError( + "attribute '%s' is not optional for entity instance of type '%s'" + % (self.wrapped_data.get_argument_name(idx), self.wrapped_data.is_a(True)) + ) + raise e else: self.method_list[idx](self.wrapped_data, idx, entity_instance.unwrap_value(value))