From d90449e2dee92371fc6e5577939f44c0349fcf52 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Tue, 9 May 2023 17:49:52 -0400 Subject: [PATCH] Fix type errors in watchdog thread cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `observer` can be None, if --restart-on-changes was not passed, but the hotkey F2 is pressed. - `is_alive` is a bound method, not a property. It was definitely used like a property in the code I referenced. I didn't fix these type checker errors for a long time because: 1. they never caused actual problems for me, and 2. when adding a type annotation `observer: Observer | None`, Pyright gets even _more_ confused, saying it's Unknown | None instead of BaseObserver | None. The secret trick was to leave it unannotated. 3. Adding parentheses to make it `is_alive()` also made Pyright seem even more confused, saying it was Unknown — until I added the conditional. --- src/textual_paint/paint.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/textual_paint/paint.py b/src/textual_paint/paint.py index 9d52291..aa41c9c 100755 --- a/src/textual_paint/paint.py +++ b/src/textual_paint/paint.py @@ -65,10 +65,11 @@ def restart_program(): try: try: - observer.stop() - observer.join(timeout=1) - if observer.is_alive: - print("Timed out waiting for file change observer thread to stop.") + if observer: + observer.stop() + observer.join(timeout=1) + if observer.is_alive(): + print("Timed out waiting for file change observer thread to stop.") except RuntimeError as e: # Ignore "cannot join current thread" error # join() might be redundant, but I'm keeping it just in case something with threading changes in the future