diff --git a/reflex/components/base/error_boundary.pyi b/reflex/components/base/error_boundary.pyi index 65109b0fe0..aaf5584e45 100644 --- a/reflex/components/base/error_boundary.pyi +++ b/reflex/components/base/error_boundary.pyi @@ -31,7 +31,7 @@ class ErrorBoundary(Component): on_click: Optional[EventType[[]]] = None, on_context_menu: Optional[EventType[[]]] = None, on_double_click: Optional[EventType[[]]] = None, - on_error: Optional[EventType[[]]] = None, + on_error: Optional[EventType] = None, on_focus: Optional[EventType[[]]] = None, on_mount: Optional[EventType[[]]] = None, on_mouse_down: Optional[EventType[[]]] = None, diff --git a/reflex/components/component.py b/reflex/components/component.py index 364353b9db..a0d9c93b09 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -45,6 +45,7 @@ EventVar, call_event_fn, call_event_handler, + empty_event, get_handler_args, ) from reflex.style import Style, format_as_emotion @@ -623,21 +624,21 @@ def get_event_triggers(self) -> Dict[str, Any]: """ default_triggers = { - EventTriggers.ON_FOCUS: lambda: [], - EventTriggers.ON_BLUR: lambda: [], - EventTriggers.ON_CLICK: lambda: [], - EventTriggers.ON_CONTEXT_MENU: lambda: [], - EventTriggers.ON_DOUBLE_CLICK: lambda: [], - EventTriggers.ON_MOUSE_DOWN: lambda: [], - EventTriggers.ON_MOUSE_ENTER: lambda: [], - EventTriggers.ON_MOUSE_LEAVE: lambda: [], - EventTriggers.ON_MOUSE_MOVE: lambda: [], - EventTriggers.ON_MOUSE_OUT: lambda: [], - EventTriggers.ON_MOUSE_OVER: lambda: [], - EventTriggers.ON_MOUSE_UP: lambda: [], - EventTriggers.ON_SCROLL: lambda: [], - EventTriggers.ON_MOUNT: lambda: [], - EventTriggers.ON_UNMOUNT: lambda: [], + EventTriggers.ON_FOCUS: empty_event, + EventTriggers.ON_BLUR: empty_event, + EventTriggers.ON_CLICK: empty_event, + EventTriggers.ON_CONTEXT_MENU: empty_event, + EventTriggers.ON_DOUBLE_CLICK: empty_event, + EventTriggers.ON_MOUSE_DOWN: empty_event, + EventTriggers.ON_MOUSE_ENTER: empty_event, + EventTriggers.ON_MOUSE_LEAVE: empty_event, + EventTriggers.ON_MOUSE_MOVE: empty_event, + EventTriggers.ON_MOUSE_OUT: empty_event, + EventTriggers.ON_MOUSE_OVER: empty_event, + EventTriggers.ON_MOUSE_UP: empty_event, + EventTriggers.ON_SCROLL: empty_event, + EventTriggers.ON_MOUNT: empty_event, + EventTriggers.ON_UNMOUNT: empty_event, } # Look for component specific triggers, @@ -648,7 +649,7 @@ def get_event_triggers(self) -> Dict[str, Any]: annotation = field.annotation if (metadata := getattr(annotation, "__metadata__", None)) is not None: args_spec = metadata[0] - default_triggers[field.name] = args_spec or (lambda: []) + default_triggers[field.name] = args_spec or (empty_event) # type: ignore return default_triggers def __repr__(self) -> str: @@ -1705,7 +1706,7 @@ def __init__(self, *args, **kwargs): value = self._create_event_chain( value=value, args_spec=event_triggers_in_component_declaration.get( - key, lambda: [] + key, empty_event ), ) self.props[format.to_camel_case(key)] = value diff --git a/reflex/components/datadisplay/dataeditor.pyi b/reflex/components/datadisplay/dataeditor.pyi index b3a9ce2b39..1b8fed2879 100644 --- a/reflex/components/datadisplay/dataeditor.pyi +++ b/reflex/components/datadisplay/dataeditor.pyi @@ -139,20 +139,20 @@ class DataEditor(NoSSRComponent): on_cell_activated: Optional[EventType] = None, on_cell_clicked: Optional[EventType] = None, on_cell_context_menu: Optional[EventType] = None, - on_cell_edited: Optional[EventType[[]]] = None, + on_cell_edited: Optional[EventType] = None, on_click: Optional[EventType[[]]] = None, - on_column_resize: Optional[EventType[[]]] = None, + on_column_resize: Optional[EventType] = None, on_context_menu: Optional[EventType[[]]] = None, - on_delete: Optional[EventType[[]]] = None, + on_delete: Optional[EventType] = None, on_double_click: Optional[EventType[[]]] = None, - on_finished_editing: Optional[EventType[[]]] = None, + on_finished_editing: Optional[EventType] = None, on_focus: Optional[EventType[[]]] = None, - on_group_header_clicked: Optional[EventType[[]]] = None, - on_group_header_context_menu: Optional[EventType[[]]] = None, - on_group_header_renamed: Optional[EventType[[]]] = None, + on_group_header_clicked: Optional[EventType] = None, + on_group_header_context_menu: Optional[EventType] = None, + on_group_header_renamed: Optional[EventType] = None, on_header_clicked: Optional[EventType] = None, on_header_context_menu: Optional[EventType] = None, - on_header_menu_click: Optional[EventType[[]]] = None, + on_header_menu_click: Optional[EventType] = None, on_item_hovered: Optional[EventType] = None, on_mount: Optional[EventType[[]]] = None, on_mouse_down: Optional[EventType[[]]] = None, diff --git a/reflex/components/react_player/audio.pyi b/reflex/components/react_player/audio.pyi index 4b566d8982..d1f29f508b 100644 --- a/reflex/components/react_player/audio.pyi +++ b/reflex/components/react_player/audio.pyi @@ -58,7 +58,7 @@ class Audio(ReactPlayer): on_play: Optional[EventType[[]]] = None, on_playback_quality_change: Optional[EventType[[]]] = None, on_playback_rate_change: Optional[EventType[[]]] = None, - on_progress: Optional[EventType[[]]] = None, + on_progress: Optional[EventType] = None, on_ready: Optional[EventType[[]]] = None, on_scroll: Optional[EventType[[]]] = None, on_seek: Optional[EventType] = None, diff --git a/reflex/components/react_player/react_player.pyi b/reflex/components/react_player/react_player.pyi index 1977eaa007..940b09e51e 100644 --- a/reflex/components/react_player/react_player.pyi +++ b/reflex/components/react_player/react_player.pyi @@ -56,7 +56,7 @@ class ReactPlayer(NoSSRComponent): on_play: Optional[EventType[[]]] = None, on_playback_quality_change: Optional[EventType[[]]] = None, on_playback_rate_change: Optional[EventType[[]]] = None, - on_progress: Optional[EventType[[]]] = None, + on_progress: Optional[EventType] = None, on_ready: Optional[EventType[[]]] = None, on_scroll: Optional[EventType[[]]] = None, on_seek: Optional[EventType] = None, diff --git a/reflex/components/react_player/video.pyi b/reflex/components/react_player/video.pyi index 6e4047d06c..a50ccf71f6 100644 --- a/reflex/components/react_player/video.pyi +++ b/reflex/components/react_player/video.pyi @@ -58,7 +58,7 @@ class Video(ReactPlayer): on_play: Optional[EventType[[]]] = None, on_playback_quality_change: Optional[EventType[[]]] = None, on_playback_rate_change: Optional[EventType[[]]] = None, - on_progress: Optional[EventType[[]]] = None, + on_progress: Optional[EventType] = None, on_ready: Optional[EventType[[]]] = None, on_scroll: Optional[EventType[[]]] = None, on_seek: Optional[EventType] = None, diff --git a/reflex/components/recharts/cartesian.py b/reflex/components/recharts/cartesian.py index 153d3fb2a6..865b50a325 100644 --- a/reflex/components/recharts/cartesian.py +++ b/reflex/components/recharts/cartesian.py @@ -252,7 +252,7 @@ def get_event_triggers(self) -> dict[str, Union[Var, Any]]: A dict mapping the event trigger to the var that is passed to the handler. """ return { - EventTriggers.ON_CHANGE: lambda: [], + EventTriggers.ON_CHANGE: empty_event, } @@ -293,10 +293,10 @@ class Cartesian(Recharts): name: Var[Union[str, int]] # The customized event handler of animation start - on_animation_start: EventHandler[lambda: []] + on_animation_start: EventHandler[empty_event] # The customized event handler of animation end - on_animation_end: EventHandler[lambda: []] + on_animation_end: EventHandler[empty_event] # The customized event handler of click on the component in this group on_click: EventHandler[empty_event] diff --git a/reflex/components/recharts/charts.py b/reflex/components/recharts/charts.py index d4785f6c4d..d7e07b2d83 100644 --- a/reflex/components/recharts/charts.py +++ b/reflex/components/recharts/charts.py @@ -330,9 +330,9 @@ def get_event_triggers(self) -> dict[str, Union[Var, Any]]: A dict mapping the event trigger to the var that is passed to the handler. """ return { - EventTriggers.ON_CLICK: lambda: [], - EventTriggers.ON_MOUSE_ENTER: lambda: [], - EventTriggers.ON_MOUSE_LEAVE: lambda: [], + EventTriggers.ON_CLICK: empty_event, + EventTriggers.ON_MOUSE_ENTER: empty_event, + EventTriggers.ON_MOUSE_LEAVE: empty_event, } @@ -419,14 +419,14 @@ def get_event_triggers(self) -> dict[str, Union[Var, Any]]: A dict mapping the event trigger to the var that is passed to the handler. """ return { - EventTriggers.ON_CLICK: lambda: [], - EventTriggers.ON_MOUSE_DOWN: lambda: [], - EventTriggers.ON_MOUSE_UP: lambda: [], - EventTriggers.ON_MOUSE_MOVE: lambda: [], - EventTriggers.ON_MOUSE_OVER: lambda: [], - EventTriggers.ON_MOUSE_OUT: lambda: [], - EventTriggers.ON_MOUSE_ENTER: lambda: [], - EventTriggers.ON_MOUSE_LEAVE: lambda: [], + EventTriggers.ON_CLICK: empty_event, + EventTriggers.ON_MOUSE_DOWN: empty_event, + EventTriggers.ON_MOUSE_UP: empty_event, + EventTriggers.ON_MOUSE_MOVE: empty_event, + EventTriggers.ON_MOUSE_OVER: empty_event, + EventTriggers.ON_MOUSE_OUT: empty_event, + EventTriggers.ON_MOUSE_ENTER: empty_event, + EventTriggers.ON_MOUSE_LEAVE: empty_event, } diff --git a/reflex/components/recharts/polar.py b/reflex/components/recharts/polar.py index bcbd5abd30..ccb96f1806 100644 --- a/reflex/components/recharts/polar.py +++ b/reflex/components/recharts/polar.py @@ -103,14 +103,14 @@ def get_event_triggers(self) -> dict[str, Union[Var, Any]]: A dict mapping the event trigger to the var that is passed to the handler. """ return { - EventTriggers.ON_ANIMATION_START: lambda: [], - EventTriggers.ON_ANIMATION_END: lambda: [], - EventTriggers.ON_CLICK: lambda: [], - EventTriggers.ON_MOUSE_MOVE: lambda: [], - EventTriggers.ON_MOUSE_OVER: lambda: [], - EventTriggers.ON_MOUSE_OUT: lambda: [], - EventTriggers.ON_MOUSE_ENTER: lambda: [], - EventTriggers.ON_MOUSE_LEAVE: lambda: [], + EventTriggers.ON_ANIMATION_START: empty_event, + EventTriggers.ON_ANIMATION_END: empty_event, + EventTriggers.ON_CLICK: empty_event, + EventTriggers.ON_MOUSE_MOVE: empty_event, + EventTriggers.ON_MOUSE_OVER: empty_event, + EventTriggers.ON_MOUSE_OUT: empty_event, + EventTriggers.ON_MOUSE_ENTER: empty_event, + EventTriggers.ON_MOUSE_LEAVE: empty_event, } @@ -167,8 +167,8 @@ def get_event_triggers(self) -> dict[str, Union[Var, Any]]: A dict mapping the event trigger to the var that is passed to the handler. """ return { - EventTriggers.ON_ANIMATION_START: lambda: [], - EventTriggers.ON_ANIMATION_END: lambda: [], + EventTriggers.ON_ANIMATION_START: empty_event, + EventTriggers.ON_ANIMATION_END: empty_event, } @@ -219,14 +219,14 @@ def get_event_triggers(self) -> dict[str, Union[Var, Any]]: A dict mapping the event trigger to the var that is passed to the handler. """ return { - EventTriggers.ON_CLICK: lambda: [], - EventTriggers.ON_MOUSE_MOVE: lambda: [], - EventTriggers.ON_MOUSE_OVER: lambda: [], - EventTriggers.ON_MOUSE_OUT: lambda: [], - EventTriggers.ON_MOUSE_ENTER: lambda: [], - EventTriggers.ON_MOUSE_LEAVE: lambda: [], - EventTriggers.ON_ANIMATION_START: lambda: [], - EventTriggers.ON_ANIMATION_END: lambda: [], + EventTriggers.ON_CLICK: empty_event, + EventTriggers.ON_MOUSE_MOVE: empty_event, + EventTriggers.ON_MOUSE_OVER: empty_event, + EventTriggers.ON_MOUSE_OUT: empty_event, + EventTriggers.ON_MOUSE_ENTER: empty_event, + EventTriggers.ON_MOUSE_LEAVE: empty_event, + EventTriggers.ON_ANIMATION_START: empty_event, + EventTriggers.ON_ANIMATION_END: empty_event, } @@ -392,12 +392,12 @@ def get_event_triggers(self) -> dict[str, Union[Var, Any]]: A dict mapping the event trigger to the var that is passed to the handler. """ return { - EventTriggers.ON_CLICK: lambda: [], - EventTriggers.ON_MOUSE_MOVE: lambda: [], - EventTriggers.ON_MOUSE_OVER: lambda: [], - EventTriggers.ON_MOUSE_OUT: lambda: [], - EventTriggers.ON_MOUSE_ENTER: lambda: [], - EventTriggers.ON_MOUSE_LEAVE: lambda: [], + EventTriggers.ON_CLICK: empty_event, + EventTriggers.ON_MOUSE_MOVE: empty_event, + EventTriggers.ON_MOUSE_OVER: empty_event, + EventTriggers.ON_MOUSE_OUT: empty_event, + EventTriggers.ON_MOUSE_ENTER: empty_event, + EventTriggers.ON_MOUSE_LEAVE: empty_event, } diff --git a/reflex/components/suneditor/editor.pyi b/reflex/components/suneditor/editor.pyi index bcc0b64ac5..f7149a02cb 100644 --- a/reflex/components/suneditor/editor.pyi +++ b/reflex/components/suneditor/editor.pyi @@ -122,16 +122,16 @@ class Editor(NoSSRComponent): class_name: Optional[Any] = None, autofocus: Optional[bool] = None, custom_attrs: Optional[Dict[str, Union[Var, str]]] = None, - on_blur: Optional[EventType[[]]] = None, - on_change: Optional[EventType[[]]] = None, + on_blur: Optional[EventType] = None, + on_change: Optional[EventType] = None, on_click: Optional[EventType[[]]] = None, on_context_menu: Optional[EventType[[]]] = None, - on_copy: Optional[EventType[[]]] = None, - on_cut: Optional[EventType[[]]] = None, + on_copy: Optional[EventType] = None, + on_cut: Optional[EventType] = None, on_double_click: Optional[EventType[[]]] = None, on_focus: Optional[EventType[[]]] = None, - on_input: Optional[EventType[[]]] = None, - on_load: Optional[EventType[[]]] = None, + on_input: Optional[EventType] = None, + on_load: Optional[EventType] = None, on_mount: Optional[EventType[[]]] = None, on_mouse_down: Optional[EventType[[]]] = None, on_mouse_enter: Optional[EventType[[]]] = None, @@ -140,12 +140,12 @@ class Editor(NoSSRComponent): on_mouse_out: Optional[EventType[[]]] = None, on_mouse_over: Optional[EventType[[]]] = None, on_mouse_up: Optional[EventType[[]]] = None, - on_paste: Optional[EventType[[]]] = None, - on_resize_editor: Optional[EventType[[]]] = None, + on_paste: Optional[EventType] = None, + on_resize_editor: Optional[EventType] = None, on_scroll: Optional[EventType[[]]] = None, on_unmount: Optional[EventType[[]]] = None, - toggle_code_view: Optional[EventType[[]]] = None, - toggle_full_screen: Optional[EventType[[]]] = None, + toggle_code_view: Optional[EventType] = None, + toggle_full_screen: Optional[EventType] = None, **props, ) -> "Editor": """Create an instance of Editor. No children allowed. diff --git a/reflex/event.py b/reflex/event.py index 58053408dd..f93bc63d2a 100644 --- a/reflex/event.py +++ b/reflex/event.py @@ -399,11 +399,6 @@ class EventChain(EventActionsMixin): invocation: Optional[Var] = dataclasses.field(default=None) -# These chains can be used for their side effects when no other events are desired. -stop_propagation = EventChain(events=[], args_spec=lambda: []).stop_propagation -prevent_default = EventChain(events=[], args_spec=lambda: []).prevent_default - - @dataclasses.dataclass( init=True, frozen=True, @@ -467,6 +462,11 @@ def empty_event() -> Tuple[()]: return tuple() # type: ignore +# These chains can be used for their side effects when no other events are desired. +stop_propagation = EventChain(events=[], args_spec=empty_event).stop_propagation +prevent_default = EventChain(events=[], args_spec=empty_event).prevent_default + + T = TypeVar("T") diff --git a/reflex/utils/pyi_generator.py b/reflex/utils/pyi_generator.py index 1df8e6fa0b..fd76576b94 100644 --- a/reflex/utils/pyi_generator.py +++ b/reflex/utils/pyi_generator.py @@ -429,7 +429,7 @@ def _generate_component_create_functiondef( def figure_out_return_type(annotation: Any): if inspect.isclass(annotation) and issubclass(annotation, inspect._empty): - return ast.Name(id="Optional[EventType[[]]]") + return ast.Name(id="Optional[EventType]") if isinstance(annotation, str) and annotation.startswith("Tuple["): inside_of_tuple = annotation.removeprefix("Tuple[").removesuffix("]")