From 05f6204ad2ac82a1973ce68dba6ad58f401c265a Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sat, 22 Feb 2025 18:09:44 +0100 Subject: [PATCH] add mypy --- README.md | 23 ++++++++++++++++------- shell.nix | 3 +++ src/dndbuster/app.py | 18 +++++++++--------- src/dndbuster/timer.py | 16 ++++++++-------- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index b3a7087..365fd93 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,9 @@ A timer that notifies you - even if "do not disturb" is enabled. * [ToC](#toc) * [Application IDs](#application-ids) * [.desktop entry](#desktop-entry) - * [Alternatives](#alternatives) * [GTK resources](#gtk-resources) + * [mypy](#mypy) + * [Alternatives](#alternatives) ## Application IDs @@ -54,6 +55,19 @@ Categories=Game; Copy `watch-icon.svg` to `~/.local/share/icons/hicolor/scalable/apps/dndbuster-stopwatch.svg` +## GTK resources + +- https://www.technett.io/post/dev/python-gtk-2/ +- https://github.com/Taiko2k/GTK4PythonTutorial?tab=readme-ov-file + +## mypy + +https://mypy.readthedocs.io/en/latest/getting_started.html + +```shell +mypy src/dndbuster/app.py +``` + ## Alternatives https://man.archlinux.org/man/notify-send.1.en @@ -63,10 +77,5 @@ nix-shell -p libnotify ``` ```shell sleep 30m -notify-send -u critical -a PleaseDisturbTimer "Alert" "Time is over!" +notify-send -u critical -a PleaseDisturbTimer "Timeout" "The time is over and your do-not-disturb mode just got busted!" ``` - -## GTK resources - -- https://www.technett.io/post/dev/python-gtk-2/ -- https://github.com/Taiko2k/GTK4PythonTutorial?tab=readme-ov-file diff --git a/shell.nix b/shell.nix index d392682..c3d98bf 100644 --- a/shell.nix +++ b/shell.nix @@ -7,6 +7,9 @@ # Typing Stubs for PyGObject pygobject-stubs + # Optional static typing for Python + mypy + # Not sure if required #dbus-python ])) diff --git a/src/dndbuster/app.py b/src/dndbuster/app.py index fa2e0d8..17f569f 100644 --- a/src/dndbuster/app.py +++ b/src/dndbuster/app.py @@ -15,17 +15,17 @@ def main(): class Application(Adw.Application): - def __init__(self): + def __init__(self) -> None: super().__init__(application_id='de.p1st.dndbuster') self.window: AppWindow | None = None - def do_activate(self): + def do_activate(self) -> None: # Windows are associated with the application. # When the last one is closed the application shuts down. self.window = AppWindow(application=self) self.window.present() - def notify_timer(self): + def notify_timer(self) -> None: notification = Gio.Notification() notification.set_title('Timeout') notification.set_body('The time is over and your do-not-disturb mode just got busted!') @@ -34,7 +34,7 @@ class Application(Adw.Application): class AppWindow(Gtk.ApplicationWindow): - def __init__(self, application: Application): + def __init__(self, application: Application) -> None: super().__init__(application=application, title='DnDBuster') self.app = application @@ -76,7 +76,7 @@ class AppWindow(Gtk.ApplicationWindow): # Weather to continue regularly calling update_progress() return not self.timer.is_paused() - def on_button_start_restart_clicked(self, _widget): + def on_button_start_restart_clicked(self, _widget) -> None: if self.timer is None: self.add_timer() @@ -87,16 +87,16 @@ class AppWindow(Gtk.ApplicationWindow): self.update_label() - def add_timer(self): + def add_timer(self) -> None: self.timer = Timer() self.timer.start() self.progress_bar.set_visible(True) self.add_timeout() - def add_timeout(self): + def add_timeout(self) -> None: GLib.timeout_add_seconds(1, self.update_progress) - def on_button_pause_resume_clicked(self, _widget): + def on_button_pause_resume_clicked(self, _widget) -> None: if self.timer.is_paused(): self.timer.resume() self.add_timeout() @@ -104,7 +104,7 @@ class AppWindow(Gtk.ApplicationWindow): self.timer.pause() self.update_label() - def update_label(self): + def update_label(self) -> None: if self.timer.is_paused(): self.button_pause_resume.set_label("Resume") else: diff --git a/src/dndbuster/timer.py b/src/dndbuster/timer.py index 6f47390..ceae7e2 100644 --- a/src/dndbuster/timer.py +++ b/src/dndbuster/timer.py @@ -2,11 +2,11 @@ import time class Timer: - def __init__(self): - self.start_seconds = None - self.pause_timer = None + def __init__(self) -> None: + self.start_seconds: float | None= None + self.pause_timer: Timer | None = None - def start(self): + def start(self) -> None: """ Start time measurement """ @@ -19,20 +19,20 @@ class Timer: """ return self.start_seconds is not None - def pause(self): + def pause(self) -> None: """ Pause time measurement. """ self.pause_timer = Timer() self.pause_timer.start() - def is_paused(self): + def is_paused(self) -> bool: """ :return: Weather time measurement is paused. """ return self.pause_timer is not None - def resume(self): + def resume(self) -> None: """ Resumes time measurement. """ @@ -41,7 +41,7 @@ class Timer: self.start_seconds += pause_delta self.pause_timer = None - def read(self): + def read(self) -> float: """ Return measured time in seconds. """