Skip to content

Commit

Permalink
add test for warning
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Jun 3, 2024
1 parent 0585197 commit 4badc90
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/superqt/utils/_throttler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from __future__ import annotations

import warnings
from concurrent.futures import Future
from contextlib import suppress
from enum import IntFlag, auto
Expand Down Expand Up @@ -223,6 +224,9 @@ def _weak_func(func: Callable[P, R]) -> Callable[P, R]:
def weak_func(*args, **kwargs):
if method := weak_method():
return method(*args, **kwargs)
warnings.warn(
"Method has been garbage collected", RuntimeWarning, stacklevel=2
)

return weak_func

Expand Down
6 changes: 6 additions & 0 deletions tests/test_throttler.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ def tmethod(self, x: int = 1) -> None:
qtbot.waitUntil(thing.tmethod._future.done, timeout=2000)
assert mock.call_count == 2

wm = thing.tmethod
assert isinstance(wm, ThrottledCallable)
del thing
gc.collect()
assert thing_ref() is None

with pytest.warns(RuntimeWarning, match="Method has been garbage collected"):
wm()
wm._set_future_result()

0 comments on commit 4badc90

Please sign in to comment.