Skip to content

Commit 91cb295

Browse files
committed
refactor(test): modernize waiter code with Python idioms and robust tests
why: Improve code readability, maintainability and reliability through modern Python patterns and enhanced test coverage for edge cases. what: - Replace for-loops with next() and generator expressions - Use walrus operator (:=) to reduce nesting in conditional code - Remove redundant conditional code in retry_extended.py - Fix incorrect test expectations for invalid prompt handling - Add negative test cases for line range validation - Update docstrings for better clarity and accuracy refs: #sourcery-ai
1 parent b846f03 commit 91cb295

File tree

3 files changed

+346
-48
lines changed

3 files changed

+346
-48
lines changed

src/libtmux/test/retry_extended.py

-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
logger = logging.getLogger(__name__)
1616

1717
if t.TYPE_CHECKING:
18-
import sys
1918
from collections.abc import Callable
2019

21-
if sys.version_info >= (3, 11):
22-
pass
23-
2420

2521
def retry_until_extended(
2622
fun: Callable[[], bool],

src/libtmux/test/waiter.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ def _contains_match(
10901090
content : list[str]
10911091
Lines of content to check
10921092
pattern : str
1093-
Pattern to look for within the content
1093+
String to check for in content
10941094
10951095
Returns
10961096
-------
@@ -1100,12 +1100,10 @@ def _contains_match(
11001100
content_str = "\n".join(content)
11011101
if pattern in content_str:
11021102
# Find which line contains the match
1103-
for i, line in enumerate(content):
1104-
if pattern in line:
1105-
return True, pattern, i
1106-
# If we somehow didn't find it in any individual line but it was in the
1107-
# joined string
1108-
return True, pattern, None
1103+
return next(
1104+
((True, pattern, i) for i, line in enumerate(content) if pattern in line),
1105+
(True, pattern, None),
1106+
)
11091107
return False, None, None
11101108

11111109

@@ -1129,16 +1127,18 @@ def _regex_match(
11291127
"""
11301128
content_str = "\n".join(content)
11311129
regex = pattern if isinstance(pattern, re.Pattern) else re.compile(pattern)
1132-
match = regex.search(content_str)
11331130

1134-
if match:
1131+
if match := regex.search(content_str):
11351132
matched_text = match.group(0)
11361133
# Try to find which line contains the match
1137-
for i, line in enumerate(content):
1138-
if regex.search(line):
1139-
return True, matched_text, i
1140-
# If no specific line found but match exists in joined content
1141-
return True, matched_text, None
1134+
return next(
1135+
(
1136+
(True, matched_text, i)
1137+
for i, line in enumerate(content)
1138+
if regex.search(line)
1139+
),
1140+
(True, matched_text, None),
1141+
)
11421142

11431143
return False, None, None
11441144

@@ -1159,15 +1159,17 @@ def _match_regex_across_lines(
11591159
"""
11601160
content_str = "\n".join(content)
11611161
regex = pattern if isinstance(pattern, re.Pattern) else re.compile(pattern)
1162-
match = regex.search(content_str)
11631162

1164-
if match:
1163+
if match := regex.search(content_str):
11651164
matched_text = match.group(0)
11661165
# Try to find which line contains the match
1167-
for i, line in enumerate(content):
1168-
if regex.search(line):
1169-
return True, matched_text, i
1170-
# If no specific line found but match exists in joined content
1171-
return True, matched_text, None
1166+
return next(
1167+
(
1168+
(True, matched_text, i)
1169+
for i, line in enumerate(content)
1170+
if regex.search(line)
1171+
),
1172+
(True, matched_text, None),
1173+
)
11721174

11731175
return False, None, None

0 commit comments

Comments
 (0)