[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CABVgOSnSrsTn0_9FuFzvFXd=H8uQEaLWMnP=3Y=xeGGS=J2JtA@mail.gmail.com>
Date: Thu, 20 Jan 2022 16:29:59 +0800
From: David Gow <davidgow@...gle.com>
To: Daniel Latypov <dlatypov@...gle.com>
Cc: Brendan Higgins <brendanhiggins@...gle.com>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
KUnit Development <kunit-dev@...glegroups.com>,
"open list:KERNEL SELFTEST FRAMEWORK"
<linux-kselftest@...r.kernel.org>,
Shuah Khan <skhan@...uxfoundation.org>
Subject: Re: [PATCH 4/5] kunit: tool: drop last uses of collections.namedtuple
On Wed, Jan 19, 2022 at 3:09 AM Daniel Latypov <dlatypov@...gle.com> wrote:
>
> 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@...gle.com>
> ---
This seems sensible, and the type-annotations are definitely a good thing.
I guess I'm going to have to learn how to use @dataclass, though...
Reviewed-by: David Gow <davidgow@...gle.com>
-- David
> tools/testing/kunit/kunit_config.py | 9 +++++----
> tools/testing/kunit/qemu_config.py | 17 ++++++++++-------
> 2 files changed, 15 insertions(+), 11 deletions(-)
>
> diff --git a/tools/testing/kunit/kunit_config.py b/tools/testing/kunit/kunit_config.py
> index 677354546156..ca33e4b7bcc5 100644
> --- a/tools/testing/kunit/kunit_config.py
> +++ b/tools/testing/kunit/kunit_config.py
> @@ -6,16 +6,17 @@
> # Author: Felix Guo <felixguoxiuping@...il.com>
> # Author: Brendan Higgins <brendanhiggins@...gle.com>
>
> -import collections
> +from dataclasses import dataclass
> import re
> from typing import List, Set
>
> CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$'
> CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$'
>
> -KconfigEntryBase = collections.namedtuple('KconfigEntryBase', ['name', 'value'])
> -
> -class KconfigEntry(KconfigEntryBase):
> +@...aclass(frozen=True)
> +class KconfigEntry:
> + name: str
> + value: str
>
> def __str__(self) -> str:
> if self.value == 'n':
> diff --git a/tools/testing/kunit/qemu_config.py b/tools/testing/kunit/qemu_config.py
> index 1672f6184e95..0b6a80398ccc 100644
> --- a/tools/testing/kunit/qemu_config.py
> +++ b/tools/testing/kunit/qemu_config.py
> @@ -5,12 +5,15 @@
> # Copyright (C) 2021, Google LLC.
> # Author: Brendan Higgins <brendanhiggins@...gle.com>
>
> -from collections import namedtuple
> +from dataclasses import dataclass
> +from typing import List
>
>
> -QemuArchParams = namedtuple('QemuArchParams', ['linux_arch',
> - 'kconfig',
> - 'qemu_arch',
> - 'kernel_path',
> - 'kernel_command_line',
> - 'extra_qemu_params'])
> +@...aclass(frozen=True)
> +class QemuArchParams:
> + linux_arch: str
> + kconfig: str
> + qemu_arch: str
> + kernel_path: str
> + kernel_command_line: str
> + extra_qemu_params: List[str]
> --
> 2.34.1.703.g22d0c6ccf7-goog
>
Powered by blists - more mailing lists