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

kunit: tool: drop last uses of collections.namedtuple

Since we formally require python3.7+ since commit df4b0807ca1a
("kunit: tool: Assert the version requirement"), we can just use
@dataclasses.dataclass instead.

In kunit_config.py, we used namedtuple to create a hashable type that
had `name` and `value` fields and had to subclass it to define a custom
`__str__()`.
@datalcass lets us just define one type instead.

In qemu_config.py, we use namedtuple to allow modules to define various
parameters. Using @dataclass, we can add type-annotations for all these
fields, making our code more typesafe and making it easier for users to
figure out how to define new configs.

Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Daniel Latypov and committed by
Shuah Khan
e6f61920 89aa72cd

+15 -11
+5 -4
tools/testing/kunit/kunit_config.py
··· 6 6 # Author: Felix Guo <felixguoxiuping@gmail.com> 7 7 # Author: Brendan Higgins <brendanhiggins@google.com> 8 8 9 - import collections 9 + from dataclasses import dataclass 10 10 import re 11 11 from typing import List, Set 12 12 13 13 CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$' 14 14 CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$' 15 15 16 - KconfigEntryBase = collections.namedtuple('KconfigEntryBase', ['name', 'value']) 17 - 18 - class KconfigEntry(KconfigEntryBase): 16 + @dataclass(frozen=True) 17 + class KconfigEntry: 18 + name: str 19 + value: str 19 20 20 21 def __str__(self) -> str: 21 22 if self.value == 'n':
+10 -7
tools/testing/kunit/qemu_config.py
··· 5 5 # Copyright (C) 2021, Google LLC. 6 6 # Author: Brendan Higgins <brendanhiggins@google.com> 7 7 8 - from collections import namedtuple 8 + from dataclasses import dataclass 9 + from typing import List 9 10 10 11 11 - QemuArchParams = namedtuple('QemuArchParams', ['linux_arch', 12 - 'kconfig', 13 - 'qemu_arch', 14 - 'kernel_path', 15 - 'kernel_command_line', 16 - 'extra_qemu_params']) 12 + @dataclass(frozen=True) 13 + class QemuArchParams: 14 + linux_arch: str 15 + kconfig: str 16 + qemu_arch: str 17 + kernel_path: str 18 + kernel_command_line: str 19 + extra_qemu_params: List[str]