Skip to contentSkip to conversation

scripts/container: Minor fixups suggested by ruff

PATCH In review Nathan Chancellor 4 participants 13 messages Jan 22 – Feb 4

Subsystems (2)
lore
Share & tools
lore

Nathan Chancellor

author maintainer
#0 lore raw

cover scripts/container: Minor fixups suggested by ruff

Hi all,

This series fixes a few warnings that I see when running

  $ ruff check --select C4,RUF scripts/container

which were the few warnings from my personal ruff.toml that seemed most interesting.

I will apply these on top of the initial scripts/container change.

---
Nathan Chancellor (3):
scripts/container: Turn runtimes class variable into a tuple scripts/container: Use list comprehension in get_names() scripts/container: Use iterable unpacking for _get_opts()

 scripts/container | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

---
base-commit: 6eac13c876805f61bbb588eaff5ada0b6dc603e8
change-id: 20260122-scripts-container-ruff-fixes-4d38fc92771a

Best regards,

Signature · 1 line
Nathan Chancellor <[email protected]>
author maintainer
#1 lore raw

PATCH 1/3 scripts/container: Turn runtimes class variable into a tuple

'ruff check --select RUF scripts/container' warns:

RUF012 Mutable class attributes should be annotated with typing.ClassVar

     --> scripts/container:108:16
      |
  106 |     """List of all supported runtimes"""
  107 |
  108 |     runtimes = [PodmanRuntime, DockerRuntime]
      |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  109 |
  110 |     @classmethod
      |

runtimes is never modified so move from a list to a tuple, clearing up the warning.

---
 scripts/container | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/container +1 -1
index b05333d8530b..dae475c6d262 100755
@@ -105,7 +105,7 @@ class PodmanRuntime(CommonRuntime):
 class Runtimes:
     """List of all supported runtimes"""
 
-    runtimes = [PodmanRuntime, DockerRuntime]
+    runtimes = (PodmanRuntime, DockerRuntime)
 
     @classmethod
     def get_names(cls):
Signature · 1 line
2.52.0
maintainer
#2 lore raw

Re: [PATCH 1/3] scripts/container: Turn runtimes class variable into a tuple

scripts/container class PodmanRuntime(CommonRuntime): · line 108

Quoted 2 lines · Nathan Chancellor
scripts/container +1 -1
-    runtimes = [PodmanRuntime, DockerRuntime]
+    runtimes = (PodmanRuntime, DockerRuntime)

My 2¢ - A subtle difference between a non-mutable list and a tuple, is that a list is a series of values with arbitrary length whereas a tuple is a single variable with a fixed number of values meant to be used together. Say, coordinates make sense as a tuple but not as a list. Search results from a database make sense to be stored in a list but not as a tuple. In this case, there could be any number of runtimes so it's semantically a list. At least that's why I used a list, now there may be good reasons for using a tuple too.

Cheers,
Guillaume

author maintainer
#3 lore raw

PATCH 2/3 scripts/container: Use list comprehension in get_names()

In reply to Nathan Chancellor

'ruff check --select C4 scripts/container' warns:

C400 Unnecessary generator (rewrite as a list comprehension)

     --> scripts/container:113:16
      |
  111 |     def get_names(cls):
  112 |         """Get a list of all the runtime names"""
  113 |         return list(runtime.name for runtime in cls.runtimes)
      |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  114 |
  115 |     @classmethod
      |

help: Rewrite as a list comprehension

Do as it suggests.

---
 scripts/container | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/container +1 -1
index dae475c6d262..5afaad762492 100755
@@ -110,7 +110,7 @@ class Runtimes:
     @classmethod
     def get_names(cls):
         """Get a list of all the runtime names"""
-        return list(runtime.name for runtime in cls.runtimes)
+        return [runtime.name for runtime in cls.runtimes]
 
     @classmethod
     def get(cls, name):
Signature · 1 line
2.52.0
maintainer
#4 lore raw

Re: [PATCH 2/3] scripts/container: Use list comprehension in get_names()

scripts/container class Runtimes: · line 113

Quoted 2 lines · Nathan Chancellor
scripts/container +1 -1
-        return list(runtime.name for runtime in cls.runtimes)
+        return [runtime.name for runtime in cls.runtimes]

