From 0043d73ae3bf392807a911670c6e2421353d5186 Mon Sep 17 00:00:00 2001 From: Mario Buikhuizen Date: Fri, 1 Sep 2023 14:52:03 +0200 Subject: [PATCH 1/2] test: add test for use_thread on memoize returning None after hit --- tests/unit/cache_test.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/unit/cache_test.py b/tests/unit/cache_test.py index 295d3dddf..ca28c67c0 100644 --- a/tests/unit/cache_test.py +++ b/tests/unit/cache_test.py @@ -252,3 +252,41 @@ def f(x: int) -> int: # we should directly get the result from the cache, so we don't go into running state assert result_values[0].state == solara.ResultState.FINISHED assert result_values[0].value == 100 + + +def test_memoize_hook_no_None_after_hit(): + has_been_none = False + + selected = solara.Reactive("1") + + @solara.memoize + def something(i): + return i + + @solara.component + def Test(): + result = something.use_thread(selected.value) + + if result.state == solara.ResultState.FINISHED: + if result.value is None: + # this should not happen + nonlocal has_been_none + has_been_none = True + + solara.Text(str(result.value)) + + box, rc = solara.render(Test(), handle_error=False) + rc.find(v.Html, children=["1"]).wait_for(timeout=2) + + assert not has_been_none + selected.set("2") + rc.find(v.Html, children=["2"]).wait_for(timeout=2) + assert not has_been_none + + selected.set("1") + rc.find(v.Html, children=["1"]).wait_for(timeout=2) + assert not has_been_none + + selected.set("3") + rc.find(v.Html, children=["3"]).wait_for(timeout=2) + assert not has_been_none From 9474620376dbc892d124ff00919c23ba79c31014 Mon Sep 17 00:00:00 2001 From: Mario Buikhuizen Date: Fri, 1 Sep 2023 15:11:32 +0200 Subject: [PATCH 2/2] fix: use_thread on memoize returning None after hit --- solara/cache.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/solara/cache.py b/solara/cache.py index 9bf582ab5..60585d8c1 100644 --- a/solara/cache.py +++ b/solara/cache.py @@ -128,8 +128,9 @@ def do_work(): self.storage[key] = new_value return new_value else: - # we don't use the return value if value is _DOES_NOT_EXIST - return None + # although we don't use the return value directly, it's still used on the next time result_thread is + # returned. + return value result_thread: solara.Result[R] = solara.use_thread(do_work, dependencies=[key], intrusive_cancel=self.intrusive_cancel) if value is _DOES_NOT_EXIST: