From dec6b15988b60b02d986a543fdd288bd48d6e448 Mon Sep 17 00:00:00 2001 From: Iisakki Rotko Date: Wed, 21 Feb 2024 09:52:33 +0100 Subject: [PATCH] feat: decrease chances of reactive key collisions --- solara/toestand.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/solara/toestand.py b/solara/toestand.py index af76efc8b..2ea5effac 100644 --- a/solara/toestand.py +++ b/solara/toestand.py @@ -272,10 +272,17 @@ def __init__(self, default_value: S, key=None): self.default_value = default_value cls = type(default_value) if key is None: + import inspect + + for frame in inspect.stack(): + file = frame.filename + if not (file.endswith("solara/toestand.py") or file.endswith("solara/reactive.py")): + break + with KernelStoreValue.scope_lock: index = self._type_counter[cls] self._type_counter[cls] += 1 - key = cls.__module__ + ":" + cls.__name__ + ":" + str(index) + key = file + ":" + cls.__name__ + ":" + str(index) super().__init__(key=key) def initial_value(self) -> S: @@ -290,13 +297,18 @@ def __init__(self, factory: Callable[[], S], key=None): except Exception: prefix = repr(factory) if key is None: + import inspect + + for frame in inspect.stack(): + file = frame.filename + if not (file.endswith("solara/toestand.py") or file.endswith("solara/reactive.py")): + break + with KernelStore.scope_lock: index = self._type_counter[prefix] self._type_counter[prefix] += 1 - try: - key = factory.__module__ + ":" + prefix + ":" + str(index) - except Exception: - key = prefix + ":" + str(index) + + key = file + ":" + prefix + ":" + str(index) super().__init__(key=key) def initial_value(self) -> S: