From d2ab01e61b25468750f9d9f0746d4202f556cdeb Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Fri, 27 Nov 2026 22:59:58 -0500 Subject: [PATCH] Fast-path CoalescedQEventLoop.call_soon on Python 3.14 and cache MessageRow.header --- src/toad/message_rows.py | 3 +- src/toad/qt/client_start.py | 27 +++++++++++++ tests/model/test_android_perf_ux_arrivals.py | 42 ++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/toad/message_rows.py b/src/toad/message_rows.py index 06f35f8ca..d87455668 100644 --- a/src/toad/message_rows.py +++ b/src/toad/message_rows.py @@ -13,6 +13,7 @@ import time from abc import ABC, abstractmethod from dataclasses import dataclass, replace +from functools import cached_property from typing import ClassVar from agent_comms.messaging.display_references import NoHistorySource @@ -217,7 +218,7 @@ def sender(self) -> LineLink: def route_text(self) -> str: return self.header.label - @property + @cached_property def header(self) -> Divider: sender = (self.name(self.route.sender, self.role),) targets: list[Span] = [] diff --git a/src/toad/qt/client_start.py b/src/toad/qt/client_start.py index 4f18c6be0..fabf7eb58 100644 --- a/src/toad/qt/client_start.py +++ b/src/toad/qt/client_start.py @@ -6,6 +6,7 @@ import argparse import asyncio +import inspect from collections import deque from pathlib import Path from time import monotonic @@ -100,6 +101,32 @@ def __init__(self, app=None, set_running_loop: bool = False, already_running: bo self._ready_timer.setInterval(0) self._ready_timer.timeout.connect(self._drain_ready) + def call_soon(self, callback, *args, context=None): # type: ignore[override] + """Fast-path zero-delay scheduling bypassing `qasync.QEventLoop.call_later`'s + deprecated `asyncio.iscoroutinefunction` check (which fires `warnings.warn` on + every asyncio step on Python 3.14, ~4.3x slower per call).""" + if inspect.iscoroutinefunction(callback): + raise TypeError("coroutines cannot be used with call_soon") + if not callable(callback): + raise TypeError(f"callback must be callable: {type(callback).__name__}") + handle = asyncio.Handle(callback, args, self, context=context) + self._ready.append(handle) + if not self._ready_armed: + self._ready_armed = True + self._ready_timer.start() + return handle + + def call_later(self, delay, callback, *args, context=None): # type: ignore[override] + if delay <= 0: + return self.call_soon(callback, *args, context=context) + if inspect.iscoroutinefunction(callback): + raise TypeError("coroutines cannot be used with call_later") + if not callable(callback): + raise TypeError(f"callback must be callable: {type(callback).__name__}") + return super()._add_callback( + asyncio.Handle(callback, args, self, context=context), delay + ) + def _add_callback(self, handle, delay: float = 0): if delay <= 0: self._ready.append(handle) diff --git a/tests/model/test_android_perf_ux_arrivals.py b/tests/model/test_android_perf_ux_arrivals.py index a6f430a04..976b3c0c4 100644 --- a/tests/model/test_android_perf_ux_arrivals.py +++ b/tests/model/test_android_perf_ux_arrivals.py @@ -3192,3 +3192,45 @@ def test_android_tab_switch_does_not_autofocus_composer_or_raise_ime(monkeypatch assert ime_shown == [True] finally: editor.close() + + +def test_coalesced_loop_call_soon_avoids_deprecation_warning_and_caches_message_header(monkeypatch) -> None: + """CoalescedQEventLoop.call_soon/call_later avoid Python 3.14's deprecated + asyncio.iscoroutinefunction warning while still rejecting coroutines, and + MessageRow.header caches its immutable Divider across repeated reads and pickling.""" + monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen") + app = QApplication.instance() or QApplication([]) + import pickle + import warnings + import pytest + from agent_comms.messaging.messages import InfoKind, Message + from toad.message_rows import MessageRow + from toad.qt.client_start import CoalescedQEventLoop + + loop = CoalescedQEventLoop(app) + ran: list[int] = [] + try: + async def _coro(): + return None + + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always", DeprecationWarning) + h1 = loop.call_soon(ran.append, 1) + h2 = loop.call_later(0, ran.append, 2) + loop._drain_ready() + assert ran == [1, 2] + assert not [w for w in caught if "iscoroutinefunction" in str(w.message)] + with pytest.raises(TypeError): + loop.call_soon(_coro) + with pytest.raises(TypeError): + loop.call_later(0.1, _coro) + assert not h1.cancelled() and not h2.cancelled() + finally: + loop.close() + + msg = Message("toad-qt", "#comms", "hi", InfoKind, timestamp=1700000000.0) + row = MessageRow(message=msg, reader="user") + first = row.header + assert row.header is first + unpickled = pickle.loads(pickle.dumps(row)) + assert unpickled.header == first