Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/toad/message_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] = []
Expand Down
27 changes: 27 additions & 0 deletions src/toad/qt/client_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import argparse
import asyncio
import inspect
from collections import deque
from pathlib import Path
from time import monotonic
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions tests/model/test_android_perf_ux_arrivals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading