Browse Source

fix type annotation for callables and awaitables

Falko Schindler 3 years ago
parent
commit
e7b3368f3f

+ 2 - 2
nicegui/config.py

@@ -1,5 +1,5 @@
 from dataclasses import dataclass
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import inspect
 import ast
 import os
@@ -15,7 +15,7 @@ class Config():
     dark: Optional[bool] = False
     reload: bool = True
     show: bool = True
-    on_connect: Optional[Union[Callable, Awaitable]] = None
+    on_connect: Optional[Callable] = None
     uvicorn_logging_level: str = 'warning'
     uvicorn_reload_dirs: str = '.'
     uvicorn_reload_includes: str = '*'

+ 2 - 2
nicegui/elements/bool_element.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 from .value_element import ValueElement
 
 class BoolElement(ValueElement):
@@ -8,6 +8,6 @@ class BoolElement(ValueElement):
                  view: jp.HTMLBaseComponent,
                  *,
                  value: bool,
-                 on_change: Optional[Union[Callable, Awaitable]],
+                 on_change: Optional[Callable],
                  ):
         super().__init__(view, value=value, on_change=on_change)

+ 2 - 2
nicegui/elements/button.py

@@ -1,4 +1,4 @@
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import justpy as jp
 
 from ..binding import bind_from, bind_to, BindableProperty
@@ -11,7 +11,7 @@ class Button(Element):
     def __init__(self,
                  text: str = '',
                  *,
-                 on_click: Optional[Union[Callable, Awaitable]] = None,
+                 on_click: Optional[Callable] = None,
                  ):
         """Button Element
 

+ 2 - 2
nicegui/elements/checkbox.py

@@ -1,4 +1,4 @@
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import justpy as jp
 from .bool_element import BoolElement
 
@@ -8,7 +8,7 @@ class Checkbox(BoolElement):
                  text: str = '',
                  *,
                  value: bool = False,
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         """Checkbox Element
 

+ 2 - 2
nicegui/elements/choice_element.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Any, Awaitable, Callable, Dict, List, Optional, Union
+from typing import Any, Callable, Dict, List, Optional, Union
 from .value_element import ValueElement
 
 class ChoiceElement(ValueElement):
@@ -9,7 +9,7 @@ class ChoiceElement(ValueElement):
                  options: Union[List, Dict],
                  *,
                  value: Any,
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         if isinstance(options, List):
             view.options = [{'label': option, 'value': option} for option in options]

+ 2 - 2
nicegui/elements/float_element.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 from .value_element import ValueElement
 
 class FloatElement(ValueElement):
@@ -9,7 +9,7 @@ class FloatElement(ValueElement):
                  *,
                  value: float,
                  format: str = None,
-                 on_change: Optional[Union[Callable, Awaitable]],
+                 on_change: Optional[Callable],
                  ):
         self.format = format
 

+ 2 - 2
nicegui/elements/input.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 from .string_element import StringElement
 
 class Input(StringElement):
@@ -9,7 +9,7 @@ class Input(StringElement):
                  *,
                  placeholder: str = None,
                  value: str = '',
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         """Text Input Element
 

+ 3 - 3
nicegui/elements/keyboard.py

@@ -1,5 +1,5 @@
 import traceback
-from typing import Awaitable, Callable, Dict, Optional, Union
+from typing import Callable, Dict, Optional
 
 from ..events import KeyEventArguments, KeyboardAction, KeyboardKey, KeyboardModifiers, handle_event
 from .custom_view import CustomView
@@ -20,7 +20,7 @@ class Keyboard(Element):
 
     def __init__(self,
                  *,
-                 on_key: Optional[Union[Callable, Awaitable]] = None,
+                 on_key: Optional[Callable] = None,
                  active: bool = True,
                  repeating: bool = True,
                  ):
@@ -29,7 +29,7 @@ class Keyboard(Element):
 
         Adds global keyboard event tracking.
 
-        :param handle_keys: callback to be executed when keyboard events occur.
+        :param on_key: callback to be executed when keyboard events occur.
         :param active: boolean flag indicating whether the callback should be executed or not (default: `True`)
         :param repeating: boolean flag indicating whether held keys should be sent repeatedly (default: `True`)
         """