My 2¢ - A generator is a generalisation of list comprehension, so using [] is syntactic sugar for list(). I prefer to always spell out the type explicitly with generators to avoid bad surprises with {} which could be seen either as a set() or a dict().

Cheers,
Guillaume

author maintainer
#5 lore raw

PATCH 3/3 scripts/container: Use iterable unpacking for _get_opts()

In reply to Nathan Chancellor

'ruff check --select RUF scripts/container' warns:

RUF005 Consider iterable unpacking instead of concatenation

    --> scripts/container:89:16
     |
  88 |       def _get_opts(self, container_name):
  89 |           return super()._get_opts(container_name) + [
     |  ________________^
  90 | |             '--user', f'{self._uid}:{self._gid}'
  91 | |         ]
     | |_________^
     |

help: Replace with iterable unpacking

RUF005 Consider iterable unpacking instead of concatenation

     --> scripts/container:100:16
      |
   99 |       def _get_opts(self, container_name):
  100 |           return super()._get_opts(container_name) + [
      |  ________________^
  101 | |             '--userns', f'keep-id:uid={self._uid},gid={self._gid}',
  102 | |         ]
      | |_________^
      |

help: Replace with iterable unpacking

Iterable unpacking is more efficient, so switch to it as it suggests (even if it is unlikely to matter given how small these lists are).

---
 scripts/container | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/container +4 -2
index 5afaad762492..09a14f7e29f3 100755
@@ -86,7 +86,8 @@ class DockerRuntime(CommonRuntime):
     name = 'docker'
 
     def _get_opts(self, container_name):
