Skip to content

Commit

Permalink
fix pyi for untyped event handlers (#4186)
Browse files Browse the repository at this point in the history
* fix pyi for untyped event handlers

* no more empty lambdas
  • Loading branch information
adhami3310 authored and masenf committed Oct 16, 2024
1 parent f8881c3 commit 20398c1
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 83 deletions.
2 changes: 1 addition & 1 deletion reflex/components/base/error_boundary.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 18 additions & 17 deletions reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
EventVar,
call_event_fn,
call_event_handler,
empty_event,
get_handler_args,
)
from reflex.style import Style, format_as_emotion
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions reflex/components/datadisplay/dataeditor.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/react_player/audio.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/react_player/react_player.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/react_player/video.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions reflex/components/recharts/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand Down Expand Up @@ -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]
Expand Down
22 changes: 11 additions & 11 deletions reflex/components/recharts/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand Down Expand Up @@ -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,
}


Expand Down
48 changes: 24 additions & 24 deletions reflex/components/recharts/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand Down Expand Up @@ -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,
}


Expand Down Expand Up @@ -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,
}


Expand Down Expand Up @@ -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,
}


Expand Down
20 changes: 10 additions & 10 deletions reflex/components/suneditor/editor.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions reflex/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")


Expand Down
2 changes: 1 addition & 1 deletion reflex/utils/pyi_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("]")

Expand Down

0 comments on commit 20398c1

Please sign in to comment.