From 5c7cf4469fe5be9f3f482aefd1fb3b3fb94a4b11 Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Sun, 4 Sep 2022 14:17:06 +0200 Subject: [PATCH] Compute meaningful hash and eq on simple type instances --- .../ifcopenshell/entity_instance.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/entity_instance.py b/src/ifcopenshell-python/ifcopenshell/entity_instance.py index 7b04d06387..c1869039da 100644 --- a/src/ifcopenshell-python/ifcopenshell/entity_instance.py +++ b/src/ifcopenshell-python/ifcopenshell/entity_instance.py @@ -234,10 +234,23 @@ class entity_instance(object): def __eq__(self, other): if not isinstance(self, type(other)): return False - return self.wrapped_data == other.wrapped_data + # Proper entity instances have a stable identity by means of the numeric + # step id. Selected type instances (such as IfcPropertySingleValue.NominalValue + # always have id=0, so we compare + if self.id(): + return self.wrapped_data == other.wrapped_data + else: + return (self.is_a(), self[0], self.wrapped_data.file_pointer()) == \ + (other.is_a(), other[0], other.wrapped_data.file_pointer()) def __hash__(self): - return hash((self.id(), self.wrapped_data.file_pointer())) + # Proper entity instances have a stable identity by means of the numeric + # step id. Selected type instances (such as IfcPropertySingleValue.NominalValue + # always have id=0, so we hash + if self.id(): + return hash((self.id(), self.wrapped_data.file_pointer())) + else: + return hash((self.is_a(), self[0], self.wrapped_data.file_pointer())) def __dir__(self): return sorted(