Skip to content

Commit a1d9c8a

Browse files
pythongh-109413: Enable strict_optional = true for libregrtest/run_workers (python#126855)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 2c0a21c commit a1d9c8a

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Lib/test/libregrtest/mypy.ini

-4
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

+13-7
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

@@ -401,7 +404,7 @@ def run(self) -> None:
401404
except WorkerError as exc:
402405
mp_result = exc.mp_result
403406
finally:
404-
self.test_name = None
407+
self.test_name = _NOT_RUNNING
405408
mp_result.result.duration = time.monotonic() - self.start_time
406409
self.output.put((False, mp_result))
407410

@@ -416,6 +419,9 @@ def run(self) -> None:
416419

417420
def _wait_completed(self) -> None:
418421
popen = self._popen
422+
# only needed for mypy:
423+
if popen is None:
424+
raise ValueError("Should never access `._popen` before calling `.run()`")
419425

420426
try:
421427
popen.wait(WAIT_COMPLETED_TIMEOUT)
@@ -483,7 +489,7 @@ def __init__(self, num_workers: int, runtests: RunTests,
483489
self.worker_timeout: float | None = min(self.timeout * 1.5, self.timeout + 5 * 60)
484490
else:
485491
self.worker_timeout = None
486-
self.workers: list[WorkerThread] | None = None
492+
self.workers: list[WorkerThread] = []
487493

488494
jobs = self.runtests.get_jobs()
489495
if jobs is not None:
@@ -503,7 +509,7 @@ def start_workers(self) -> None:
503509
processes = plural(nworkers, "process", "processes")
504510
msg = (f"Run {tests} in parallel using "
505511
f"{nworkers} worker {processes}")
506-
if self.timeout:
512+
if self.timeout and self.worker_timeout is not None:
507513
msg += (" (timeout: %s, worker timeout: %s)"
508514
% (format_duration(self.timeout),
509515
format_duration(self.worker_timeout)))
@@ -555,7 +561,7 @@ def display_result(self, mp_result: MultiprocessResult) -> None:
555561
if mp_result.err_msg:
556562
# WORKER_BUG
557563
text += ' (%s)' % mp_result.err_msg
558-
elif (result.duration >= PROGRESS_MIN_TIME and not pgo):
564+
elif (result.duration and result.duration >= PROGRESS_MIN_TIME and not pgo):
559565
text += ' (%s)' % format_duration(result.duration)
560566
if not pgo:
561567
running = get_running(self.workers)

0 commit comments

Comments
 (0)