Skip to content

Commit a7b44d6

Browse files
miss-islingtonsobolevnAlexWaygood
authored
[3.13] gh-109413: Enable strict_optional = true for libregrtest/run_workers (GH-126855) (#126967)
gh-109413: Enable `strict_optional = true` for `libregrtest/run_workers` (GH-126855) (cherry picked from commit a1d9c8a) Co-authored-by: sobolevn <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent da7e93d commit a7b44d6

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Lib/test/libregrtest/mypy.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ disallow_untyped_defs = False
2222
check_untyped_defs = False
2323
warn_return_any = False
2424

25-
# Enable --strict-optional for these ASAP:
26-
[mypy-Lib.test.libregrtest.run_workers.*]
27-
strict_optional = False
28-
2925
# Various internal modules that typeshed deliberately doesn't have stubs for:
3026
[mypy-_abc.*,_opcode.*,_overlapped.*,_testcapi.*,_testinternalcapi.*,test.*]
3127
ignore_missing_imports = True

Lib/test/libregrtest/run_workers.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ def __init__(self,
102102
super().__init__()
103103

104104

105+
_NOT_RUNNING = "<not running>"
106+
107+
105108
class WorkerThread(threading.Thread):
106109
def __init__(self, worker_id: int, runner: "RunWorkers") -> None:
107110
super().__init__()
@@ -111,8 +114,8 @@ def __init__(self, worker_id: int, runner: "RunWorkers") -> None:
111114
self.output = runner.output
112115
self.timeout = runner.worker_timeout
113116
self.log = runner.log
114-
self.test_name: TestName | None = None
115-
self.start_time: float | None = None
117+
self.test_name = _NOT_RUNNING
118+
self.start_time = time.monotonic()
116119
self._popen: subprocess.Popen[str] | None = None
117120
self._killed = False
118121
self._stopped = False
@@ -129,7 +132,7 @@ def __repr__(self) -> str:
129132
popen = self._popen
130133
if popen is not None:
131134
dt = time.monotonic() - self.start_time
132-
info.extend((f'pid={self._popen.pid}',
135+
info.extend((f'pid={popen.pid}',
133136
f'time={format_duration(dt)}'))
134137
return '<%s>' % ' '.join(info)
135138

@@ -395,7 +398,7 @@ def run(self) -> None:
395398
except WorkerError as exc:
396399
mp_result = exc.mp_result
397400
finally:
398-
self.test_name = None
401+
self.test_name = _NOT_RUNNING
399402
mp_result.result.duration = time.monotonic() - self.start_time
400403
self.output.put((False, mp_result))
401404

@@ -410,6 +413,9 @@ def run(self) -> None:
410413

411414
def _wait_completed(self) -> None:
412415
popen = self._popen
416+
# only needed for mypy:
417+
if popen is None:
418+
raise ValueError("Should never access `._popen` before calling `.run()`")
413419

414420
try:
415421
popen.wait(WAIT_COMPLETED_TIMEOUT)
@@ -477,7 +483,7 @@ def __init__(self, num_workers: int, runtests: RunTests,
477483
self.worker_timeout: float | None = min(self.timeout * 1.5, self.timeout + 5 * 60)
478484
else:
479485
self.worker_timeout = None
480-
self.workers: list[WorkerThread] | None = None
486+
self.workers: list[WorkerThread] = []
481487

482488
jobs = self.runtests.get_jobs()
483489
if jobs is not None:
@@ -497,7 +503,7 @@ def start_workers(self) -> None:
497503
processes = plural(nworkers, "process", "processes")
498504
msg = (f"Run {tests} in parallel using "
499505
f"{nworkers} worker {processes}")
500-
if self.timeout:
506+
if self.timeout and self.worker_timeout is not None:
501507
msg += (" (timeout: %s, worker timeout: %s)"
502508
% (format_duration(self.timeout),
503509
format_duration(self.worker_timeout)))
@@ -549,7 +555,7 @@ def display_result(self, mp_result: MultiprocessResult) -> None:
549555
if mp_result.err_msg:
550556
# WORKER_BUG
551557
text += ' (%s)' % mp_result.err_msg
552-
elif (result.duration >= PROGRESS_MIN_TIME and not pgo):
558+
elif (result.duration and result.duration >= PROGRESS_MIN_TIME and not pgo):
553559
text += ' (%s)' % format_duration(result.duration)
554560
if not pgo:
555561
running = get_running(self.workers)

0 commit comments

Comments
 (0)