Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchdog ignore closed_no_write #2954

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
.. currentmodule:: werkzeug

Unreleased

- The Watchdog reloader ignores file closed_no_write events. Bump the minimum
version of Watchdog to 5.0.3. :issue:`2945`


Version 3.0.4
-------------

Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ virtualenv==20.26.3
# via
# pre-commit
# tox
watchdog==4.0.2
watchdog==5.0.3
# via
# -r tests.txt
# -r typing.txt
2 changes: 1 addition & 1 deletion requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ pytest-timeout==2.3.1
# via -r tests.in
pytest-xprocess==0.23.0
# via -r tests.in
watchdog==4.0.2
watchdog==5.0.3
# via -r tests.in
2 changes: 1 addition & 1 deletion requirements/typing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ types-setuptools==73.0.0.20240822
# via -r typing.in
typing-extensions==4.12.2
# via mypy
watchdog==4.0.2
watchdog==5.0.3
# via -r typing.in
3 changes: 2 additions & 1 deletion src/werkzeug/_reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ def run_step(self) -> None:

class WatchdogReloaderLoop(ReloaderLoop):
def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
from watchdog.events import EVENT_TYPE_CLOSED_NO_WRITE
from watchdog.events import EVENT_TYPE_OPENED
from watchdog.events import FileModifiedEvent
from watchdog.events import PatternMatchingEventHandler
Expand All @@ -322,7 +323,7 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:

class EventHandler(PatternMatchingEventHandler):
def on_any_event(self, event: FileModifiedEvent): # type: ignore
if event.event_type == EVENT_TYPE_OPENED:
if event.event_type in [EVENT_TYPE_OPENED, EVENT_TYPE_CLOSED_NO_WRITE]:
return

trigger_reload(event.src_path)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from unittest.mock import patch

import pytest
from watchdog.events import EVENT_TYPE_CLOSED_NO_WRITE
from watchdog.events import EVENT_TYPE_MODIFIED
from watchdog.events import EVENT_TYPE_OPENED
from watchdog.events import FileModifiedEvent
Expand Down Expand Up @@ -120,6 +121,29 @@ def test_reloader_sys_path(tmp_path, dev_server, reloader_type):
assert client.request().status == 200


@patch.object(WatchdogReloaderLoop, "trigger_reload")
def test_watchdog_reloader_ignores_certain_events(mock_trigger_reload):
reloader = WatchdogReloaderLoop()
modified_event = FileModifiedEvent("")
modified_event.event_type = EVENT_TYPE_MODIFIED
reloader.event_handler.on_any_event(modified_event)
mock_trigger_reload.assert_called_once()

reloader.trigger_reload.reset_mock()

opened_event = FileModifiedEvent("")
opened_event.event_type = EVENT_TYPE_OPENED
reloader.event_handler.on_any_event(opened_event)
reloader.trigger_reload.assert_not_called()

reloader.trigger_reload.reset_mock()

closed_no_write_event = FileModifiedEvent("")
closed_no_write_event.event_type = EVENT_TYPE_CLOSED_NO_WRITE
reloader.event_handler.on_any_event(closed_no_write_event)
reloader.trigger_reload.assert_not_called()


@patch.object(WatchdogReloaderLoop, "trigger_reload")
def test_watchdog_reloader_ignores_opened(mock_trigger_reload):
reloader = WatchdogReloaderLoop()
Expand Down
Loading