Skip to content

Commit 78fb94d

Browse files
committed
fix(io): keep plyer backend probe warnings out of alerts
Without dbus or notify-send, plyer warned twice before raising NotImplementedError, which AlertsService already reports with its print fallback.
1 parent a8d9cd3 commit 78fb94d

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎src/bikes/io/services.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import typing as T
1111
import urllib.parse
12+
import warnings
1213

1314
import loguru
1415
import mlflow
@@ -146,12 +147,16 @@ def notify(self, title: str, message: str) -> None:
146147
notify_message = self._truncate(message, self._MAX_MESSAGE_LENGTH)
147148
app_name = self._truncate(app_name, self._MAX_APP_NAME_LENGTH)
148149
try:
149-
notification.notify(
150-
title=notify_title,
151-
message=notify_message,
152-
app_name=app_name,
153-
timeout=self.timeout,
154-
)
150+
# plyer warns while it probes for a backend (dbus, notify-send): the
151+
# NotImplementedError fallback below already reports a missing one.
152+
with warnings.catch_warnings():
153+
warnings.filterwarnings(action="ignore", category=UserWarning, module="plyer")
154+
notification.notify(
155+
title=notify_title,
156+
message=notify_message,
157+
app_name=app_name,
158+
timeout=self.timeout,
159+
)
155160
except NotImplementedError:
156161
print("Notifications are not supported on this system.") # noqa: T201 # user-facing fallback
157162
self._print(title=title, message=message)

‎tests/io/test_services.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# %% IMPORTS
22

3+
import warnings
4+
35
import _pytest.capture as pc
46
import _pytest.logging as pl
57
import mlflow
@@ -41,6 +43,25 @@ def test_alerts_service(enable: bool, mocker: pm.MockerFixture, capsys: pc.Captu
4143
assert capsys.readouterr().out == "[Bikes] test: hello\n", "Notification should be printed to stdout!"
4244

4345

46+
def test_alerts_service__silences_backend_probe_warnings(
47+
mocker: pm.MockerFixture, capsys: pc.CaptureFixture[str]
48+
) -> None:
49+
# given
50+
def notify(**_kwargs: str) -> None:
51+
warnings.warn_explicit("notify-send not found.", UserWarning, "notification.py", 1, module="plyer.linux")
52+
raise NotImplementedError
53+
54+
service = services.AlertsService(enable=True)
55+
mocker.patch.object(services, "notification", new=mocker.Mock()).notify.side_effect = notify
56+
# when
57+
with warnings.catch_warnings(record=True) as caught:
58+
warnings.simplefilter("always")
59+
service.notify(title="test", message="hello")
60+
# then
61+
assert caught == [], "Backend probe warnings should not reach the user!"
62+
assert "Notifications are not supported on this system." in capsys.readouterr().out
63+
64+
4465
def test_alerts_service__not_supported(mocker: pm.MockerFixture, capsys: pc.CaptureFixture[str]) -> None:
4566
# given
4667
service = services.AlertsService(enable=True)

0 commit comments

Comments
 (0)