Quellcode durchsuchen

mypy and pylint

Falko Schindler vor 1 Jahr
Ursprung
Commit
84a9a41961
4 geänderte Dateien mit 22 neuen und 16 gelöschten Zeilen
  1. 9 9
      nicegui/app.py
  2. 3 2
      nicegui/elements/timer.py
  3. 8 3
      nicegui/functions/refreshable.py
  4. 2 2
      nicegui/nicegui.py

+ 9 - 9
nicegui/app.py

@@ -28,7 +28,7 @@ class App(FastAPI):
         self.native = Native()
         self.storage = Storage()
         self.urls = ObservableSet()
-        self.state: State = State.STOPPED
+        self._state: State = State.STOPPED
 
         self._startup_handlers: List[Union[Callable[..., Any], Awaitable]] = []
         self._shutdown_handlers: List[Union[Callable[..., Any], Awaitable]] = []
@@ -39,38 +39,38 @@ class App(FastAPI):
     @property
     def is_starting(self) -> bool:
         """Return whether NiceGUI is starting."""
-        return self.state == State.STARTING
+        return self._state == State.STARTING
 
     @property
     def is_started(self) -> bool:
         """Return whether NiceGUI is started."""
-        return self.state == State.STARTED
+        return self._state == State.STARTED
 
     @property
     def is_stopping(self) -> bool:
         """Return whether NiceGUI is stopping."""
-        return self.state == State.STOPPING
+        return self._state == State.STOPPING
 
     @property
     def is_stopped(self) -> bool:
         """Return whether NiceGUI is stopped."""
-        return self.state == State.STOPPED
+        return self._state == State.STOPPED
 
     def start(self) -> None:
         """Start NiceGUI. (For internal use only.)"""
-        self.state = State.STARTING
+        self._state = State.STARTING
         with globals.index_client:
             for t in self._startup_handlers:
                 helpers.safe_invoke(t)
-        self.state = State.STARTED
+        self._state = State.STARTED
 
     def stop(self) -> None:
         """Stop NiceGUI. (For internal use only.)"""
-        self.state = State.STOPPING
+        self._state = State.STOPPING
         with globals.index_client:
             for t in self._shutdown_handlers:
                 helpers.safe_invoke(t)
-        self.state = State.STOPPED
+        self._state = State.STOPPED
 
     def on_connect(self, handler: Union[Callable, Awaitable]) -> None:
         """Called every time a new client connects to NiceGUI.

+ 3 - 2
nicegui/elements/timer.py

@@ -1,5 +1,6 @@
 import asyncio
 import time
+from contextlib import nullcontext
 from typing import Any, Callable, Optional
 
 from .. import background_tasks, globals, helpers  # pylint: disable=redefined-builtin
@@ -58,7 +59,7 @@ class Timer(Element, component='timer.js'):
         try:
             if not await self._connected():
                 return
-            with self.parent_slot:
+            with self.parent_slot or nullcontext():
                 await asyncio.sleep(self.interval)
                 if self.active and not self._should_stop():
                     await self._invoke_callback()
@@ -69,7 +70,7 @@ class Timer(Element, component='timer.js'):
         try:
             if not await self._connected():
                 return
-            with self.parent_slot:
+            with self.parent_slot or nullcontext():
                 while not self._should_stop():
                     try:
                         start = time.time()

+ 8 - 3
nicegui/functions/refreshable.py

@@ -1,7 +1,7 @@
 from __future__ import annotations
 
 from dataclasses import dataclass, field
-from typing import Any, Awaitable, Callable, ClassVar, Dict, List, Optional, Tuple, Union
+from typing import Any, Awaitable, Callable, ClassVar, Dict, List, Optional, Tuple, Union, cast
 
 from typing_extensions import Self
 
@@ -119,8 +119,13 @@ class refreshable:
 
 
 def state(value: Any) -> Tuple[Any, Callable[[Any], None]]:
-    target = RefreshableTarget.current_target
-    assert target is not None
+    """Create a state variable that automatically updates its refreshable UI container.
+
+    :param value: The initial value of the state variable.
+
+    :return: A tuple containing the current value and a function to update the value.
+    """
+    target = cast(RefreshableTarget, RefreshableTarget.current_target)
 
     if target.next_index >= len(target.locals):
         target.locals.append(value)

+ 2 - 2
nicegui/nicegui.py

@@ -148,7 +148,7 @@ def handle_handshake(client: Client) -> None:
         client.disconnect_task = None
     for t in client.connect_handlers:
         safe_invoke(t, client)
-    for t in app._connect_handlers:
+    for t in app._connect_handlers:  # pylint: disable=protected-access
         safe_invoke(t, client)
 
 
@@ -170,7 +170,7 @@ async def handle_disconnect(client: Client) -> None:
         _delete_client(client.id)
     for t in client.disconnect_handlers:
         safe_invoke(t, client)
-    for t in app._disconnect_handlers:
+    for t in app._disconnect_handlers:  # pylint: disable=protected-access
         safe_invoke(t, client)