From 96d3e407895cbd80fbaa261dd02627f0366a342e Mon Sep 17 00:00:00 2001 From: Yaroslav Chvanov Date: Sat, 4 Mar 2023 14:58:42 +0300 Subject: [PATCH] piper: fix optional access warnings --- piper/buttondialog.py | 4 ++++ piper/mouseperspective.py | 6 ++++++ piper/window.py | 7 +++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/piper/buttondialog.py b/piper/buttondialog.py index a9a717b..6f0cbfb 100644 --- a/piper/buttondialog.py +++ b/piper/buttondialog.py @@ -321,11 +321,14 @@ class ButtonDialog(Gtk.Dialog): type = RatbagdButton.Macro.KEY_PRESS elif event.type == Gdk.EventType.KEY_RELEASE: type = RatbagdButton.Macro.KEY_RELEASE + else: + raise ValueError("Incorrect event type") # TODO: this needs to be checked for its Wayland support. if not self._XORG_KEYCODE_OFFSET <= event.hardware_keycode <= 255: print("Keycode is not within the valid range.", file=sys.stderr) else: + assert self._current_macro is not None self._current_macro.append( type, event.hardware_keycode - self._XORG_KEYCODE_OFFSET ) @@ -369,6 +372,7 @@ class ButtonDialog(Gtk.Dialog): @Gtk.Template.Callback("_on_apply_button_clicked") def _on_apply_button_clicked(self, button: Gtk.Button) -> None: if self.stack.get_visible_child_name() == "capture": + assert self._current_macro is not None self._current_macro.accept() return Gdk.EVENT_PROPAGATE diff --git a/piper/mouseperspective.py b/piper/mouseperspective.py index 9c00d8f..0155ff9 100644 --- a/piper/mouseperspective.py +++ b/piper/mouseperspective.py @@ -79,6 +79,7 @@ class MousePerspective(Gtk.Overlay): ) active_profile = device.active_profile + assert active_profile is not None self._set_profile(active_profile) self.button_profile.set_visible(len(device.profiles) > 1) @@ -106,6 +107,8 @@ class MousePerspective(Gtk.Overlay): self.listbox_profiles.select_row(row) def _set_profile(self, profile: RatbagdProfile) -> None: + assert self._device is not None + self.stack.foreach(Gtk.Widget.destroy) if profile.resolutions: self.stack.add_titled( @@ -148,6 +151,7 @@ class MousePerspective(Gtk.Overlay): @Gtk.Template.Callback("_on_save_button_clicked") def _on_save_button_clicked(self, _button: Gtk.Button) -> None: + assert self._device is not None self._device.commit() @Gtk.Template.Callback("_on_notification_error_close_clicked") @@ -163,6 +167,7 @@ class MousePerspective(Gtk.Overlay): @Gtk.Template.Callback("_on_add_profile_button_clicked") def _on_add_profile_button_clicked(self, button: Gtk.Button) -> None: + assert self._device is not None # Enable the first disabled profile we find. for profile in self._device.profiles: if not profile.disabled: @@ -175,6 +180,7 @@ class MousePerspective(Gtk.Overlay): def _on_profile_notify_enabled( self, profile: RatbagdProfile, pspec: Optional[GObject.ParamSpec] ) -> None: + assert self._device is not None # We're only interested in the case where the last profile is disabled, # so that we can reset the sensitivity of the add button. if profile.disabled and profile == self._device.profiles[-1]: diff --git a/piper/window.py b/piper/window.py index e540f27..81bb437 100644 --- a/piper/window.py +++ b/piper/window.py @@ -208,8 +208,11 @@ class Window(Gtk.ApplicationWindow): def _on_device_selected(self, perspective, device: RatbagdDevice) -> None: self._present_mouse_perspective(device) - def _get_child(self, name: str) -> Optional[Gtk.Widget]: - return self.stack_perspectives.get_child_by_name(name) + def _get_child(self, name: str) -> Gtk.Widget: + child = self.stack_perspectives.get_child_by_name(name) + if child is None: + raise ValueError(f"Child `{name}` was not found") + return child def _perspective_add_back_button(self, perspective, ratbag: Ratbagd) -> None: button_back = Gtk.Button.new_from_icon_name(