From cce2fee1fadbe8fda533f804d5c273ee05af6eed Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 25 Apr 2024 12:14:55 +0500 Subject: [PATCH] More descriptive error setting non-present attributes Example: ``` import ifcopenshell ifc_file = ifcopenshell.file(schema="IFC2X3") # ifc_file.begin_transaction() wall = ifc_file.createIfcWall() wall.Identification = "25" # Before: # Traceback (most recent call last): # File "test.py", line 4, in # wall.Identification = "25" # ^^^^^^^^^^^^^^^^^^^ # File "\ifcopenshell\entity_instance.py", line 279, in __setattr__ # self[index] = value # ~~~~^^^^^^^ # File "\ifcopenshell\entity_instance.py", line 293, in __setitem__ # method = self.method_list[idx] # ~~~~~~~~~~~~~~~~^^^^^ # IndexError: list index out of range # or this (if file had a transaction going) # File "\ifcopenshell\entity_instance.py", line 279, in __setattr__ # self[index] = value # ~~~~^^^^^^^ # File "\ifcopenshell\entity_instance.py", line 288, in __setitem__ # self.wrapped_data.file.transaction.store_edit(self, idx, value) # File "\ifcopenshell\file.py", line 101, in store_edit # "old": self.serialise_value(element, element[index]), # ~~~~~~~^^^^^^^ # File "\ifcopenshell\entity_instance.py", line 283, in __getitem__ # raise IndexError("Attribute index {} out of range for instance of type {}".format(key, self.is_a())) # IndexError: Attribute index 4294967295 out of range for instance of type IfcWall # After: # Traceback (most recent call last): # File "test.py", line 4, in # wall.Identification = "25" # ^^^^^^^^^^^^^^^^^^^ # File "ifcopenshell\entity_instance.py", line 283, in __setattr__ # raise AttributeError( # AttributeError: entity instance of type 'IFC2X3.IfcWall' has no attribute 'Identification' ``` --- .../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 00e441d9ac..c6802aa4a3 100644 --- a/src/ifcopenshell-python/ifcopenshell/entity_instance.py +++ b/src/ifcopenshell-python/ifcopenshell/entity_instance.py @@ -276,7 +276,15 @@ class entity_instance(object): def __setattr__(self, key: str, value: Any) -> None: index = self.wrapped_data.get_argument_index(key) - self[index] = value + try: + self[index] = value + except IndexError as e: + # get_argument_index returns 0xFFFFFFFF if attribute is not found + if index == 0xFFFFFFFF: + raise AttributeError( + "entity instance of type '%s' has no attribute '%s'" % (self.wrapped_data.is_a(True), key) + ) + raise e def __getitem__(self, key: int) -> Any: if key < 0 or key >= len(self):