Skip to content

Commit

Permalink
Run mypy on libregrtest in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Nov 30, 2023
1 parent 2223899 commit 5d3802f
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
pull_request:
paths:
- ".github/workflows/mypy.yml"
- "Lib/test/libregrtest/**"
- "Tools/cases_generator/**"
- "Tools/clinic/**"
- "Tools/peg_generator/**"
Expand All @@ -32,6 +33,7 @@ jobs:
strategy:
matrix:
target: [
"Lib/test/libregrtest",
"Tools/cases_generator",
"Tools/clinic",
"Tools/peg_generator",
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/libregrtest/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shlex
import sys
from test.support import os_helper, Py_DEBUG
from .utils import ALL_RESOURCES, RESOURCE_NAMES
from .utils import ALL_RESOURCES, RESOURCE_NAMES, TestFilter


USAGE = """\
Expand Down Expand Up @@ -161,7 +161,7 @@ def __init__(self, **kwargs) -> None:
self.forever = False
self.header = False
self.failfast = False
self.match_tests = []
self.match_tests: TestFilter = []
self.pgo = False
self.pgo_extended = False
self.worker_json = None
Expand Down
12 changes: 9 additions & 3 deletions Lib/test/libregrtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ def run_test(
namespace = dict(locals())
tracer.runctx(cmd, globals=globals(), locals=namespace)
result = namespace['result']
result.covered_lines = list(tracer.counts)
# Mypy doesn't know about this attribute yet,
# but it will do soon: https://github.com/python/typeshed/pull/11091
result.covered_lines = list(tracer.counts) # type: ignore[attr-defined]
else:
result = run_single_test(test_name, runtests)

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

if coverage is not None:
coverage.write_results(show_missing=True, summary=True,
# uses a new-in-py313 keyword argument that mypy doesn't know about yet:
coverage.write_results(show_missing=True, summary=True, # type: ignore[call-arg]
coverdir=self.coverage_dir,
ignore_missing_files=True)

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

# For a partial run, we do not need to clutter the output.
if (self.want_header
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/libregrtest/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[mypy]
files = Lib/test/libregrtest
explicit_package_bases = True
python_version = 3.11
python_version = 3.12
platform = linux
pretty = True

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/libregrtest/refleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def runtest_refleak(test_name, test_func,
except ImportError:
zdc = None # Run unmodified on platforms without zipimport support
else:
zdc = zipimport._zip_directory_cache.copy()
# private attribute that mypy doesn't know about:
zdc = zipimport._zip_directory_cache.copy() # type: ignore[attr-defined]
abcs = {}
for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
if not isabstract(abc):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/libregrtest/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self):
self.test_times: list[tuple[float, TestName]] = []
self.stats = TestStats()
# used by --junit-xml
self.testsuite_xml: list[str] = []
self.testsuite_xml: list = []
# used by -T with -j
self.covered_lines: set[Location] = set()

Expand Down
9 changes: 6 additions & 3 deletions Lib/test/libregrtest/run_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import threading
import time
import traceback
from typing import Literal, TextIO
from typing import Any, Literal, TextIO

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

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

kwargs = {}
kwargs: dict[str, Any] = {}
if match_tests:
kwargs['match_tests'] = [(test, True) for test in match_tests]
if self.runtests.output_on_failure:
Expand Down Expand Up @@ -345,6 +347,7 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
json_file, json_tmpfile = self.create_json_file(stack)
worker_runtests = self.create_worker_runtests(test_name, json_file)

retcode: str | int | None
retcode, tmp_files = self.run_tmp_files(worker_runtests,
stdout_file.fileno())

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/libregrtest/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def configure_subprocess(self, popen_kwargs: dict) -> None:
popen_kwargs['pass_fds'] = [self.file]
case JsonFileType.WINDOWS_HANDLE:
# Windows handle
startupinfo = subprocess.STARTUPINFO()
# We run mypy with `--platform=linux` so it complains about this:
startupinfo = subprocess.STARTUPINFO() # type: ignore[attr-defined]
startupinfo.lpAttributeList = {"handle_list": [self.file]}
popen_kwargs['startupinfo'] = startupinfo

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/libregrtest/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def setup_tests(runtests: RunTests):
support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, timeout)

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

if runtests.gc_threshold is not None:
gc.set_threshold(runtests.gc_threshold)
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/libregrtest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sysconfig
import tempfile
import textwrap
from collections.abc import Callable
from collections.abc import Callable, Iterable

from test import support
from test.support import os_helper
Expand Down Expand Up @@ -547,7 +547,7 @@ def is_cross_compiled():
return ('_PYTHON_HOST_PLATFORM' in os.environ)


def format_resources(use_resources: tuple[str, ...]):
def format_resources(use_resources: Iterable[str]):
use_resources = set(use_resources)
all_resources = set(ALL_RESOURCES)

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

cpu_count = os.cpu_count()
cpu_count: object = os.cpu_count()
if cpu_count:
process_cpu_count = os.process_cpu_count()
# The function is new in py313; mypy doesn't know about it yet:
process_cpu_count = os.process_cpu_count() # type: ignore[attr-defined]
if process_cpu_count and process_cpu_count != cpu_count:
cpu_count = f"{process_cpu_count} (process) / {cpu_count} (system)"
print("== CPU count:", cpu_count)
Expand Down

0 comments on commit 5d3802f

Please sign in to comment.