瀏覽代碼

#1518 change type of GenericEventArguments.args to Any

Falko Schindler 1 年之前
父節點
當前提交
3b3aed247f

+ 2 - 2
nicegui/elements/color_picker.py

@@ -1,4 +1,4 @@
-from typing import Any, Callable, cast
+from typing import Any, Callable
 
 from ..element import Element
 from ..events import ColorPickEventArguments, GenericEventArguments, handle_event
@@ -16,7 +16,7 @@ class ColorPicker(Menu):
         super().__init__(value=value)
         with self:
             def handle_change(e: GenericEventArguments):
-                handle_event(on_pick, ColorPickEventArguments(sender=self, client=self.client, color=cast(str, e.args)))
+                handle_event(on_pick, ColorPickEventArguments(sender=self, client=self.client, color=e.args))
             self.q_color = Element('q-color').on('change', handle_change)
 
     def set_color(self, color: str) -> None:

+ 12 - 11
nicegui/elements/interactive_image.py

@@ -1,7 +1,7 @@
 from __future__ import annotations
 
 from pathlib import Path
-from typing import Any, Callable, List, Optional, Union
+from typing import Any, Callable, List, Optional, Union, cast
 
 from ..events import GenericEventArguments, MouseEventArguments, handle_event
 from .mixins.content_element import ContentElement
@@ -27,7 +27,7 @@ class InteractiveImage(SourceElement, ContentElement, component='interactive_ima
         See `OpenCV Webcam <https://github.com/zauberzeug/nicegui/tree/main/examples/opencv_webcam/main.py>`_ for an example.
 
         :param source: the source of the image; can be an URL, local file path or a base64 string
-        :param content: SVG content which should be overlayed; viewport has the same dimensions as the image
+        :param content: SVG content which should be overlaid; viewport has the same dimensions as the image
         :param on_mouse: callback for mouse events (yields `type`, `image_x` and `image_y`)
         :param events: list of JavaScript events to subscribe to (default: `['click']`)
         :param cross: whether to show crosshairs (default: `False`)
@@ -39,18 +39,19 @@ class InteractiveImage(SourceElement, ContentElement, component='interactive_ima
         def handle_mouse(e: GenericEventArguments) -> None:
             if on_mouse is None:
                 return
+            args = cast(dict, e.args)
             arguments = MouseEventArguments(
                 sender=self,
                 client=self.client,
-                type=e.args.get('mouse_event_type', ''),
-                image_x=e.args.get('image_x', 0.0),
-                image_y=e.args.get('image_y', 0.0),
-                button=e.args.get('button', 0),
-                buttons=e.args.get('buttons', 0),
-                alt=e.args.get('alt', False),
-                ctrl=e.args.get('ctrl', False),
-                meta=e.args.get('meta', False),
-                shift=e.args.get('shift', False),
+                type=args.get('mouse_event_type', ''),
+                image_x=args.get('image_x', 0.0),
+                image_y=args.get('image_y', 0.0),
+                button=args.get('button', 0),
+                buttons=args.get('buttons', 0),
+                alt=args.get('alt', False),
+                ctrl=args.get('ctrl', False),
+                meta=args.get('meta', False),
+                shift=args.get('shift', False),
             )
             handle_event(on_mouse, arguments)
         self.on('mouse', handle_mouse)

+ 0 - 1
nicegui/elements/number.py

@@ -93,7 +93,6 @@ class Number(ValidationElement, DisableableElement):
     def _event_args_to_value(self, e: GenericEventArguments) -> Any:
         if not e.args:
             return None
-        assert isinstance(e.args, str)
         return float(e.args)
 
     def _value_to_model_value(self, value: Any) -> Any:

+ 0 - 1
nicegui/elements/radio.py

@@ -24,7 +24,6 @@ class Radio(ChoiceElement, DisableableElement):
         super().__init__(tag='q-option-group', options=options, value=value, on_change=on_change)
 
     def _event_args_to_value(self, e: GenericEventArguments) -> Any:
-        assert isinstance(e.args, int)
         return self._values[e.args]
 
     def _value_to_model_value(self, value: Any) -> Any:

+ 0 - 2
nicegui/elements/select.py

@@ -49,7 +49,6 @@ class Select(ChoiceElement, DisableableElement, component='select.js'):
         self._props['clearable'] = clearable
 
     def on_filter(self, e: GenericEventArguments) -> None:
-        assert isinstance(e.args, str)
         self.options = [
             option
             for option in self.original_options
@@ -62,7 +61,6 @@ class Select(ChoiceElement, DisableableElement, component='select.js'):
         if self.multiple:
             if e.args is None:
                 return []
-            assert isinstance(e.args, list)
             return [self._values[arg['value']] for arg in e.args]
         else:
             if e.args is None:

+ 0 - 1
nicegui/elements/toggle.py

@@ -24,7 +24,6 @@ class Toggle(ChoiceElement, DisableableElement):
         super().__init__(tag='q-btn-toggle', options=options, value=value, on_change=on_change)
 
     def _event_args_to_value(self, e: GenericEventArguments) -> Any:
-        assert isinstance(e.args, int)
         return self._values[e.args]
 
     def _value_to_model_value(self, value: Any) -> Any:

+ 1 - 1
nicegui/events.py

@@ -33,7 +33,7 @@ class UiEventArguments(EventArguments):
 
 @dataclass(**KWONLY_SLOTS)
 class GenericEventArguments(UiEventArguments):
-    args: Dict[str, Any]
+    args: Any
 
     def __getitem__(self, key: str) -> Any:
         if key == 'args':