Procházet zdrojové kódy

ensure timer runs awaitable

Rodja Trappe před 1 rokem
rodič
revize
401d61e32d
3 změnil soubory, kde provedl 18 přidání a 2 odebrání
  1. 2 2
      nicegui/functions/timer.py
  2. 1 0
      tests/screen.py
  3. 15 0
      tests/test_timer.py

+ 2 - 2
nicegui/functions/timer.py

@@ -1,6 +1,6 @@
 import asyncio
 import time
-from typing import Any, Callable, Optional
+from typing import Any, Awaitable, Callable, Optional
 
 from .. import background_tasks, globals
 from ..binding import BindableProperty
@@ -89,7 +89,7 @@ class Timer:
         try:
             assert self.callback is not None
             result = self.callback()
-            if is_coroutine_function(self.callback):
+            if isinstance(result, Awaitable):
                 await result
         except Exception as e:
             globals.handle_exception(e)

+ 1 - 0
tests/screen.py

@@ -166,6 +166,7 @@ class Screen:
         self.selenium.get_screenshot_as_file(filename)
 
     def assert_py_logger(self, level: str, message: str) -> None:
+        '''Assert that the Python logger has received a message with the given level and text'''
         try:
             assert self.caplog.records, 'Expected a log message'
             record = self.caplog.records[0]

+ 15 - 0
tests/test_timer.py

@@ -1,3 +1,6 @@
+import asyncio
+import warnings
+
 import pytest
 
 from nicegui import ui
@@ -58,3 +61,15 @@ def test_setting_visibility(screen: Screen, once: bool):
     screen.open('/')
     screen.wait(0.5)
     screen.should_not_contain('Some Label')
+
+
+def test_awaiting_coroutine(screen: Screen, capfd):
+    warnings.simplefilter('error')
+
+    async def update_user():
+        await asyncio.sleep(0.1)
+
+    ui.timer(1, lambda: update_user())
+
+    screen.open('/')
+    screen.wait(1)