[llvm] [lit] Add type hints to builtin_commands (PR #201497)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 4 08:11:45 PDT 2026


https://github.com/prasoon054 updated https://github.com/llvm/llvm-project/pull/201497

>From 569f3225361157f373a398d24e9b122c6f609a5d Mon Sep 17 00:00:00 2001
From: Prasoon Kumar <prasoonkumar054 at gmail.com>
Date: Thu, 4 Jun 2026 09:02:19 +0530
Subject: [PATCH] [lit] Add type hints to builtin_commands

Signed-off-by: Prasoon Kumar <prasoonkumar054 at gmail.com>
---
 .../builtin_commands/_launch_with_limit.py    |  3 +-
 llvm/utils/lit/lit/builtin_commands/cat.py    |  5 ++-
 llvm/utils/lit/lit/builtin_commands/diff.py   | 45 +++++++++++++------
 3 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/llvm/utils/lit/lit/builtin_commands/_launch_with_limit.py b/llvm/utils/lit/lit/builtin_commands/_launch_with_limit.py
index a9dc2595497e7..4c56353f09a3b 100644
--- a/llvm/utils/lit/lit/builtin_commands/_launch_with_limit.py
+++ b/llvm/utils/lit/lit/builtin_commands/_launch_with_limit.py
@@ -2,11 +2,12 @@
 import subprocess
 import resource
 import os
+from typing import List, NoReturn
 
 ULIMIT_ENV_VAR_PREFIX = "LIT_INTERNAL_ULIMIT_"
 
 
-def main(argv):
+def main(argv: List[str]) -> NoReturn:
     command_args = argv[1:]
     for env_var in os.environ:
         if env_var.startswith(ULIMIT_ENV_VAR_PREFIX):
diff --git a/llvm/utils/lit/lit/builtin_commands/cat.py b/llvm/utils/lit/lit/builtin_commands/cat.py
index 2797e0cbb4154..a23c777264cde 100644
--- a/llvm/utils/lit/lit/builtin_commands/cat.py
+++ b/llvm/utils/lit/lit/builtin_commands/cat.py
@@ -1,9 +1,10 @@
 import getopt
 import sys
 from io import StringIO
+from typing import List, Union
 
 
-def convertToCaretAndMNotation(data):
+def convertToCaretAndMNotation(data: Union[str, bytes]) -> bytes:
     newdata = StringIO()
     if isinstance(data, str):
         data = bytearray(data.encode())
@@ -26,7 +27,7 @@ def convertToCaretAndMNotation(data):
     return newdata.getvalue().encode()
 
 
-def main(argv):
+def main(argv: List[str]) -> None:
     arguments = argv[1:]
     short_options = "v"
     long_options = ["show-nonprinting"]
diff --git a/llvm/utils/lit/lit/builtin_commands/diff.py b/llvm/utils/lit/lit/builtin_commands/diff.py
index 5eab10fe17165..27dc80fdbc812 100644
--- a/llvm/utils/lit/lit/builtin_commands/diff.py
+++ b/llvm/utils/lit/lit/builtin_commands/diff.py
@@ -8,6 +8,11 @@
 import sys
 
 import util
+from typing import List, NoReturn, Optional, Tuple
+
+# A directory tree node: (dirname, child_trees).
+# child_trees is None for a file, [] for an empty dir, or a list of nodes.
+DirTree = Tuple[str, Optional[List["DirTree"]]]
 
 
 class DiffFlags:
@@ -25,7 +30,7 @@ class DiffFlags:
         "strip_trailing_cr",
     )
 
-    def __init__(self):
+    def __init__(self) -> None:
         self.ignore_all_space = False
         self.ignore_space_change = False
         self.ignore_matching_lines = False
@@ -36,10 +41,10 @@ def __init__(self):
         self.strip_trailing_cr = False
 
 
-def getDirTree(path, basedir=""):
+def getDirTree(path: str, basedir: str = "") -> DirTree:
     # Tree is a tuple of form (dirname, child_trees).
     # An empty dir has child_trees = [], a file has child_trees = None.
