From bc128036dab51562968919be97619a7dd8dba097 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 23 Mar 2025 15:08:29 +0100 Subject: [PATCH 1/4] wait_signal: Simplify utils --- src/pytestqt/wait_signal.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/pytestqt/wait_signal.py b/src/pytestqt/wait_signal.py index 04c66b6..9fb264a 100644 --- a/src/pytestqt/wait_signal.py +++ b/src/pytestqt/wait_signal.py @@ -1,4 +1,6 @@ import functools +import dataclasses +from typing import Any from pytestqt.exceptions import TimeoutError from pytestqt.qt_compat import qt_api @@ -257,12 +259,12 @@ def _get_timeout_error_message(self): ) +@dataclasses.dataclass class SignalAndArgs: - def __init__(self, signal_name, args): - self.signal_name = signal_name - self.args = args + signal_name: str + args: list[Any] - def _get_readable_signal_with_optional_args(self): + def __str__(self) -> str: args = repr(self.args) if self.args else "" # remove signal parameter signature, e.g. turn "some_signal(str,int)" to "some_signal", because we're adding @@ -272,18 +274,9 @@ def _get_readable_signal_with_optional_args(self): return signal_name + args - def __str__(self): - return self._get_readable_signal_with_optional_args() - - def __eq__(self, other): - if isinstance(other, self.__class__): - return self.__dict__ == other.__dict__ - else: - return False - -# Returns e.g. "3rd" for 3, or "21st" for 21 -def get_ordinal_str(n): +def get_ordinal_str(n: int) -> str: + """Return e.g. "3rd" for 3, or "21st" for 21.""" return "%d%s" % (n, {1: "st", 2: "nd", 3: "rd"}.get(n if n < 20 else n % 10, "th")) From 9be4e0f3bf0f2931f70afb4900b47f84022faacf Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 24 Mar 2025 18:18:33 +0100 Subject: [PATCH 2/4] Use zip() --- src/pytestqt/wait_signal.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pytestqt/wait_signal.py b/src/pytestqt/wait_signal.py index 9fb264a..2795365 100644 --- a/src/pytestqt/wait_signal.py +++ b/src/pytestqt/wait_signal.py @@ -563,9 +563,7 @@ def _get_signal_for_index(self, index): def _cleanup(self): super()._cleanup() - for i in range(len(self._signals)): - signal = self._signals[i] - slot = self._slots[i] + for signal, slot in zip(self._signals, self._slots): _silent_disconnect(signal, slot) del self._signals_emitted[:] self._signals_map.clear() From 730eed3bb1662ea73d0837ed5cba566b599ff6cc Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 25 Mar 2025 10:06:47 +0100 Subject: [PATCH 3/4] Clean up some comments --- src/pytestqt/wait_signal.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/pytestqt/wait_signal.py b/src/pytestqt/wait_signal.py index 2795365..a0b8a1f 100644 --- a/src/pytestqt/wait_signal.py +++ b/src/pytestqt/wait_signal.py @@ -301,22 +301,17 @@ def __init__(self, timeout=5000, raising=True, check_params_cbs=None, order="non super().__init__(timeout, raising=raising) self._order = order self._check_params_callbacks = check_params_cbs - self._signals_emitted = ( - [] - ) # list of booleans, indicates whether the signal was already emitted - self._signals_map = ( - {} - ) # maps from a unique Signal to a list of indices where to expect signal instance emits - self._signals = ( - [] - ) # list of all Signals (for compatibility with _AbstractSignalBlocker) + self._signals_emitted: list[bool] = [] # whether the signal was already emitted + # maps from a unique Signal to a list of indices where to expect signal instance emits + self._signals_map = {} + # list of all Signals (for compatibility with _AbstractSignalBlocker) + self._signals = [] self._slots = [] # list of slot functions self._signal_expected_index = 0 # only used when forcing order self._strict_order_violated = False self._actual_signal_and_args_at_violation = None - self._signal_names = ( - {} - ) # maps from the unique Signal to the name of the signal (as string) + # maps from the unique Signal to the name of the signal (as string) + self._signal_names = {} self.all_signals_and_args = [] # list of SignalAndArgs instances def add_signals(self, signals): From 2b9b92a7cbce83fd33d2eb37a368942cc936603c Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 25 Mar 2025 22:45:40 +0100 Subject: [PATCH 4/4] Fix SignalsAndArgs.args type --- src/pytestqt/wait_signal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytestqt/wait_signal.py b/src/pytestqt/wait_signal.py index a0b8a1f..359e744 100644 --- a/src/pytestqt/wait_signal.py +++ b/src/pytestqt/wait_signal.py @@ -262,7 +262,7 @@ def _get_timeout_error_message(self): @dataclasses.dataclass class SignalAndArgs: signal_name: str - args: list[Any] + args: tuple[Any, ...] def __str__(self) -> str: args = repr(self.args) if self.args else ""