public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scripts: generate_rust_analyzer.py: reduce import noise
@ 2026-04-27 14:23 Tamir Duberstein
  2026-04-27 14:38 ` Link Mauve
  0 siblings, 1 reply; 3+ messages in thread
From: Tamir Duberstein @ 2026-04-27 14:23 UTC (permalink / raw)
  To: Jesung Yang, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich
  Cc: rust-for-linux, linux-kernel, Tamir Duberstein

Use `import typing as t` to avoid having to list each imported symbol
whenever it is added.

Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
 scripts/generate_rust_analyzer.py | 68 +++++++++++++++++++--------------------
 1 file changed, 34 insertions(+), 34 deletions(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index d5f9a0ca742c..c61e76cb40a1 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -10,15 +10,15 @@ import os
 import pathlib
 import subprocess
 import sys
-from typing import Dict, Iterable, List, Literal, Optional, TypedDict
+import typing as t
 
-def invoke_rustc(args: List[str]) -> str:
+def invoke_rustc(args: t.List[str]) -> str:
     return subprocess.check_output(
         [os.environ["RUSTC"]] + args,
         stdin=subprocess.DEVNULL,
     ).decode('utf-8').strip()
 
-def args_crates_cfgs(cfgs: List[str]) -> Dict[str, List[str]]:
+def args_crates_cfgs(cfgs: t.List[str]) -> t.Dict[str, t.List[str]]:
     crates_cfgs = {}
     for cfg in cfgs:
         crate, vals = cfg.split("=", 1)
@@ -26,28 +26,28 @@ def args_crates_cfgs(cfgs: List[str]) -> Dict[str, List[str]]:
 
     return crates_cfgs
 
-class Dependency(TypedDict):
+class Dependency(t.TypedDict):
     crate: int
     name: str
 
 
-class Source(TypedDict):
-    include_dirs: List[str]
-    exclude_dirs: List[str]
+class Source(t.TypedDict):
+    include_dirs: t.List[str]
+    exclude_dirs: t.List[str]
 
 
-class Crate(TypedDict):
+class Crate(t.TypedDict):
     display_name: str
     root_module: str
     is_workspace_member: bool
-    deps: List[Dependency]
-    cfg: List[str]
+    deps: t.List[Dependency]
+    cfg: t.List[str]
     edition: str
-    env: Dict[str, str]
+    env: t.Dict[str, str]
 
 
 class ProcMacroCrate(Crate):
-    is_proc_macro: Literal[True]
+    is_proc_macro: t.Literal[True]
     proc_macro_dylib_path: str  # `pathlib.Path` is not JSON serializable.
 
 
@@ -59,10 +59,10 @@ def generate_crates(
     srctree: pathlib.Path,
     objtree: pathlib.Path,
     sysroot_src: pathlib.Path,
-    external_src: Optional[pathlib.Path],
-    cfgs: List[str],
+    external_src: t.Optional[pathlib.Path],
+    cfgs: t.List[str],
     core_edition: str,
-) -> List[Crate]:
+) -> t.List[Crate]:
     # Generate the configuration list.
     generated_cfg = []
     with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
@@ -72,7 +72,7 @@ def generate_crates(
             generated_cfg.append(line)
 
     # Now fill the crates list.
-    crates: List[Crate] = []
+    crates: t.List[Crate] = []
     crates_cfgs = args_crates_cfgs(cfgs)
 
     def get_crate_name(path: pathlib.Path) -> str:
@@ -81,11 +81,11 @@ def generate_crates(
     def build_crate(
         display_name: str,
         root_module: pathlib.Path,
-        deps: List[Dependency],
+        deps: t.List[Dependency],
         *,
-        cfg: Optional[List[str]],
-        is_workspace_member: Optional[bool],
-        edition: Optional[str],
+        cfg: t.Optional[t.List[str]],
+        is_workspace_member: t.Optional[bool],
+        edition: t.Optional[str],
     ) -> Crate:
         cfg = cfg if cfg is not None else crates_cfgs.get(display_name, [])
         is_workspace_member = (
@@ -107,11 +107,11 @@ def generate_crates(
     def append_proc_macro_crate(
         display_name: str,
         root_module: pathlib.Path,
-        deps: List[Dependency],
+        deps: t.List[Dependency],
         *,
-        cfg: Optional[List[str]] = None,
-        is_workspace_member: Optional[bool] = None,
-        edition: Optional[str] = None,
+        cfg: t.Optional[t.List[str]] = None,
+        is_workspace_member: t.Optional[bool] = None,
+        edition: t.Optional[str] = None,
     ) -> Dependency:
         crate = build_crate(
             display_name,
@@ -145,11 +145,11 @@ def generate_crates(
     def append_crate(
         display_name: str,
         root_module: pathlib.Path,
-        deps: List[Dependency],
+        deps: t.List[Dependency],
         *,
-        cfg: Optional[List[str]] = None,
-        is_workspace_member: Optional[bool] = None,
-        edition: Optional[str] = None,
+        cfg: t.Optional[t.List[str]] = None,
+        is_workspace_member: t.Optional[bool] = None,
+        edition: t.Optional[str] = None,
     ) -> Dependency:
         return register_crate(
             build_crate(
@@ -164,9 +164,9 @@ def generate_crates(
 
     def append_sysroot_crate(
         display_name: str,
-        deps: List[Dependency],
+        deps: t.List[Dependency],
         *,
-        cfg: Optional[List[str]] = None,
+        cfg: t.Optional[t.List[str]] = None,
     ) -> Dependency:
         return append_crate(
             display_name,
@@ -266,7 +266,7 @@ def generate_crates(
 
     def append_crate_with_generated(
         display_name: str,
-        deps: List[Dependency],
+        deps: t.List[Dependency],
     ) -> Dependency:
         crate = build_crate(
             display_name,
@@ -317,7 +317,7 @@ def generate_crates(
     # Then, the rest outside of `rust/`.
     #
     # We explicitly mention the top-level folders we want to cover.
-    extra_dirs: Iterable[pathlib.Path] = (
+    extra_dirs: t.Iterable[pathlib.Path] = (
         srctree / dir for dir in ("samples", "drivers")
     )
     if external_src is not None:
@@ -356,12 +356,12 @@ def main() -> None:
 
     class Args(argparse.Namespace):
         verbose: bool
-        cfgs: List[str]
+        cfgs: t.List[str]
         srctree: pathlib.Path
         objtree: pathlib.Path
         sysroot: pathlib.Path
         sysroot_src: pathlib.Path
-        exttree: Optional[pathlib.Path]
+        exttree: t.Optional[pathlib.Path]
         core_edition: str
 
     args = parser.parse_args(namespace=Args())

---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260427-rust-analyzer-import-typing-as-t-6388452ba855

Best regards,
--  
Tamir Duberstein <tamird@kernel.org>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] scripts: generate_rust_analyzer.py: reduce import noise
  2026-04-27 14:23 [PATCH] scripts: generate_rust_analyzer.py: reduce import noise Tamir Duberstein
@ 2026-04-27 14:38 ` Link Mauve
  2026-04-27 14:51   ` Tamir Duberstein
  0 siblings, 1 reply; 3+ messages in thread
From: Link Mauve @ 2026-04-27 14:38 UTC (permalink / raw)
  To: Tamir Duberstein
  Cc: Jesung Yang, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel

Hi Tamir,

On Mon, Apr 27, 2026 at 10:23:44AM -0400, Tamir Duberstein wrote:
> Use `import typing as t` to avoid having to list each imported symbol
> whenever it is added.

This isn’t really idiomatic Python though, every other Python project
I’ve worked on listed all types they import from typing the way this was
done previously.

I think the reasoning is that it’s more important to keep the location
directly in the code more readable, at the cost of making the import
lines slightly longer.

But if this is a regular kernel practice, please disregard my comment!

> 
> Signed-off-by: Tamir Duberstein <tamird@kernel.org>
> ---
>  scripts/generate_rust_analyzer.py | 68 +++++++++++++++++++--------------------
>  1 file changed, 34 insertions(+), 34 deletions(-)
> 
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index d5f9a0ca742c..c61e76cb40a1 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -10,15 +10,15 @@ import os
>  import pathlib
>  import subprocess
>  import sys
> -from typing import Dict, Iterable, List, Literal, Optional, TypedDict
> +import typing as t
>  
> -def invoke_rustc(args: List[str]) -> str:
> +def invoke_rustc(args: t.List[str]) -> str:
>      return subprocess.check_output(
>          [os.environ["RUSTC"]] + args,
>          stdin=subprocess.DEVNULL,
>      ).decode('utf-8').strip()
>  
> -def args_crates_cfgs(cfgs: List[str]) -> Dict[str, List[str]]:
> +def args_crates_cfgs(cfgs: t.List[str]) -> t.Dict[str, t.List[str]]:
>      crates_cfgs = {}
>      for cfg in cfgs:
>          crate, vals = cfg.split("=", 1)
> @@ -26,28 +26,28 @@ def args_crates_cfgs(cfgs: List[str]) -> Dict[str, List[str]]:
>  
>      return crates_cfgs
>  
> -class Dependency(TypedDict):
> +class Dependency(t.TypedDict):
>      crate: int
>      name: str
>  
>  
> -class Source(TypedDict):
> -    include_dirs: List[str]
> -    exclude_dirs: List[str]
> +class Source(t.TypedDict):
> +    include_dirs: t.List[str]
> +    exclude_dirs: t.List[str]
>  
>  
> -class Crate(TypedDict):
> +class Crate(t.TypedDict):
>      display_name: str
>      root_module: str
>      is_workspace_member: bool
> -    deps: List[Dependency]
> -    cfg: List[str]
> +    deps: t.List[Dependency]
> +    cfg: t.List[str]
>      edition: str
> -    env: Dict[str, str]
> +    env: t.Dict[str, str]
>  
>  
>  class ProcMacroCrate(Crate):
> -    is_proc_macro: Literal[True]
> +    is_proc_macro: t.Literal[True]
>      proc_macro_dylib_path: str  # `pathlib.Path` is not JSON serializable.
>  
>  
> @@ -59,10 +59,10 @@ def generate_crates(
>      srctree: pathlib.Path,
>      objtree: pathlib.Path,
>      sysroot_src: pathlib.Path,
> -    external_src: Optional[pathlib.Path],
> -    cfgs: List[str],
> +    external_src: t.Optional[pathlib.Path],
> +    cfgs: t.List[str],
>      core_edition: str,
> -) -> List[Crate]:
> +) -> t.List[Crate]:
>      # Generate the configuration list.
>      generated_cfg = []
>      with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
> @@ -72,7 +72,7 @@ def generate_crates(
>              generated_cfg.append(line)
>  
>      # Now fill the crates list.
> -    crates: List[Crate] = []
> +    crates: t.List[Crate] = []
>      crates_cfgs = args_crates_cfgs(cfgs)
>  
>      def get_crate_name(path: pathlib.Path) -> str:
> @@ -81,11 +81,11 @@ def generate_crates(
>      def build_crate(
>          display_name: str,
>          root_module: pathlib.Path,
> -        deps: List[Dependency],
> +        deps: t.List[Dependency],
>          *,
> -        cfg: Optional[List[str]],
> -        is_workspace_member: Optional[bool],
> -        edition: Optional[str],
> +        cfg: t.Optional[t.List[str]],
> +        is_workspace_member: t.Optional[bool],
> +        edition: t.Optional[str],
>      ) -> Crate:
>          cfg = cfg if cfg is not None else crates_cfgs.get(display_name, [])
>          is_workspace_member = (
> @@ -107,11 +107,11 @@ def generate_crates(
>      def append_proc_macro_crate(
>          display_name: str,
>          root_module: pathlib.Path,
> -        deps: List[Dependency],
> +        deps: t.List[Dependency],
>          *,
> -        cfg: Optional[List[str]] = None,
> -        is_workspace_member: Optional[bool] = None,
> -        edition: Optional[str] = None,
> +        cfg: t.Optional[t.List[str]] = None,
> +        is_workspace_member: t.Optional[bool] = None,
> +        edition: t.Optional[str] = None,
>      ) -> Dependency:
>          crate = build_crate(
>              display_name,
> @@ -145,11 +145,11 @@ def generate_crates(
>      def append_crate(
>          display_name: str,
>          root_module: pathlib.Path,
> -        deps: List[Dependency],
> +        deps: t.List[Dependency],
>          *,
> -        cfg: Optional[List[str]] = None,
> -        is_workspace_member: Optional[bool] = None,
> -        edition: Optional[str] = None,
> +        cfg: t.Optional[t.List[str]] = None,
> +        is_workspace_member: t.Optional[bool] = None,
> +        edition: t.Optional[str] = None,
>      ) -> Dependency:
>          return register_crate(
>              build_crate(
> @@ -164,9 +164,9 @@ def generate_crates(
>  
>      def append_sysroot_crate(
>          display_name: str,
> -        deps: List[Dependency],
> +        deps: t.List[Dependency],
>          *,
> -        cfg: Optional[List[str]] = None,
> +        cfg: t.Optional[t.List[str]] = None,
>      ) -> Dependency:
>          return append_crate(
>              display_name,
> @@ -266,7 +266,7 @@ def generate_crates(
>  
>      def append_crate_with_generated(
>          display_name: str,
> -        deps: List[Dependency],
> +        deps: t.List[Dependency],
>      ) -> Dependency:
>          crate = build_crate(
>              display_name,
> @@ -317,7 +317,7 @@ def generate_crates(
>      # Then, the rest outside of `rust/`.
>      #
>      # We explicitly mention the top-level folders we want to cover.
> -    extra_dirs: Iterable[pathlib.Path] = (
> +    extra_dirs: t.Iterable[pathlib.Path] = (
>          srctree / dir for dir in ("samples", "drivers")
>      )
>      if external_src is not None:
> @@ -356,12 +356,12 @@ def main() -> None:
>  
>      class Args(argparse.Namespace):
>          verbose: bool
> -        cfgs: List[str]
> +        cfgs: t.List[str]
>          srctree: pathlib.Path
>          objtree: pathlib.Path
>          sysroot: pathlib.Path
>          sysroot_src: pathlib.Path
> -        exttree: Optional[pathlib.Path]
> +        exttree: t.Optional[pathlib.Path]
>          core_edition: str
>  
>      args = parser.parse_args(namespace=Args())
> 
> ---
> base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
> change-id: 20260427-rust-analyzer-import-typing-as-t-6388452ba855
> 
> Best regards,
> --  
> Tamir Duberstein <tamird@kernel.org>
> 
> 

-- 
Link Mauve

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] scripts: generate_rust_analyzer.py: reduce import noise
  2026-04-27 14:38 ` Link Mauve