-        return super()._get_opts(container_name) + [
+        return [
+            *super()._get_opts(container_name),
             '--user', f'{self._uid}:{self._gid}'
         ]
 
@@ -97,7 +98,8 @@ class PodmanRuntime(CommonRuntime):
     name = 'podman'
 
     def _get_opts(self, container_name):
-        return super()._get_opts(container_name) + [
+        return [
+            *super()._get_opts(container_name),
             '--userns', f'keep-id:uid={self._uid},gid={self._gid}',
         ]
 
Signature · 1 line
2.52.0
maintainer
#6 lore raw

Re: [PATCH 3/3] scripts/container: Use iterable unpacking for _get_opts()

scripts/container class DockerRuntime(CommonRuntime): · line 92

Quoted 5 lines · Nathan Chancellor
scripts/container +2 -1
-        return super()._get_opts(container_name) + [
+        return [
+            *super()._get_opts(container_name),
              '--user', f'{self._uid}:{self._gid}'
          ]

My last 2¢ - Yes it's more efficient to build the list in one go but I find it easier to read and maintain with the + [...options...] syntax. I guess it's hard to tell a linter that sometimes, runtime performance isn't so important.

These are all minor things but they come with interesting language implications so I thought it was worth a few comments...

Cheers,
Guillaume

#7 lore raw
In reply to Nathan Chancellor
Quoted 7 lines · Nathan Chancellor

This series fixes a few warnings that I see when running

  $ ruff check --select C4,RUF scripts/container

which were the few warnings from my personal ruff.toml that seemed most interesting.

I haven't had time to look into the new container support yet, but having kernel Python scripts Ruff-clean sounds good in general, and I wonder -- should we consider having a .ruff.toml eventually?

At least we have a small .pylintrc already, which is quite recent by the way.

I think I recall you also using Black and/or Flake8, for ClangBuiltLinux-related bits perhaps?

Thanks!

Cheers,
Miguel

author maintainer
#8 lore raw
Quoted 4 lines · Miguel Ojeda
Quoted 3 lines · Nathan Chancellor

This series fixes a few warnings that I see when running

Quoted 8 lines
Quoted 4 lines
  $ ruff check --select C4,RUF scripts/container

which were the few warnings from my personal ruff.toml that seemed most interesting.

I haven't had time to look into the new container support yet, but having kernel Python scripts Ruff-clean sounds good in general, and I wonder -- should we consider having a .ruff.toml eventually?

I think that we should. Even something as simple as the one in the Ruff docs would keep things consistent:

https://docs.astral.sh/ruff/linter/#rule-selection

I guess the one problem there is consistency amongst developers (since one's linting workflow is fairly personal) and having a wide range of Python code across the tree but having a .ruff.toml for people to run if they want it is harmless.

Quoted 1 line

At least we have a small .pylintrc already, which is quite recent by the way.

Ah, good to know!

Quoted 2 lines

I think I recall you also using Black and/or Flake8, for ClangBuiltLinux-related bits perhaps?

We use Ruff and pylint for linting, yapf for formatting. Ruff can replace flake8 outright with the right configuration in my experience.

Cheers,
Nathan

#9 lore raw
Quoted 10 lines · Nathan Chancellor

I think that we should. Even something as simple as the one in the Ruff docs would keep things consistent:

https://docs.astral.sh/ruff/linter/#rule-selection

I guess the one problem there is consistency amongst developers (since one's linting workflow is fairly personal) and having a wide range of Python code across the tree but having a .ruff.toml for people to run if they want it is harmless.

Yeah, I think as long as it is optional (and the rules reasonable -- not sure how opinionated the defaults are, but we can tweak things as needed), then it is a win with little downside.

It is similar to the clang-format situation -- some developers take advantage of it, others don't.

Over time, the idea would be to try to define the set of rules that most people is happy with, and thus to get more and more files to conform.

Thanks!

Cheers,
Miguel

maintainer Reviewed-by on the v1 series
#10 lore raw
In reply to Nathan Chancellor

Hi Nathan,

Quoted 17 lines · Nathan Chancellor

Hi all,

This series fixes a few warnings that I see when running

   $ ruff check --select C4,RUF scripts/container

which were the few warnings from my personal ruff.toml that seemed most interesting.

I will apply these on top of the initial scripts/container change.

---
Nathan Chancellor (3):
scripts/container: Turn runtimes class variable into a tuple scripts/container: Use list comprehension in get_names() scripts/container: Use iterable unpacking for _get_opts()

Quoted 5 lines
  scripts/container | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)

---
base-commit: 6eac13c876805f61bbb588eaff5ada0b6dc603e8
change-id: 20260122-scripts-container-ruff-fixes-4d38fc92771a

LGTM - some of these things are arguably more a matter of individual taste than absolute best practices but it's good to have a well-defined code quality standard. I typically use Pylint, Pycodestyle and Mypy, not Ruff or Black. So for the whole series:

Cheers,
Guillaume

author maintainer
#11 lore raw
Quoted 26 lines · Guillaume Tucker

Hi Nathan,

Quoted 17 lines · Nathan Chancellor

Hi all,

This series fixes a few warnings that I see when running

   $ ruff check --select C4,RUF scripts/container

which were the few warnings from my personal ruff.toml that seemed most interesting.

I will apply these on top of the initial scripts/container change.

---
Nathan Chancellor (3):
scripts/container: Turn runtimes class variable into a tuple scripts/container: Use list comprehension in get_names() scripts/container: Use iterable unpacking for _get_opts()

Quoted 5 lines
  scripts/container | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)

---
base-commit: 6eac13c876805f61bbb588eaff5ada0b6dc603e8
change-id: 20260122-scripts-container-ruff-fixes-4d38fc92771a

Quoted 6 lines

LGTM - some of these things are arguably more a matter of individual taste than absolute best practices but it's good to have a well-defined code quality standard. I typically use Pylint, Pycodestyle and Mypy, not Ruff or Black. So for the whole series:

Thanks a lot for taking a look and providing the comments. I am going to hold off on this series for a little bit to ponder on providing a .ruff.toml file first to ensure we have a common understanding around Python linting in the kernel, especially given a lot of these lints can be opinionated as you mentioned.

Cheers,
Nathan

#12 lore raw
In reply to Nathan Chancellor
Quoted 12 lines · Nathan Chancellor

Hi all,

This series fixes a few warnings that I see when running

  $ ruff check --select C4,RUF scripts/container

which were the few warnings from my personal ruff.toml that seemed most interesting.

I will apply these on top of the initial scripts/container change.

Quoted 8 lines

---
Nathan Chancellor (3):
scripts/container: Turn runtimes class variable into a tuple scripts/container: Use list comprehension in get_names() scripts/container: Use iterable unpacking for _get_opts()

 scripts/container | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

scripts/container is written in a generic way.

In my understanding, this runs an arbitrary command in a container, and that's it. Nothing is related to Kbuild.

$ scripts/container  -i debian:trixie  echo hello
hello

If this script is really useful,
why don't you keep this outside the kernel code so that non-kernel developers can use it?

Signature · 2 lines
Best Regards
Masahiro Yamada