Skip to content

Commit 674c288

Browse files
AlexWaygoodhugovk
andauthored
gh-109413: Run mypy on libregrtest in CI (#112558)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 6d5e0dc commit 674c288

File tree

10 files changed

+32
-17
lines changed

10 files changed

+32
-17
lines changed

.github/workflows/mypy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
pull_request:
99
paths:
1010
- ".github/workflows/mypy.yml"
11+
- "Lib/test/libregrtest/**"
1112
- "Tools/cases_generator/**"
1213
- "Tools/clinic/**"
1314
- "Tools/peg_generator/**"
@@ -32,6 +33,7 @@ jobs:
3233
strategy:
3334
matrix:
3435
target: [
36+
"Lib/test/libregrtest",
3537
"Tools/cases_generator",
3638
"Tools/clinic",
3739
"Tools/peg_generator",

Lib/test/libregrtest/cmdline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shlex
44
import sys
55
from test.support import os_helper, Py_DEBUG
6-
from .utils import ALL_RESOURCES, RESOURCE_NAMES
6+
from .utils import ALL_RESOURCES, RESOURCE_NAMES, TestFilter
77

88

99
USAGE = """\
@@ -161,7 +161,7 @@ def __init__(self, **kwargs) -> None:
161161
self.forever = False
162162
self.header = False
163163
self.failfast = False
164-
self.match_tests = []
164+
self.match_tests: TestFilter = []
165165
self.pgo = False
166166
self.pgo_extended = False
167167
self.worker_json = None

Lib/test/libregrtest/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ def run_test(
295295
namespace = dict(locals())
296296
tracer.runctx(cmd, globals=globals(), locals=namespace)
297297
result = namespace['result']
298-
result.covered_lines = list(tracer.counts)
298+
# Mypy doesn't know about this attribute yet,
299+
# but it will do soon: https://github.com/python/typeshed/pull/11091
300+
result.covered_lines = list(tracer.counts) # type: ignore[attr-defined]
299301
else:
300302
result = run_single_test(test_name, runtests)
301303

@@ -371,7 +373,8 @@ def finalize_tests(self, coverage: trace.CoverageResults | None) -> None:
371373
os.unlink(self.next_single_filename)
372374

373375
if coverage is not None:
374-
coverage.write_results(show_missing=True, summary=True,
376+
# uses a new-in-Python 3.13 keyword argument that mypy doesn't know about yet:
377+
coverage.write_results(show_missing=True, summary=True, # type: ignore[call-arg]
375378
coverdir=self.coverage_dir,
376379
ignore_missing_files=True)
377380

@@ -432,7 +435,10 @@ def _run_tests(self, selected: TestTuple, tests: TestList | None) -> int:
432435
if self.num_workers < 0:
433436
# Use all CPUs + 2 extra worker processes for tests
434437
# that like to sleep
435-
self.num_workers = (os.process_cpu_count() or 1) + 2
438+
#
439+
# os.process.cpu_count() is new in Python 3.13;
440+
# mypy doesn't know about it yet
441+
self.num_workers = (os.process_cpu_count() or 1) + 2 # type: ignore[attr-defined]
436442

437443
# For a partial run, we do not need to clutter the output.
438444
if (self.want_header

Lib/test/libregrtest/mypy.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[mypy]
66
files = Lib/test/libregrtest
77
explicit_package_bases = True
8-
python_version = 3.11
8+
python_version = 3.12
99
platform = linux
1010
pretty = True
1111

Lib/test/libregrtest/refleak.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def runtest_refleak(test_name, test_func,
5252
except ImportError:
5353
zdc = None # Run unmodified on platforms without zipimport support
5454
else:
55-
zdc = zipimport._zip_directory_cache.copy()
55+
# private attribute that mypy doesn't know about:
56+
zdc = zipimport._zip_directory_cache.copy() # type: ignore[attr-defined]
5657
abcs = {}
5758
for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
5859
if not isabstract(abc):

Lib/test/libregrtest/results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self):
3434
self.test_times: list[tuple[float, TestName]] = []
3535
self.stats = TestStats()
3636
# used by --junit-xml
37-
self.testsuite_xml: list[str] = []
37+
self.testsuite_xml: list = []
3838
# used by -T with -j
3939
self.covered_lines: set[Location] = set()
4040

Lib/test/libregrtest/run_workers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import threading
1111
import time
1212
import traceback
13-
from typing import Literal, TextIO
13+
from typing import Any, Literal, TextIO
1414

1515
from test import support
1616
from test.support import os_helper, MS_WINDOWS
@@ -243,7 +243,9 @@ def create_json_file(self, stack: contextlib.ExitStack) -> tuple[JsonFile, TextI
243243

244244
json_fd = json_tmpfile.fileno()
245245
if MS_WINDOWS:
246-
json_handle = msvcrt.get_osfhandle(json_fd)
246+
# The msvcrt module is only available on Windows;
247+
# we run mypy with `--platform=linux` in CI
248+
json_handle: int = msvcrt.get_osfhandle(json_fd) # type: ignore[attr-defined]
247249
json_file = JsonFile(json_handle,
248250
JsonFileType.WINDOWS_HANDLE)
249251
else:
@@ -259,7 +261,7 @@ def create_worker_runtests(self, test_name: TestName, json_file: JsonFile) -> Ru
259261
else:
260262
match_tests = None
261263

262-
kwargs = {}
264+
kwargs: dict[str, Any] = {}
263265
if match_tests:
264266
kwargs['match_tests'] = [(test, True) for test in match_tests]
265267
if self.runtests.output_on_failure:
@@ -345,6 +347,7 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
345347
json_file, json_tmpfile = self.create_json_file(stack)
346348
worker_runtests = self.create_worker_runtests(test_name, json_file)
347349

350+
retcode: str | int | None
348351
retcode, tmp_files = self.run_tmp_files(worker_runtests,
349352
stdout_file.fileno())
350353

Lib/test/libregrtest/runtests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def configure_subprocess(self, popen_kwargs: dict) -> None:
3333
popen_kwargs['pass_fds'] = [self.file]
3434
case JsonFileType.WINDOWS_HANDLE:
3535
# Windows handle
36-
startupinfo = subprocess.STARTUPINFO()
36+
# We run mypy with `--platform=linux` so it complains about this:
37+
startupinfo = subprocess.STARTUPINFO() # type: ignore[attr-defined]
3738
startupinfo.lpAttributeList = {"handle_list": [self.file]}
3839
popen_kwargs['startupinfo'] = startupinfo
3940

Lib/test/libregrtest/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def setup_tests(runtests: RunTests):
124124
support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, timeout)
125125

126126
if runtests.hunt_refleak:
127-
unittest.BaseTestSuite._cleanup = False
127+
# private attribute that mypy doesn't know about:
128+
unittest.BaseTestSuite._cleanup = False # type: ignore[attr-defined]
128129

129130
if runtests.gc_threshold is not None:
130131
gc.set_threshold(runtests.gc_threshold)

Lib/test/libregrtest/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import sysconfig
1313
import tempfile
1414
import textwrap
15-
from collections.abc import Callable
15+
from collections.abc import Callable, Iterable
1616

1717
from test import support
1818
from test.support import os_helper
@@ -547,7 +547,7 @@ def is_cross_compiled():
547547
return ('_PYTHON_HOST_PLATFORM' in os.environ)
548548

549549

550-
def format_resources(use_resources: tuple[str, ...]):
550+
def format_resources(use_resources: Iterable[str]):
551551
use_resources = set(use_resources)
552552
all_resources = set(ALL_RESOURCES)
553553

@@ -580,9 +580,10 @@ def display_header(use_resources: tuple[str, ...],
580580
print("== Python build:", ' '.join(get_build_info()))
581581
print("== cwd:", os.getcwd())
582582

583-
cpu_count = os.cpu_count()
583+
cpu_count: object = os.cpu_count()
584584
if cpu_count:
585-
process_cpu_count = os.process_cpu_count()
585+
# The function is new in Python 3.13; mypy doesn't know about it yet:
586+
process_cpu_count = os.process_cpu_count() # type: ignore[attr-defined]
586587
if process_cpu_count and process_cpu_count != cpu_count:
587588
cpu_count = f"{process_cpu_count} (process) / {cpu_count} (system)"
588589
print("== CPU count:", cpu_count)

0 commit comments

Comments
 (0)