Re: [PATCH v2] scripts/show_delta: reformat code

From: Miguel Ojeda
Date: Fri Dec 01 2023 - 10:33:07 EST


On Fri, Dec 1, 2023 at 3:49 PM Hu Haowen <2023002089@xxxxxxxxxxxxxxxx> wrote:
>
> Just got a glimpse on the usage of Black and realized the convenience
> it provides and strictness of code style it supplies. It is pretty
> feasible for code style analysis series of Python scripts within the
> kernel source.
>
> However, here comes the issue that this tool binds itself to its own
> bunches of rules how the code should be formatted by default, resulting
> in some kind of scenes which do not match what we want when doing kernel
> programming, or more exactly this tool may not follow the rules regulated
> by the kernel developers or mentioned within kernel documentation,
> which means we are obliged to conduct a programming standard for Python
> coding within kernel source internally, and then ask Black to review and
> reformat the code accordingly. But this programming standard is absent
> currently, consequently it should be specified initially from my
> perspective. What is your idea on this?

This is essentially the same problem we have for C.

For C, what I did was try to find an initial "common enough" style and
document the tool in `Documentation/process/clang-format.rst` so that
maintainers could start to use the tool easily if they so wished, at
their own pace. In other words, the benefit was just having the style
around. Then, maybe, after some years, when the tool is good enough
and maintainers are on board, we can start to think about
`clang-format`ing the kernel.

Now, for Python, we have orders of magnitude less code, so perhaps
using the default options of whatever tool is a possibility. In any
case, it would be a matter of exploring the tools, asking for
feedback, documenting the choice made in `Documentation/`, providing
an example patch formatting one of the existing scripts, etc. The main
benefit would be having decided on a particular approach. I would
still avoid sending tree-wide formatting of all scripts until
maintainers of those scripts agree.

I would also recommend taking the chance to also look at linting and
not just formatting, especially given tools like Ruff provide both.
And if you happen to find an actual issue in an existing script thanks
to the linting, then that would be great and allows you to showcase
their usefulness (and maintainers are probably more likely to welcome
series like that vs. just formatting :)

Hope that helps!

Cheers,
Miguel