Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kunit: tool: fix pre-existing `mypy --strict` errors and update run_checks.py

Basically, get this command to be happy and make run_checks.py happy
$ mypy --strict --exclude '_test.py$' --exclude qemu_configs/ ./tools/testing/kunit/

Primarily the changes are
* add `-> None` return type annotations
* add all the missing argument type annotations

Previously, we had false positives from mypy in `main()`, see commit
09641f7c7d8f ("kunit: tool: surface and address more typing issues").
But after commit 2dc9d6ca52a4 ("kunit: kunit.py extract handlers")
refactored things, the variable name reuse mypy hated is gone.

Note: mypy complains we don't annotate the types the unused args in our
signal handler. That's silly.
But to make it happy, I've copy-pasted an appropriate annotation from
https://github.com/python/typing/discussions/1042#discussioncomment-2013595.

Reported-by: Johannes Berg <johannes.berg@intel.com>
Link: https://lore.kernel.org/linux-kselftest/9a172b50457f4074af41fe1dc8e55dcaf4795d7e.camel@sipsolutions.net/
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Daniel Latypov and committed by
Shuah Khan
1da2e622 126901ba

+31 -30
+12 -12
tools/testing/kunit/kunit.py
··· 269 269 def get_default_jobs() -> int: 270 270 return len(os.sched_getaffinity(0)) 271 271 272 - def add_common_opts(parser) -> None: 272 + def add_common_opts(parser: argparse.ArgumentParser) -> None: 273 273 parser.add_argument('--build_dir', 274 274 help='As in the make command, it specifies the build ' 275 275 'directory.', ··· 320 320 help='Additional QEMU arguments, e.g. "-smp 8"', 321 321 action='append', metavar='') 322 322 323 - def add_build_opts(parser) -> None: 323 + def add_build_opts(parser: argparse.ArgumentParser) -> None: 324 324 parser.add_argument('--jobs', 325 325 help='As in the make command, "Specifies the number of ' 326 326 'jobs (commands) to run simultaneously."', 327 327 type=int, default=get_default_jobs(), metavar='N') 328 328 329 - def add_exec_opts(parser) -> None: 329 + def add_exec_opts(parser: argparse.ArgumentParser) -> None: 330 330 parser.add_argument('--timeout', 331 331 help='maximum number of seconds to allow for all tests ' 332 332 'to run. This does not include time taken to build the ' ··· 351 351 type=str, 352 352 choices=['suite', 'test']) 353 353 354 - def add_parse_opts(parser) -> None: 354 + def add_parse_opts(parser: argparse.ArgumentParser) -> None: 355 355 parser.add_argument('--raw_output', help='If set don\'t parse output from kernel. ' 356 356 'By default, filters to just KUnit output. Use ' 357 357 '--raw_output=all to show everything', ··· 386 386 extra_qemu_args=qemu_args) 387 387 388 388 389 - def run_handler(cli_args): 389 + def run_handler(cli_args: argparse.Namespace) -> None: 390 390 if not os.path.exists(cli_args.build_dir): 391 391 os.mkdir(cli_args.build_dir) 392 392 ··· 405 405 sys.exit(1) 406 406 407 407 408 - def config_handler(cli_args): 408 + def config_handler(cli_args: argparse.Namespace) -> None: 409 409 if cli_args.build_dir and ( 410 410 not os.path.exists(cli_args.build_dir)): 411 411 os.mkdir(cli_args.build_dir) ··· 421 421 sys.exit(1) 422 422 423 423 424 - def build_handler(cli_args): 424 + def build_handler(cli_args: argparse.Namespace) -> None: 425 425 linux = tree_from_args(cli_args) 426 426 request = KunitBuildRequest(build_dir=cli_args.build_dir, 427 427 make_options=cli_args.make_options, ··· 434 434 sys.exit(1) 435 435 436 436 437 - def exec_handler(cli_args): 437 + def exec_handler(cli_args: argparse.Namespace) -> None: 438 438 linux = tree_from_args(cli_args) 439 439 exec_request = KunitExecRequest(raw_output=cli_args.raw_output, 440 440 build_dir=cli_args.build_dir, ··· 450 450 sys.exit(1) 451 451 452 452 453 - def parse_handler(cli_args): 453 + def parse_handler(cli_args: argparse.Namespace) -> None: 454 454 if cli_args.file is None: 455 - sys.stdin.reconfigure(errors='backslashreplace') # pytype: disable=attribute-error 456 - kunit_output = sys.stdin 455 + sys.stdin.reconfigure(errors='backslashreplace') # type: ignore 456 + kunit_output = sys.stdin # type: Iterable[str] 457 457 else: 458 458 with open(cli_args.file, 'r', errors='backslashreplace') as f: 459 459 kunit_output = f.read().splitlines() ··· 475 475 } 476 476 477 477 478 - def main(argv): 478 + def main(argv: Sequence[str]) -> None: 479 479 parser = argparse.ArgumentParser( 480 480 description='Helps writing and running KUnit tests.') 481 481 subparser = parser.add_subparsers(dest='subcommand')
+2 -2
tools/testing/kunit/kunit_config.py
··· 8 8 9 9 from dataclasses import dataclass 10 10 import re 11 - from typing import Dict, Iterable, List, Tuple 11 + from typing import Any, Dict, Iterable, List, Tuple 12 12 13 13 CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$' 14 14 CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$' ··· 34 34 def __init__(self) -> None: 35 35 self._entries = {} # type: Dict[str, str] 36 36 37 - def __eq__(self, other) -> bool: 37 + def __eq__(self, other: Any) -> bool: 38 38 if not isinstance(other, self.__class__): 39 39 return False 40 40 return self._entries == other._entries
+15 -14
tools/testing/kunit/kunit_kernel.py
··· 16 16 import signal 17 17 import threading 18 18 from typing import Iterator, List, Optional, Tuple 19 + from types import FrameType 19 20 20 21 import kunit_config 21 22 import qemu_config ··· 57 56 def make_arch_config(self, base_kunitconfig: kunit_config.Kconfig) -> kunit_config.Kconfig: 58 57 return base_kunitconfig 59 58 60 - def make_olddefconfig(self, build_dir: str, make_options) -> None: 59 + def make_olddefconfig(self, build_dir: str, make_options: Optional[List[str]]) -> None: 61 60 command = ['make', 'ARCH=' + self._linux_arch, 'O=' + build_dir, 'olddefconfig'] 62 61 if self._cross_compile: 63 62 command += ['CROSS_COMPILE=' + self._cross_compile] ··· 71 70 except subprocess.CalledProcessError as e: 72 71 raise ConfigError(e.output.decode()) 73 72 74 - def make(self, jobs, build_dir: str, make_options) -> None: 73 + def make(self, jobs: int, build_dir: str, make_options: Optional[List[str]]) -> None: 75 74 command = ['make', 'ARCH=' + self._linux_arch, 'O=' + build_dir, '--jobs=' + str(jobs)] 76 75 if make_options: 77 76 command.extend(make_options) ··· 133 132 class LinuxSourceTreeOperationsUml(LinuxSourceTreeOperations): 134 133 """An abstraction over command line operations performed on a source tree.""" 135 134 136 - def __init__(self, cross_compile=None): 135 + def __init__(self, cross_compile: Optional[str]=None): 137 136 super().__init__(linux_arch='um', cross_compile=cross_compile) 138 137 139 138 def make_arch_config(self, base_kunitconfig: kunit_config.Kconfig) -> kunit_config.Kconfig: ··· 216 215 217 216 if not hasattr(config, 'QEMU_ARCH'): 218 217 raise ValueError('qemu_config module missing "QEMU_ARCH": ' + config_path) 219 - params: qemu_config.QemuArchParams = config.QEMU_ARCH # type: ignore 218 + params: qemu_config.QemuArchParams = config.QEMU_ARCH 220 219 if extra_qemu_args: 221 220 params.extra_qemu_params.extend(extra_qemu_args) 222 221 return params.linux_arch, LinuxSourceTreeOperationsQemu( ··· 230 229 build_dir: str, 231 230 kunitconfig_paths: Optional[List[str]]=None, 232 231 kconfig_add: Optional[List[str]]=None, 233 - arch=None, 234 - cross_compile=None, 235 - qemu_config_path=None, 236 - extra_qemu_args=None) -> None: 232 + arch: Optional[str]=None, 233 + cross_compile: Optional[str]=None, 234 + qemu_config_path: Optional[str]=None, 235 + extra_qemu_args: Optional[List[str]]=None) -> None: 237 236 signal.signal(signal.SIGINT, self.signal_handler) 238 237 if qemu_config_path: 239 238 self._arch, self._ops = _get_qemu_ops(qemu_config_path, extra_qemu_args, cross_compile) ··· 276 275 logging.error(message) 277 276 return False 278 277 279 - def build_config(self, build_dir: str, make_options) -> bool: 278 + def build_config(self, build_dir: str, make_options: Optional[List[str]]) -> bool: 280 279 kconfig_path = get_kconfig_path(build_dir) 281 280 if build_dir and not os.path.exists(build_dir): 282 281 os.mkdir(build_dir) ··· 304 303 old_kconfig = kunit_config.parse_file(old_path) 305 304 return old_kconfig != self._kconfig 306 305 307 - def build_reconfig(self, build_dir: str, make_options) -> bool: 306 + def build_reconfig(self, build_dir: str, make_options: Optional[List[str]]) -> bool: 308 307 """Creates a new .config if it is not a subset of the .kunitconfig.""" 309 308 kconfig_path = get_kconfig_path(build_dir) 310 309 if not os.path.exists(kconfig_path): ··· 320 319 os.remove(kconfig_path) 321 320 return self.build_config(build_dir, make_options) 322 321 323 - def build_kernel(self, jobs, build_dir: str, make_options) -> bool: 322 + def build_kernel(self, jobs: int, build_dir: str, make_options: Optional[List[str]]) -> bool: 324 323 try: 325 324 self._ops.make_olddefconfig(build_dir, make_options) 326 325 self._ops.make(jobs, build_dir, make_options) ··· 329 328 return False 330 329 return self.validate_config(build_dir) 331 330 332 - def run_kernel(self, args=None, build_dir='', filter_glob='', timeout=None) -> Iterator[str]: 331 + def run_kernel(self, args: Optional[List[str]]=None, build_dir: str='', filter_glob: str='', timeout: Optional[int]=None) -> Iterator[str]: 333 332 if not args: 334 333 args = [] 335 334 if filter_glob: ··· 340 339 assert process.stdout is not None # tell mypy it's set 341 340 342 341 # Enforce the timeout in a background thread. 343 - def _wait_proc(): 342 + def _wait_proc() -> None: 344 343 try: 345 344 process.wait(timeout=timeout) 346 345 except Exception as e: ··· 366 365 waiter.join() 367 366 subprocess.call(['stty', 'sane']) 368 367 369 - def signal_handler(self, unused_sig, unused_frame) -> None: 368 + def signal_handler(self, unused_sig: int, unused_frame: Optional[FrameType]) -> None: 370 369 logging.error('Build interruption occurred. Cleaning console.') 371 370 subprocess.call(['stty', 'sane'])
+2 -2
tools/testing/kunit/run_checks.py
··· 23 23 'kunit_tool_test.py': ['./kunit_tool_test.py'], 24 24 'kunit smoke test': ['./kunit.py', 'run', '--kunitconfig=lib/kunit', '--build_dir=kunit_run_checks'], 25 25 'pytype': ['/bin/sh', '-c', 'pytype *.py'], 26 - 'mypy': ['/bin/sh', '-c', 'mypy *.py'], 26 + 'mypy': ['mypy', '--strict', '--exclude', '_test.py$', '--exclude', 'qemu_configs/', '.'], 27 27 } 28 28 29 29 # The user might not have mypy or pytype installed, skip them if so. ··· 73 73 sys.exit(1) 74 74 75 75 76 - def run_cmd(argv: Sequence[str]): 76 + def run_cmd(argv: Sequence[str]) -> None: 77 77 subprocess.check_output(argv, stderr=subprocess.STDOUT, cwd=ABS_TOOL_PATH, timeout=TIMEOUT) 78 78 79 79