From 17b123bae8d396def9348094cf9b391bd548f519 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Fri, 1 Dec 2023 12:17:59 +0000 Subject: [PATCH 1/5] gh-109413: libregrtest: enable mypy's `--strict-optional` check on most files --- Lib/test/libregrtest/mypy.ini | 2 +- Lib/test/libregrtest/results.py | 2 +- Lib/test/libregrtest/runtests.py | 2 +- Lib/test/libregrtest/single.py | 8 ++++---- Lib/test/libregrtest/utils.py | 9 +++++++++ 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Lib/test/libregrtest/mypy.ini b/Lib/test/libregrtest/mypy.ini index 331fe681b9f56b..22c7c7a9acef14 100644 --- a/Lib/test/libregrtest/mypy.ini +++ b/Lib/test/libregrtest/mypy.ini @@ -25,7 +25,7 @@ warn_return_any = False disable_error_code = return # Enable --strict-optional for these ASAP: -[mypy-Lib.test.libregrtest.main.*,Lib.test.libregrtest.run_workers.*,Lib.test.libregrtest.worker.*,Lib.test.libregrtest.single.*,Lib.test.libregrtest.results.*,Lib.test.libregrtest.utils.*] +[mypy-Lib.test.libregrtest.main.*,Lib.test.libregrtest.run_workers.*] strict_optional = False # Various internal modules that typeshed deliberately doesn't have stubs for: diff --git a/Lib/test/libregrtest/results.py b/Lib/test/libregrtest/results.py index 59a566c032847e..8eed73874fa300 100644 --- a/Lib/test/libregrtest/results.py +++ b/Lib/test/libregrtest/results.py @@ -31,7 +31,7 @@ def __init__(self): self.interrupted: bool = False self.worker_bug: bool = False - self.test_times: list[tuple[float, TestName]] = [] + self.test_times: list[tuple[float | None, TestName]] = [] self.stats = TestStats() # used by --junit-xml self.testsuite_xml: list = [] diff --git a/Lib/test/libregrtest/runtests.py b/Lib/test/libregrtest/runtests.py index b765ba5b41d236..95acc1d190e420 100644 --- a/Lib/test/libregrtest/runtests.py +++ b/Lib/test/libregrtest/runtests.py @@ -93,7 +93,7 @@ class RunTests: python_cmd: tuple[str, ...] | None randomize: bool random_seed: int | str - json_file: JsonFile | None + json_file: JsonFile def copy(self, **override): state = dataclasses.asdict(self) diff --git a/Lib/test/libregrtest/single.py b/Lib/test/libregrtest/single.py index 5c7bc7d40fb394..eafeb5fe26f3f3 100644 --- a/Lib/test/libregrtest/single.py +++ b/Lib/test/libregrtest/single.py @@ -237,11 +237,11 @@ def _runtest(result: TestResult, runtests: RunTests) -> None: output_on_failure = runtests.output_on_failure timeout = runtests.timeout - use_timeout = ( - timeout is not None and threading_helper.can_start_thread - ) - if use_timeout: + if timeout is not None and threading_helper.can_start_thread: + use_timeout = True faulthandler.dump_traceback_later(timeout, exit=True) + else: + use_timeout = False try: setup_tests(runtests) diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index d47e9388e62db2..906c43a78b686c 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -377,10 +377,19 @@ def get_temp_dir(tmp_dir: StrPath | None = None) -> StrPath: # Python out of the source tree, especially when the # source tree is read only. tmp_dir = sysconfig.get_config_var('srcdir') + if not tmp_dir: + raise RuntimeError( + "Could not determine the correct value for tmp_dir" + ) tmp_dir = os.path.join(tmp_dir, 'build') else: # WASI platform tmp_dir = sysconfig.get_config_var('projectbase') + if tmp_dir is None: + raise RuntimeError( + "sysconfig.get_config_var('projectbase') " + "unexpectedly returned `None` on WASI" + ) tmp_dir = os.path.join(tmp_dir, 'build') # When get_temp_dir() is called in a worker process, From 25e9d39980d7dc495c822d29d1460befbbb66992 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 1 Dec 2023 12:36:25 +0000 Subject: [PATCH 2/5] reject also the empty string Co-authored-by: Victor Stinner --- Lib/test/libregrtest/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 906c43a78b686c..d4972ce4a50d2a 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -385,10 +385,10 @@ def get_temp_dir(tmp_dir: StrPath | None = None) -> StrPath: else: # WASI platform tmp_dir = sysconfig.get_config_var('projectbase') - if tmp_dir is None: + if not tmp_dir: raise RuntimeError( "sysconfig.get_config_var('projectbase') " - "unexpectedly returned `None` on WASI" + f"unexpectedly returned {tmp_dir!r} on WASI" ) tmp_dir = os.path.join(tmp_dir, 'build') From 1e8b938bce656c2bbd3bd4244aa7cc600e9b9c86 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Fri, 1 Dec 2023 13:05:12 +0000 Subject: [PATCH 3/5] fix incorrect change in `results.py` --- Lib/test/libregrtest/results.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/libregrtest/results.py b/Lib/test/libregrtest/results.py index 8eed73874fa300..9b048ecab63e17 100644 --- a/Lib/test/libregrtest/results.py +++ b/Lib/test/libregrtest/results.py @@ -31,7 +31,7 @@ def __init__(self): self.interrupted: bool = False self.worker_bug: bool = False - self.test_times: list[tuple[float | None, TestName]] = [] + self.test_times: list[tuple[float, TestName]] = [] self.stats = TestStats() # used by --junit-xml self.testsuite_xml: list = [] @@ -117,6 +117,8 @@ def accumulate_result(self, result: TestResult, runtests: RunTests): self.worker_bug = True if result.has_meaningful_duration() and not rerun: + if result.duration is None: + raise RuntimeError("result.duration was unexpectedly None!") self.test_times.append((result.duration, test_name)) if result.stats is not None: self.stats.accumulate(result.stats) From 563db9a83bcb070e77f22c5589aa24ca889a535b Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Fri, 1 Dec 2023 13:08:45 +0000 Subject: [PATCH 4/5] revert changes to `runtests.py`, Victor's PR does it better --- Lib/test/libregrtest/runtests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/libregrtest/runtests.py b/Lib/test/libregrtest/runtests.py index 95acc1d190e420..b765ba5b41d236 100644 --- a/Lib/test/libregrtest/runtests.py +++ b/Lib/test/libregrtest/runtests.py @@ -93,7 +93,7 @@ class RunTests: python_cmd: tuple[str, ...] | None randomize: bool random_seed: int | str - json_file: JsonFile + json_file: JsonFile | None def copy(self, **override): state = dataclasses.asdict(self) From 028b8be0cf988558acf62fcdae894845df6f2b40 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 1 Dec 2023 13:14:47 +0000 Subject: [PATCH 5/5] ValueError Co-authored-by: Victor Stinner --- Lib/test/libregrtest/results.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/libregrtest/results.py b/Lib/test/libregrtest/results.py index 9b048ecab63e17..a41ea8aba028c3 100644 --- a/Lib/test/libregrtest/results.py +++ b/Lib/test/libregrtest/results.py @@ -118,7 +118,7 @@ def accumulate_result(self, result: TestResult, runtests: RunTests): if result.has_meaningful_duration() and not rerun: if result.duration is None: - raise RuntimeError("result.duration was unexpectedly None!") + raise ValueError("result.duration is None") self.test_times.append((result.duration, test_name)) if result.stats is not None: self.stats.accumulate(result.stats)