Fix type errors in watchdog thread cleanup

- `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.
This commit is contained in:
Isaiah Odhner 2023-05-09 17:49:52 -04:00
parent ef409f4756
commit d90449e2de

View File

@ -65,10 +65,11 @@ def restart_program():
try: try:
try: try:
observer.stop() if observer:
observer.join(timeout=1) observer.stop()
if observer.is_alive: observer.join(timeout=1)
print("Timed out waiting for file change observer thread to stop.") if observer.is_alive():
print("Timed out waiting for file change observer thread to stop.")
except RuntimeError as e: except RuntimeError as e:
# Ignore "cannot join current thread" error # 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 # join() might be redundant, but I'm keeping it just in case something with threading changes in the future