Skip to content

Commit 9ffa5f3

Browse files
committed
Add type annotations
1 parent 2c80e03 commit 9ffa5f3

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

bpython/curtsiesfrontend/filewatch.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
22
from collections import defaultdict
3+
from typing import Dict, Iterable, Set, List
34

45
from .. import importcompletion
56

67
try:
78
from watchdog.observers import Observer
8-
from watchdog.events import FileSystemEventHandler
9+
from watchdog.events import FileSystemEventHandler, FileSystemEvent
910
except ImportError:
1011

1112
def ModuleChangedEventHandler(*args):
@@ -14,26 +15,26 @@ def ModuleChangedEventHandler(*args):
1415
else:
1516

1617
class ModuleChangedEventHandler(FileSystemEventHandler): # type: ignore [no-redef]
17-
def __init__(self, paths, on_change):
18-
self.dirs = defaultdict(set)
18+
def __init__(self, paths: Iterable[str], on_change) -> None:
19+
self.dirs: Dict[str, Set[str]] = defaultdict(set)
1920
self.on_change = on_change
20-
self.modules_to_add_later = []
21+
self.modules_to_add_later: List[str] = []
2122
self.observer = Observer()
22-
self.old_dirs = defaultdict(set)
23+
self.old_dirs: Dict[str, Set[str]] = defaultdict(set)
2324
self.started = False
2425
self.activated = False
2526
for path in paths:
2627
self._add_module(path)
2728

2829
super().__init__()
2930

30-
def reset(self):
31+
def reset(self) -> None:
3132
self.dirs = defaultdict(set)
3233
del self.modules_to_add_later[:]
3334
self.old_dirs = defaultdict(set)
3435
self.observer.unschedule_all()
3536

36-
def _add_module(self, path):
37+
def _add_module(self, path: str) -> None:
3738
"""Add a python module to track changes"""
3839
path = os.path.abspath(path)
3940
for suff in importcompletion.SUFFIXES:
@@ -45,10 +46,10 @@ def _add_module(self, path):
4546
self.observer.schedule(self, dirname, recursive=False)
4647
self.dirs[dirname].add(path)
4748

48-
def _add_module_later(self, path):
49+
def _add_module_later(self, path: str) -> None:
4950
self.modules_to_add_later.append(path)
5051

51-
def track_module(self, path):
52+
def track_module(self, path: str) -> None:
5253
"""
5354
Begins tracking this if activated, or remembers to track later.
5455
"""
@@ -57,7 +58,7 @@ def track_module(self, path):
5758
else:
5859
self._add_module_later(path)
5960

60-
def activate(self):
61+
def activate(self) -> None:
6162
if self.activated:
6263
raise ValueError(f"{self!r} is already activated.")
6364
if not self.started:
@@ -70,13 +71,13 @@ def activate(self):
7071
del self.modules_to_add_later[:]
7172
self.activated = True
7273

73-
def deactivate(self):
74+
def deactivate(self) -> None:
7475
if not self.activated:
7576
raise ValueError(f"{self!r} is not activated.")
7677
self.observer.unschedule_all()
7778
self.activated = False
7879

79-
def on_any_event(self, event):
80+
def on_any_event(self, event: FileSystemEvent) -> None:
8081
dirpath = os.path.dirname(event.src_path)
8182
paths = [path + ".py" for path in self.dirs[dirpath]]
8283
if event.src_path in paths:

stubs/watchdog/events.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
class FileSystemEvent:
2+
@property
3+
def src_path(self) -> str: ...
4+
15
class FileSystemEventHandler: ...

stubs/watchdog/observers.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from .events import FileSystemEventHandler
2+
13
class Observer:
24
def start(self): ...
3-
def schedule(self, dirname: str, recursive: bool): ...
5+
def schedule(
6+
self, observer: FileSystemEventHandler, dirname: str, recursive: bool
7+
): ...
48
def unschedule_all(self): ...

0 commit comments

Comments
 (0)