-    child_trees = []
+    child_trees: List[DirTree] = []
     for dirname, child_dirs, files in os.walk(os.path.join(basedir, path)):
         for child_dir in child_dirs:
             child_trees.append(getDirTree(child_dir, dirname))
@@ -48,8 +53,8 @@ def getDirTree(path, basedir=""):
         return path, sorted(child_trees)
 
 
-def compareTwoFiles(flags, filepaths):
-    filelines = []
+def compareTwoFiles(flags: DiffFlags, filepaths: List[str]) -> int:
+    filelines: List[List[bytes]] = []
     for file in filepaths:
         if file == "-":
             stdin_fileno = sys.stdin.fileno()
@@ -70,7 +75,9 @@ def compareTwoFiles(flags, filepaths):
             return compareTwoBinaryFiles(flags, filepaths, filelines)
 
 
-def compareTwoBinaryFiles(flags, filepaths, filelines):
+def compareTwoBinaryFiles(
+    flags: DiffFlags, filepaths: List[str], filelines: List[List[bytes]]
+) -> int:
     exitCode = 0
     diffs = difflib.diff_bytes(
         difflib.unified_diff,
@@ -87,8 +94,13 @@ def compareTwoBinaryFiles(flags, filepaths, filelines):
     return exitCode
 
 
-def compareTwoTextFiles(flags, filepaths, filelines_bin, encoding):
-    filelines = []
+def compareTwoTextFiles(
+    flags: DiffFlags,
+    filepaths: List[str],
+    filelines_bin: List[List[bytes]],
+    encoding: str,
+) -> int:
+    filelines: List[List[str]] = []
     for lines_bin in filelines_bin:
         lines = []
         for line_bin in lines_bin:
@@ -134,7 +146,7 @@ def compose2(f, g):
     return exitCode
 
 
-def printDirVsFile(dir_path, file_path):
+def printDirVsFile(dir_path: str, file_path: str) -> None:
     if os.path.getsize(file_path):
         msg = "File %s is a directory while file %s is a regular file"
     else:
@@ -142,7 +154,7 @@ def printDirVsFile(dir_path, file_path):
     sys.stdout.write(msg % (dir_path, file_path) + "\n")
 
 
-def printFileVsDir(file_path, dir_path):
+def printFileVsDir(file_path: str, dir_path: str) -> None:
     if os.path.getsize(file_path):
         msg = "File %s is a regular file while file %s is a directory"
     else:
@@ -150,11 +162,15 @@ def printFileVsDir(file_path, dir_path):
     sys.stdout.write(msg % (file_path, dir_path) + "\n")
 
 
-def printOnlyIn(basedir, path, name):
+def printOnlyIn(basedir: str, path: str, name: str) -> None:
     sys.stdout.write("Only in %s: %s\n" % (os.path.join(basedir, path), name))
 
 
-def compareDirTrees(flags, dir_trees, base_paths=["", ""]):
+def compareDirTrees(
+    flags: DiffFlags,
+    dir_trees: List[DirTree],
+    base_paths: List[str] = ["", ""],
+) -> int:
     # Dirnames of the trees are not checked, it's caller's responsibility,
     # as top-level dirnames are always different. Base paths are important
     # for doing os.walk, but we don't put it into tree's dirname in order
@@ -225,7 +241,7 @@ def compareDirTrees(flags, dir_trees, base_paths=["", ""]):
     return exitCode
 
 
-def main(argv):
+def main(argv: List[str]) -> NoReturn:
     if sys.platform == "win32":
         sys.stdout = io.TextIOWrapper(sys.stdout.buffer, newline="\n")
 
@@ -237,7 +253,8 @@ def main(argv):
         sys.exit(1)
 
     flags = DiffFlags()
-    filelines, filepaths, dir_trees = ([] for i in range(3))
+    filepaths: List[str] = []
+    dir_trees: List[DirTree] = []
     for o, a in opts:
         if o == "-w":
             flags.ignore_all_space = True



More information about the llvm-commits mailing list