Pārlūkot izejas kodu

fix binding removal for non-hashable objects (fixes #2540)

Falko Schindler 1 gadu atpakaļ
vecāks
revīzija
1eb76baec8
4 mainītis faili ar 8 papildinājumiem un 13 dzēšanām
  1. 5 10
      nicegui/binding.py
  2. 1 1
      nicegui/client.py
  3. 1 1
      nicegui/elements/leaflet.py
  4. 1 1
      nicegui/elements/scene.py

+ 5 - 10
nicegui/binding.py

@@ -155,32 +155,27 @@ class BindableProperty:
             self._change_handler(owner, value)
 
 
-def remove(objects: Iterable[Any], type_: Type) -> None:
+def remove(objects: Iterable[Any]) -> None:
     """Remove all bindings that involve the given objects.
 
-    The ``type_`` argument is as a quick pre-filter.
-
     :param objects: The objects to remove.
-    :param type_: The type of the objects to remove.
     """
-    object_set = set(objects)
+    object_ids = set(map(id, objects))
     active_links[:] = [
         (source_obj, source_name, target_obj, target_name, transform)
         for source_obj, source_name, target_obj, target_name, transform in active_links
-        if not (isinstance(source_obj, type_) and source_obj in object_set or
-                isinstance(target_obj, type_) and target_obj in object_set)
+        if id(source_obj) not in object_ids and id(target_obj) not in object_ids
     ]
     for key, binding_list in list(bindings.items()):
         binding_list[:] = [
             (source_obj, target_obj, target_name, transform)
             for source_obj, target_obj, target_name, transform in binding_list
-            if not (isinstance(source_obj, type_) and source_obj in object_set or
-                    isinstance(target_obj, type_) and target_obj in object_set)
+            if id(source_obj) not in object_ids and id(target_obj) not in object_ids
         ]
         if not binding_list:
             del bindings[key]
     for (obj_id, name), obj in list(bindable_properties.items()):
-        if isinstance(obj, type_) and obj in object_set:
+        if id(obj) in object_ids:
             del bindable_properties[(obj_id, name)]
 
 

+ 1 - 1
nicegui/client.py

@@ -292,7 +292,7 @@ class Client:
 
     def remove_elements(self, elements: Iterable[Element]) -> None:
         """Remove the given elements from the client."""
-        binding.remove(elements, Element)
+        binding.remove(elements)
         element_ids = [element.id for element in elements]
         for element_id in element_ids:
             del self.elements[element_id]

+ 1 - 1
nicegui/elements/leaflet.py

@@ -143,5 +143,5 @@ class Leaflet(Element, component='leaflet.js'):
         return self.run_method('run_layer_method', layer_id, name, *args, timeout=timeout, check_interval=check_interval)
 
     def _handle_delete(self) -> None:
-        binding.remove(self.layers, Layer)
+        binding.remove(self.layers)
         super().delete()

+ 1 - 1
nicegui/elements/scene.py

@@ -199,7 +199,7 @@ class Scene(Element,
                         self.camera.up_x, self.camera.up_y, self.camera.up_z, duration)
 
     def _handle_delete(self) -> None:
-        binding.remove(list(self.objects.values()), Object3D)
+        binding.remove(list(self.objects.values()))
         super()._handle_delete()
 
     def delete_objects(self, predicate: Callable[[Object3D], bool] = lambda _: True) -> None: