From 3b3aed247f88374218ea336d90a94a18c1941455 Mon Sep 17 00:00:00 2001 From: Falko Schindler Date: Tue, 29 Aug 2023 16:36:12 +0200 Subject: [PATCH] #1518 change type of GenericEventArguments.args to Any --- nicegui/elements/color_picker.py | 4 ++-- nicegui/elements/interactive_image.py | 23 ++++++++++++----------- nicegui/elements/number.py | 1 - nicegui/elements/radio.py | 1 - nicegui/elements/select.py | 2 -- nicegui/elements/toggle.py | 1 - nicegui/events.py | 2 +- 7 files changed, 15 insertions(+), 19 deletions(-) diff --git a/nicegui/elements/color_picker.py b/nicegui/elements/color_picker.py index 65e803c12..14dba53b3 100644 --- a/nicegui/elements/color_picker.py +++ b/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 @@ def __init__(self, *, on_pick: Callable[..., Any], value: bool = False) -> None: 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: diff --git a/nicegui/elements/interactive_image.py b/nicegui/elements/interactive_image.py index 4e8d5e41c..a1a397117 100644 --- a/nicegui/elements/interactive_image.py +++ b/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 @@ def __init__(self, See `OpenCV Webcam `_ 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 @@ def __init__(self, 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) diff --git a/nicegui/elements/number.py b/nicegui/elements/number.py index 1116edd45..aa7add3a1 100644 --- a/nicegui/elements/number.py +++ b/nicegui/elements/number.py @@ -93,7 +93,6 @@ def sanitize(self) -> None: 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: diff --git a/nicegui/elements/radio.py b/nicegui/elements/radio.py index 18ff40f63..e40ee5039 100644 --- a/nicegui/elements/radio.py +++ b/nicegui/elements/radio.py @@ -24,7 +24,6 @@ def __init__(self, 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: diff --git a/nicegui/elements/select.py b/nicegui/elements/select.py index 8b2c44184..1b381308a 100644 --- a/nicegui/elements/select.py +++ b/nicegui/elements/select.py @@ -49,7 +49,6 @@ def __init__(self, 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 @@ def _event_args_to_value(self, e: GenericEventArguments) -> Any: 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: diff --git a/nicegui/elements/toggle.py b/nicegui/elements/toggle.py index 98d696928..57a287586 100644 --- a/nicegui/elements/toggle.py +++ b/nicegui/elements/toggle.py @@ -24,7 +24,6 @@ def __init__(self, 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: diff --git a/nicegui/events.py b/nicegui/events.py index 855e1b8b8..b4bdb9bac 100644 --- a/nicegui/events.py +++ b/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':