+ 2 - 2
nicegui/elements/menu_item.py

@@ -1,4 +1,4 @@
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import justpy as jp
 
 from ..events import ClickEventArguments, handle_event
@@ -9,7 +9,7 @@ class MenuItem(Element):
 
     def __init__(self,
                  text: str = '',
-                 on_click: Optional[Union[Callable, Awaitable]] = None,
+                 on_click: Optional[Callable] = None,
                  *,
                  auto_close: bool = True,
                  ):

+ 2 - 2
nicegui/elements/number.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 from .float_element import FloatElement
 
 class Number(FloatElement):
@@ -10,7 +10,7 @@ class Number(FloatElement):
                  placeholder: str = None,
                  value: float = None,
                  format: str = None,
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         """Number Input Element
 

+ 2 - 2
nicegui/elements/page.py

@@ -1,6 +1,6 @@
 import inspect
 import justpy as jp
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 from pygments.formatters import HtmlFormatter
 from starlette.requests import Request
 from ..globals import config, page_stack, view_stack
@@ -14,7 +14,7 @@ class Page(jp.QuasarPage):
                  dark: Optional[bool] = ...,
                  classes: str = 'q-ma-md column items-start',
                  css: str = HtmlFormatter().get_style_defs('.codehilite'),
-                 on_connect: Optional[Union[Awaitable, Callable]] = None,
+                 on_connect: Optional[Callable] = None,
                  ):
         """Page
 

+ 2 - 2
nicegui/elements/radio.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Awaitable, Callable, Dict, List, Optional, Union
+from typing import Callable, Dict, List, Optional, Union
 from .choice_element import ChoiceElement
 
 class Radio(ChoiceElement):
@@ -8,7 +8,7 @@ class Radio(ChoiceElement):
                  options: Union[List, Dict],
                  *,
                  value: any = None,
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         """Radio Selection Element
 

+ 3 - 3
nicegui/elements/scene.py

@@ -1,6 +1,6 @@
 from dataclasses import dataclass
 from justpy import WebPage
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import traceback
 import websockets
 from .element import Element
@@ -33,7 +33,7 @@ class SceneCamera:
 
 class SceneView(CustomView):
 
-    def __init__(self, *, width: int, height: int, on_click: Union[Callable, Awaitable]):
+    def __init__(self, *, width: int, height: int, on_click: Optional[Callable]):
         super().__init__('scene', width=width, height=height)
         self.on_click = on_click
         self.allowed_events = ['onConnect', 'onClick']
@@ -76,7 +76,7 @@ class Scene(Element):
     from .scene_objects import Curve as curve
     from .scene_objects import Texture as texture
 
-    def __init__(self, width: int = 400, height: int = 300, on_click: Optional[Union[Callable, Awaitable]] = None):
+    def __init__(self, width: int = 400, height: int = 300, on_click: Optional[Callable] = None):
         """3D Scene
 
         Display a 3d scene using `three.js <https://threejs.org/>`_.

+ 2 - 2
nicegui/elements/select.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Awaitable, Callable, Dict, List, Optional, Union
+from typing import Callable, Dict, List, Optional, Union
 from .choice_element import ChoiceElement
 
 class Select(ChoiceElement):
@@ -8,7 +8,7 @@ class Select(ChoiceElement):
                  options: Union[List, Dict],
                  *,
                  value: any = None,
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         """Dropdown Selection Element
 

+ 2 - 2
nicegui/elements/slider.py

@@ -1,4 +1,4 @@
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import justpy as jp
 from .float_element import FloatElement
 
@@ -10,7 +10,7 @@ class Slider(FloatElement):
                  max: float,
                  step: float = 1,
                  value: float = None,
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         """Slider Element
 

+ 2 - 2
nicegui/elements/string_element.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 from .value_element import ValueElement
 
 class StringElement(ValueElement):
@@ -8,6 +8,6 @@ class StringElement(ValueElement):
                  view: jp.HTMLBaseComponent,
                  *,
                  value: float,
-                 on_change: Optional[Union[Callable, Awaitable]],
+                 on_change: Optional[Callable],
                  ):
         super().__init__(view, value=value, on_change=on_change)

+ 2 - 2
nicegui/elements/switch.py

