Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Typing of Event Handlers #3523

Merged
merged 3 commits into from
Jun 24, 2024
Merged

Enhance Typing of Event Handlers #3523

merged 3 commits into from
Jun 24, 2024

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Jun 23, 2024

Summary by Sourcery

This pull request enhances the typing of event handlers across multiple modules by introducing the OptionalEventCallable type. It simplifies event handler initialization and improves type safety and clarity in the codebase.

  • Enhancements:
    • Enhanced typing for event handlers by introducing the OptionalEventCallable type.
    • Simplified event handler initialization by removing redundant JSON parsing and directly passing ControlEvent objects.
    • Updated various event handler properties to use OptionalEventCallable for better type safety and clarity.

Copy link
Contributor

sourcery-ai bot commented Jun 23, 2024

Reviewer's Guide by Sourcery

This pull request enhances the typing of event handlers across multiple files by introducing the OptionalEventCallable type. It updates the type annotations for event handler parameters to use this new type and simplifies event handler lambda functions by removing json.loads and directly passing the event.

File-Level Changes

Files Changes
sdk/python/packages/flet-core/src/flet_core/gesture_detector.py
sdk/python/packages/flet-core/src/flet_core/semantics.py
sdk/python/packages/flet-core/src/flet_core/datatable.py
sdk/python/packages/flet-core/src/flet_core/file_picker.py
sdk/python/packages/flet-core/src/flet_core/dismissible.py
sdk/python/packages/flet-core/src/flet_core/charts/line_chart.py
sdk/python/packages/flet-core/src/flet_core/container.py
sdk/python/packages/flet-core/src/flet_core/scrollable_control.py
sdk/python/packages/flet-core/src/flet_core/cupertino_slider.py
sdk/python/packages/flet-core/src/flet_core/slider.py
sdk/python/packages/flet-core/src/flet_core/dropdown.py
sdk/python/packages/flet-core/src/flet_core/outlined_button.py
sdk/python/packages/flet-core/src/flet_core/text_button.py
sdk/python/packages/flet-core/src/flet_core/charts/bar_chart.py
sdk/python/packages/flet-core/src/flet_core/charts/pie_chart.py
sdk/python/packages/flet-core/src/flet_core/drag_target.py
sdk/python/packages/flet-core/src/flet_core/popup_menu_button.py
sdk/python/packages/flet-core/src/flet_core/audio.py
sdk/python/packages/flet-core/src/flet_core/date_picker.py
sdk/python/packages/flet-core/src/flet_core/canvas/canvas.py
sdk/python/packages/flet-core/src/flet_core/elevated_button.py
sdk/python/packages/flet-core/src/flet_core/textfield.py
sdk/python/packages/flet-core/src/flet_core/video.py
sdk/python/packages/flet-core/src/flet_core/chip.py
sdk/python/packages/flet-core/src/flet_core/menu_item_button.py
sdk/python/packages/flet-core/src/flet_core/submenu_button.py
sdk/python/packages/flet-core/src/flet_core/cupertino_checkbox.py
sdk/python/packages/flet-core/src/flet_core/list_tile.py
sdk/python/packages/flet-core/src/flet_core/range_slider.py
sdk/python/packages/flet-core/src/flet_core/switch.py
sdk/python/packages/flet-core/src/flet_core/webview.py
sdk/python/packages/flet-core/src/flet_core/auto_complete.py
sdk/python/packages/flet-core/src/flet_core/icon_button.py
sdk/python/packages/flet-core/src/flet_core/search_bar.py
sdk/python/packages/flet-core/src/flet_core/transparent_pointer.py
sdk/python/packages/flet-core/src/flet_core/__init__.py
Added import for OptionalEventCallable and updated type annotations for event handler parameters to use OptionalEventCallable. Simplified event handler lambda functions by removing json.loads and directly passing the event.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ndonkoHenri - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 7 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines -173 to 174
self.__on_tap_down = EventHandler(lambda e: TapEvent(**json.loads(e.data)))
self.__on_tap_down = EventHandler(lambda e: TapEvent(e))
self._add_event_handler("tap_down", self.__on_tap_down.get_handler())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential data loss in event handler

The change from json.loads(e.data) to e directly in the event handler might lead to data loss if e is not a JSON string. Ensure that e is always a JSON string or modify the TapEvent class to handle raw event data appropriately.

@@ -53,9 +59,12 @@ def __init__(self, path, files) -> None:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential issue with file list handling

Ensure that d["files"] is always a list. If there's a possibility of it being None or another type, additional checks might be necessary to avoid runtime errors.

@taaaf11
Copy link
Contributor

taaaf11 commented Jun 23, 2024

+1

@FeodorFitsner FeodorFitsner merged commit e9a651f into main Jun 24, 2024
1 of 2 checks passed
@ndonkoHenri ndonkoHenri deleted the handlers branch June 24, 2024 18:56
zrr1999 pushed a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
* Enhance typing of event handlers | reformat *Event classes

* rename OptionalEventCallback to OptionalEventCallable

---------

Co-authored-by: Feodor Fitsner <feodor@appveyor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants