Skip to content

Commit 9c772bc

Browse files
committed
fix: ruff + mypy
1 parent d315d30 commit 9c772bc

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

src/watchdog/observers/inotify.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@
115115

116116

117117
class FileSystemEventCtor(Protocol):
118-
def __call__(self, src_path: bytes | str, dest_path: bytes | str = "") -> FileSystemEvent:
119-
...
118+
def __call__(self, src_path: bytes | str, dest_path: bytes | str = "") -> FileSystemEvent: ...
120119

121120

122121
@dataclass
@@ -366,8 +365,12 @@ def _remove_watch_internally(self, wd: WatchDescriptor) -> None:
366365
self._active_callbacks_by_path[path] = wd2
367366

368367

369-
def _select_event_type(dir_event: type[FileSystemEvent], file_event: type[FileSystemEvent], *, is_directory: bool
370-
) -> FileSystemEventCtor:
368+
def _select_event_type(
369+
dir_event: type[FileSystemEvent],
370+
file_event: type[FileSystemEvent],
371+
*,
372+
is_directory: bool,
373+
) -> FileSystemEventCtor:
371374
"""selects the correct FileSystemEvent Type based on `is_directory` and returns it."""
372375
return cast(FileSystemEventCtor, dir_event if is_directory else file_event)
373376

@@ -398,7 +401,7 @@ def __init__(
398401
*,
399402
timeout: float = DEFAULT_EMITTER_TIMEOUT,
400403
event_filter: list[type[FileSystemEvent]] | None = None,
401-
inotify_fd: InotifyFD | None = None
404+
inotify_fd: InotifyFD | None = None,
402405
) -> None:
403406
super().__init__(event_queue, watch, timeout=timeout, event_filter=event_filter)
404407
self._lock: threading.Lock = threading.Lock()
@@ -413,7 +416,7 @@ def on_thread_start(self) -> None:
413416
path,
414417
is_recursive=self.watch.is_recursive,
415418
event_mask=event_mask,
416-
follow_symlink=self.watch.follow_symlink
419+
follow_symlink=self.watch.follow_symlink,
417420
)
418421

419422
def on_thread_stop(self) -> None:
@@ -510,13 +513,16 @@ def get_event_mask_from_filter(self) -> Mask:
510513
elif cls in {DirCreatedEvent, FileCreatedEvent}:
511514
event_mask = Mask(event_mask | InotifyConstants.IN_MOVE | InotifyConstants.IN_CREATE)
512515
elif cls is DirModifiedEvent:
513-
event_mask = Mask(event_mask | (
514-
InotifyConstants.IN_MOVE
515-
| InotifyConstants.IN_ATTRIB
516-
| InotifyConstants.IN_MODIFY
517-
| InotifyConstants.IN_CREATE
518-
| InotifyConstants.IN_CLOSE_WRITE
519-
))
516+
event_mask = Mask(
517+
event_mask
518+
| (
519+
InotifyConstants.IN_MOVE
520+
| InotifyConstants.IN_ATTRIB
521+
| InotifyConstants.IN_MODIFY
522+
| InotifyConstants.IN_CREATE
523+
| InotifyConstants.IN_CLOSE_WRITE
524+
)
525+
)
520526
elif cls is FileModifiedEvent:
521527
event_mask = Mask(event_mask | InotifyConstants.IN_ATTRIB | InotifyConstants.IN_MODIFY)
522528
elif cls in {DirDeletedEvent, FileDeletedEvent}:

src/watchdog/observers/inotify_c.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333

3434
inotify_add_watch = cast(
3535
Callable[[int, bytes, int], WatchDescriptor],
36-
ctypes.CFUNCTYPE(c_int, c_int, c_char_p, c_uint32, use_errno=True)(("inotify_add_watch", libc)))
36+
ctypes.CFUNCTYPE(c_int, c_int, c_char_p, c_uint32, use_errno=True)(("inotify_add_watch", libc)),
37+
)
3738

3839
inotify_rm_watch = cast(
3940
Callable[[int, WatchDescriptor], int],
40-
ctypes.CFUNCTYPE(c_int, c_int, c_uint32, use_errno=True)(("inotify_rm_watch", libc)))
41+
ctypes.CFUNCTYPE(c_int, c_int, c_uint32, use_errno=True)(("inotify_rm_watch", libc)),
42+
)
4143

4244
inotify_init = cast(Callable[[], int], ctypes.CFUNCTYPE(c_int, use_errno=True)(("inotify_init", libc)))
4345

@@ -124,9 +126,7 @@ class InotifyConstants:
124126

125127

126128
def _get_mask_string(mask: int) -> str:
127-
return "|".join(
128-
name for name, c_val in INOTIFY_ALL_CONSTANTS.items() if mask & c_val
129-
)
129+
return "|".join(name for name, c_val in INOTIFY_ALL_CONSTANTS.items() if mask & c_val)
130130

131131

132132
class InotifyEventStruct(ctypes.Structure):
@@ -173,6 +173,7 @@ def on_watch_deleted(self, wd: WatchDescriptor) -> None:
173173
@dataclass
174174
class Watch:
175175
"""Represents an inotify watch"""
176+
176177
wd: WatchDescriptor
177178
"""the inotify watch descriptor"""
178179
mask: Mask
@@ -194,11 +195,13 @@ def is_used(self) -> bool:
194195
return bool(self.callbacks)
195196

196197
def short_str(self) -> str:
197-
contents = ", ".join([
198-
f"wd={self.wd}",
199-
f"mask={_get_mask_string(self.mask)}",
200-
f"_initial_creation_path={self._initial_creation_path!r}",
201-
])
198+
contents = ", ".join(
199+
[
200+
f"wd={self.wd}",
201+
f"mask={_get_mask_string(self.mask)}",
202+
f"_initial_creation_path={self._initial_creation_path!r}",
203+
]
204+
)
202205
return f"<{type(self).__name__}: {contents}>"
203206

204207

@@ -522,7 +525,7 @@ def _parse_event_buffer(event_buffer: bytes) -> Generator[tuple[WatchDescriptor,
522525
i = 0
523526
while i + 16 <= len(event_buffer):
524527
wd, mask, cookie, length = struct.unpack_from("iIII", event_buffer, i)
525-
name = event_buffer[i + 16: i + 16 + length].rstrip(b"\0")
528+
name = event_buffer[i + 16 : i + 16 + length].rstrip(b"\0")
526529
i += 16 + length
527530
yield wd, mask, cookie, name
528531

@@ -533,8 +536,7 @@ def _parse_event_buffer(event_buffer: bytes) -> Generator[tuple[WatchDescriptor,
533536

534537
@dataclass(unsafe_hash=True, frozen=True)
535538
class InotifyEvent:
536-
"""Inotify event struct wrapper.
537-
"""
539+
"""Inotify event struct wrapper."""
538540

539541
wd: WatchDescriptor
540542
"""Watch descriptor"""
@@ -611,10 +613,12 @@ def is_directory(self) -> bool:
611613
return self.is_delete_self or self.is_move_self or self.mask & InotifyConstants.IN_ISDIR > 0
612614

613615
def __repr__(self) -> str:
614-
contents = ", ".join([
615-
f"wd={self.wd}",
616-
f"mask={_get_mask_string(self.mask)}",
617-
f"cookie={self.cookie}",
618-
f"name={os.fsdecode(self.name)!r}",
619-
])
616+
contents = ", ".join(
617+
[
618+
f"wd={self.wd}",
619+
f"mask={_get_mask_string(self.mask)}",
620+
f"cookie={self.cookie}",
621+
f"name={os.fsdecode(self.name)!r}",
622+
]
623+
)
620624
return f"<{type(self).__name__}: {contents}>"

src/watchdog/observers/inotify_move_event_grouper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
class PathedInotifyEvent(NamedTuple):
2525
"""An InotifyEvent and its full source path"""
26+
2627
ev: InotifyEvent
2728
path: bytes
2829

@@ -79,6 +80,7 @@ def matching_from_event(event: GroupedInotifyEvent) -> bool:
7980
def get_queued_moved_from_event(self, cookie: int) -> PathedInotifyEvent | None:
8081
"""Finds a queued IN_MOVED_FROM event with the give cookie, but does not
8182
remove it."""
83+
8284
def matching_from_event(event: GroupedInotifyEvent) -> bool:
8385
return isinstance(event, PathedInotifyEvent) and event.ev.is_moved_from and event.ev.cookie == cookie
8486

src/watchdog/utils/patterns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _get_sep(path: PurePath) -> str:
3838

3939
def _full_match(path: PurePath, pattern: str) -> bool:
4040
try:
41-
return path.full_match(pattern)
41+
return path.full_match(pattern) # type: ignore[attr-defined]
4242
except AttributeError:
4343
# Replicate for python <3.13
4444
# Please remove this, backwards_compat.py, and python license attributions

tests/test_inotify_watch_group.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def create_inotify_watch(path: bytes, *, recursive: bool = False, follow_symlink
3232
path,
3333
is_recursive=recursive,
3434
follow_symlink=follow_symlink,
35-
event_mask=WATCHDOG_ALL_EVENTS
35+
event_mask=WATCHDOG_ALL_EVENTS,
3636
)
3737

3838

@@ -246,6 +246,3 @@ def assert_inotify_root_events(inotify_root: InotifyWatchGroup) -> None:
246246
inotify_root.deactivate()
247247
inotify_a.deactivate()
248248
cleanup()
249-
250-
251-

0 commit comments

Comments
 (0)