@ 2026-04-27 14:51   ` Tamir Duberstein
  0 siblings, 0 replies; 3+ messages in thread
From: Tamir Duberstein @ 2026-04-27 14:51 UTC (permalink / raw)
  To: Link Mauve
  Cc: Jesung Yang, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel

On Mon, Apr 27, 2026 at 10:38 AM Link Mauve <linkmauve@linkmauve.fr> wrote:
>
> Hi Tamir,
>
> On Mon, Apr 27, 2026 at 10:23:44AM -0400, Tamir Duberstein wrote:
> > Use `import typing as t` to avoid having to list each imported symbol
> > whenever it is added.
>
> This isn’t really idiomatic Python though, every other Python project
> I’ve worked on listed all types they import from typing the way this was
> done previously.
>
> I think the reasoning is that it’s more important to keep the location
> directly in the code more readable, at the cost of making the import
> lines slightly longer.
>
> But if this is a regular kernel practice, please disregard my comment!

Thanks for taking a look. I'm not aware of regular kernel practice in
this area. This patch is motivated by the discussion in
https://lore.kernel.org/all/CAJ-ks9=p3i5xbnDojJ7QopmUanec0ZPZ_x3_zD0Csyad5L7FEA@mail.gmail.com/
where symbols used from `typing` may become conditional, which would
complicate declaring all the imports at the top.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-27 14:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-27 14:23 [PATCH] scripts: generate_rust_analyzer.py: reduce import noise Tamir Duberstein
2026-04-27 14:38 ` Link Mauve
2026-04-27 14:51   ` Tamir Duberstein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox