Skip to content

Commit

Permalink
Merge pull request #265 from widgetti/fix_memoize_use_thread_none
Browse files Browse the repository at this point in the history
fix: use_thread on memoize returning None after hit
  • Loading branch information
maartenbreddels authored Sep 4, 2023
2 parents 4b54ac8 + 9474620 commit 0662bcb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
5 changes: 3 additions & 2 deletions solara/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0662bcb

Please sign in to comment.