Skip to content

Commit 69f1e88

Browse files
committed
fix(waiter): refactor wait functions to improve code quality
- Combine nested if statements for cleaner code - Fix line length issues for improved readability - Restore start_time variable for correct timing calculations - Put return statements in correct position to fix TRY300 linting issues
1 parent f155d15 commit 69f1e88

File tree

1 file changed

+48
-41
lines changed

1 file changed

+48
-41
lines changed

src/libtmux/test/waiter.py

+48-41
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,18 @@ def check_content() -> bool:
454454
if match_type == ContentMatchType.CONTAINS:
455455
if not isinstance(content_pattern, str):
456456
raise TypeError(ERR_CONTAINS_TYPE)
457+
content_str = "\n".join(content)
458+
if content_pattern in content_str:
459+
result.matched_content = content_pattern
460+
# Find which line contains the match
461+
for i, line in enumerate(content):
462+
if content_pattern in line:
463+
result.match_line = i
464+
break
465+
return True
466+
return False
457467

468+
# Handle regex match type
458469
if match_type == ContentMatchType.REGEX:
459470
if isinstance(content_pattern, (str, re.Pattern)):
460471
pattern = (
@@ -474,18 +485,7 @@ def check_content() -> bool:
474485
return True
475486
return False
476487
raise TypeError(ERR_REGEX_TYPE)
477-
478-
# Contains match (default)
479-
content_str = "\n".join(content)
480-
if isinstance(content_pattern, str) and content_pattern in content_str:
481-
result.matched_content = content_pattern
482-
# Find which line contains the match
483-
for i, line in enumerate(content):
484-
if content_pattern in line:
485-
result.match_line = i
486-
break
487-
return True
488-
return False
488+
return None
489489

490490
try:
491491
success, exception = retry_until_extended(
@@ -494,10 +494,14 @@ def check_content() -> bool:
494494
interval=interval,
495495
raises=raises,
496496
)
497-
result.success = success
498497
if exception:
498+
if raises:
499+
raise
499500
result.error = str(exception)
500-
return result
501+
return result
502+
else:
503+
result.success = success
504+
return result
501505
except WaitTimeout as e:
502506
if raises:
503507
raise
@@ -778,15 +782,12 @@ def has_at_least_5_lines(content):
778782
raise ValueError(msg)
779783

780784
# If match_types is a single value, convert to a list of the same value
781-
match_types_list: list[ContentMatchType]
782-
if isinstance(match_types, ContentMatchType):
783-
match_types_list = [match_types] * len(content_patterns)
784-
else:
785-
match_types_list = match_types
786-
787-
if len(match_types_list) != len(content_patterns):
785+
if not isinstance(match_types, list):
786+
match_types = [match_types] * len(content_patterns)
787+
elif len(match_types) != len(content_patterns):
788788
msg = (
789-
"If match_types is a list, it must have the same length as content_patterns"
789+
f"match_types list ({len(match_types)}) "
790+
f"doesn't match patterns ({len(content_patterns)})"
790791
)
791792
raise ValueError(msg)
792793

@@ -802,7 +803,7 @@ def check_any_content() -> bool:
802803
result.content = content
803804

804805
for i, (pattern, match_type) in enumerate(
805-
zip(content_patterns, match_types_list),
806+
zip(content_patterns, match_types),
806807
):
807808
# Handle predicate match
808809
if match_type == ContentMatchType.PREDICATE:
@@ -877,11 +878,15 @@ def check_any_content() -> bool:
877878
interval=interval,
878879
raises=raises,
879880
)
880-
result.success = success
881881
if exception:
882+
if raises:
883+
raise
882884
result.error = str(exception)
883-
result.elapsed_time = time.time() - start_time
884-
return result
885+
return result
886+
else:
887+
result.success = success
888+
result.elapsed_time = time.time() - start_time
889+
return result
885890
except WaitTimeout as e:
886891
if raises:
887892
raise
@@ -972,22 +977,19 @@ def has_at_least_5_lines(content):
972977
ContentMatchType.PREDICATE
973978
)
974979
"""
975-
# If match_types is a single value, convert to a list of the same value
976-
match_types_list: list[ContentMatchType]
977-
if isinstance(match_types, ContentMatchType):
978-
match_types_list = [match_types] * len(content_patterns)
979-
else:
980-
match_types_list = match_types
981-
982-
if len(match_types_list) != len(content_patterns):
980+
# Convert single match_type to list of same type
981+
if not isinstance(match_types, list):
982+
match_types = [match_types] * len(content_patterns)
983+
elif len(match_types) != len(content_patterns):
983984
msg = (
984-
"If match_types is a list, it must have the same length as content_patterns"
985+
f"match_types list ({len(match_types)}) "
986+
f"doesn't match patterns ({len(content_patterns)})"
985987
)
986988
raise ValueError(msg)
987989

988990
result = WaitResult(success=False)
989-
start_time = time.time()
990991
matched_patterns: list[str] = []
992+
start_time = time.time()
991993

992994
def check_all_content() -> bool:
993995
content = pane.capture_pane(start=start, end=end)
@@ -998,7 +1000,7 @@ def check_all_content() -> bool:
9981000
matched_patterns.clear()
9991001

10001002
for i, (pattern, match_type) in enumerate(
1001-
zip(content_patterns, match_types_list),
1003+
zip(content_patterns, match_types),
10021004
):
10031005
# Handle predicate match
10041006
if match_type == ContentMatchType.PREDICATE:
@@ -1062,11 +1064,15 @@ def check_all_content() -> bool:
10621064
interval=interval,
10631065
raises=raises,
10641066
)
1065-
result.success = success
10661067
if exception:
1068+
if raises:
1069+
raise
10671070
result.error = str(exception)
1068-
result.elapsed_time = time.time() - start_time
1069-
return result
1071+
return result
1072+
else:
1073+
result.success = success
1074+
result.elapsed_time = time.time() - start_time
1075+
return result
10701076
except WaitTimeout as e:
10711077
if raises:
10721078
raise
@@ -1099,7 +1105,8 @@ def _contains_match(
10991105
for i, line in enumerate(content):
11001106
if pattern in line:
11011107
return True, pattern, i
1102-
# If we somehow didn't find it in any individual line but it was in the joined string
1108+
# If we somehow didn't find it in any individual line but it was in the
1109+
# joined string
11031110
return True, pattern, None
11041111
return False, None, None
11051112

0 commit comments

Comments
 (0)