@@ -1,4 +1,4 @@
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import justpy as jp
 from .bool_element import BoolElement
 
@@ -8,7 +8,7 @@ class Switch(BoolElement):
                  text: str = '',
                  *,
                  value: bool = False,
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         """Switch Element
 

+ 2 - 2
nicegui/elements/toggle.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Awaitable, Callable, Dict, List, Optional, Union
+from typing import Callable, Dict, List, Optional, Union
 from .choice_element import ChoiceElement
 
 class Toggle(ChoiceElement):
@@ -8,7 +8,7 @@ class Toggle(ChoiceElement):
                  options: Union[List, Dict],
                  *,
                  value: any = None,
-                 on_change: Optional[Union[Callable, Awaitable]] = None,
+                 on_change: Optional[Callable] = None,
                  ):
         """Toggle Element
 

+ 2 - 2
nicegui/elements/upload.py

@@ -1,6 +1,6 @@
 import traceback
 import justpy as jp
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import base64
 
 from ..events import UploadEventArguments, handle_event
@@ -11,7 +11,7 @@ class Upload(Element):
     def __init__(self,
                  *,
                  multiple: bool = False,
-                 on_upload: Optional[Union[Callable, Awaitable]] = None,
+                 on_upload: Optional[Callable] = None,
                  ):
         """File Upload Element
 

+ 2 - 2
nicegui/elements/value_element.py

@@ -1,5 +1,5 @@
 import justpy as jp
-from typing import Any, Awaitable, Callable, Optional, Union
+from typing import Any, Callable, Optional
 
 from ..events import ValueChangeEventArguments, handle_event
 from ..binding import bind_from, bind_to, BindableProperty
@@ -15,7 +15,7 @@ class ValueElement(Element):
                  view: jp.HTMLBaseComponent,
                  *,
                  value: Any,
-                 on_change: Optional[Union[Callable, Awaitable]],
+                 on_change: Optional[Callable],
                  ):
         super().__init__(view)
 

+ 2 - 5
nicegui/events.py

@@ -3,7 +3,7 @@ from inspect import signature
 from justpy.htmlcomponents import HTMLBaseComponent
 from pydantic import BaseModel
 import traceback
-from typing import Any, Awaitable, Callable, List, Optional, Union
+from typing import Any, Callable, List, Optional
 from starlette.websockets import WebSocket
 
 from .elements.element import Element
@@ -205,10 +205,7 @@ class KeyEventArguments(EventArguments):
     modifiers: KeyboardModifiers
 
 
-def handle_event(handler: Optional[Union[Callable, Awaitable]],
-                 arguments: EventArguments,
-                 *,
-                 update: Optional[HTMLBaseComponent] = None):
+def handle_event(handler: Optional[Callable], arguments: EventArguments, *, update: Optional[HTMLBaseComponent] = None):
     try:
         if handler is None:
             return

+ 2 - 2
nicegui/run.py

@@ -1,4 +1,4 @@
-from typing import Awaitable, Callable, Optional, Union
+from typing import Callable, Optional
 import inspect
 import sys
 import webbrowser
@@ -31,7 +31,7 @@ def run(self, *,
         dark: Optional[bool] = False,
         reload: bool = True,
         show: bool = True,
-        on_connect: Optional[Union[Callable, Awaitable]] = None,
+        on_connect: Optional[Callable] = None,
         uvicorn_logging_level: str = 'warning',
         uvicorn_reload_dirs: str = '.',
         uvicorn_reload_includes: str = '*',

+ 2 - 2
nicegui/timer.py

@@ -1,7 +1,7 @@
 import asyncio
 import time
 import traceback
-from typing import Awaitable, Callable, List, Union
+from typing import Callable, List
 from collections import namedtuple
 
 from .binding import BindableProperty
@@ -15,7 +15,7 @@ class Timer:
 
     active = BindableProperty()
 
-    def __init__(self, interval: float, callback: Union[Callable, Awaitable], *, active: bool = True, once: bool = False):
+    def __init__(self, interval: float, callback: Callable, *, active: bool = True, once: bool = False):
         """Timer
 
         One major drive behind the creation of NiceGUI was the necessity to have a simple approach to update the interface in regular intervals, for example to show a graph with incomming measurements.