[llvm] [lit] Add type hints (PR #200708)
via llvm-commits
llvm-commits at lists.llvm.org
Sun May 31 21:19:19 PDT 2026
https://github.com/prasoon054 updated https://github.com/llvm/llvm-project/pull/200708
>From cf8a274ee7023ee84b9b9cad959c1a729c1126e8 Mon Sep 17 00:00:00 2001
From: Prasoon Kumar <prasoonkumar054 at gmail.com>
Date: Mon, 1 Jun 2026 09:09:40 +0530
Subject: [PATCH] [lit] Add type hint 1.0
Signed-off-by: Prasoon Kumar <prasoonkumar054 at gmail.com>
---
llvm/utils/lit/lit/BooleanExpression.py | 21 +++++-----
llvm/utils/lit/lit/InprocBuiltins.py | 19 ++++-----
llvm/utils/lit/lit/LitConfig.py | 50 +++++++++++++-----------
llvm/utils/lit/lit/ProgressBar.py | 15 ++++---
llvm/utils/lit/lit/ShCommands.py | 22 ++++++-----
llvm/utils/lit/lit/ShUtil.py | 33 ++++++++--------
llvm/utils/lit/lit/ShellEnvironment.py | 21 +++++-----
llvm/utils/lit/lit/Test.py | 43 +++++++++++---------
llvm/utils/lit/lit/TestTimes.py | 13 ++++--
llvm/utils/lit/lit/TestingConfig.py | 48 +++++++++++++----------
llvm/utils/lit/lit/discovery.py | 19 +++++----
llvm/utils/lit/lit/display.py | 20 ++++++----
llvm/utils/lit/lit/formats/base.py | 11 +++++-
llvm/utils/lit/lit/formats/googletest.py | 3 +-
llvm/utils/lit/lit/formats/shtest.py | 5 ++-
llvm/utils/lit/lit/main.py | 24 ++++++------
llvm/utils/lit/lit/reports.py | 8 ++--
17 files changed, 216 insertions(+), 159 deletions(-)
diff --git a/llvm/utils/lit/lit/BooleanExpression.py b/llvm/utils/lit/lit/BooleanExpression.py
index 9b9573d2f3f14..664525c69c2f3 100644
--- a/llvm/utils/lit/lit/BooleanExpression.py
+++ b/llvm/utils/lit/lit/BooleanExpression.py
@@ -1,4 +1,5 @@
import re
+from typing import Set, Union
class BooleanExpression:
@@ -26,7 +27,7 @@ class BooleanExpression:
# 'true' is true.
# All other identifiers are false.
@staticmethod
- def evaluate(string, variables):
+ def evaluate(string: str, variables: Set[str]) -> bool:
try:
parser = BooleanExpression(string, set(variables))
return parser.parseAll()
@@ -35,7 +36,7 @@ def evaluate(string, variables):
#####
- def __init__(self, string, variables):
+ def __init__(self, string: str, variables: Set[str]) -> None:
self.tokens = BooleanExpression.tokenize(string)
self.variables = variables
self.variables.add("true")
@@ -71,14 +72,14 @@ def quote(self, token):
else:
return repr(token)
- def accept(self, t):
+ def accept(self, t: str) -> bool:
if self.token == t:
self.token = next(self.tokens)
return True
else:
return False
- def expect(self, t):
+ def expect(self, t: Union[object, str]) -> None:
if self.token == t:
if self.token != BooleanExpression.END:
self.token = next(self.tokens)
@@ -88,7 +89,7 @@ def expect(self, t):
)
@staticmethod
- def isMatchExpression(token):
+ def isMatchExpression(token: str) -> bool:
if (
token is BooleanExpression.END
or token == "&&"
@@ -100,7 +101,7 @@ def isMatchExpression(token):
return False
return True
- def parseMATCH(self):
+ def parseMATCH(self) -> None:
regex = ""
for part in filter(None, re.split(r"(\{\{.+?\}\})", self.token)):
if part.startswith("{{"):
@@ -112,7 +113,7 @@ def parseMATCH(self):
self.value = any(regex.fullmatch(var) for var in self.variables)
self.token = next(self.tokens)
- def parseNOT(self):
+ def parseNOT(self) -> None:
if self.accept("!"):
self.parseNOT()
self.value = not self.value
@@ -127,7 +128,7 @@ def parseNOT(self):
else:
self.parseMATCH()
- def parseAND(self):
+ def parseAND(self) -> None:
self.parseNOT()
while self.accept("&&"):
left = self.value
@@ -137,7 +138,7 @@ def parseAND(self):
# doesn't matter for this limited expression grammar
self.value = left and right
- def parseOR(self):
+ def parseOR(self) -> None:
self.parseAND()
while self.accept("||"):
left = self.value
@@ -147,7 +148,7 @@ def parseOR(self):
# doesn't matter for this limited expression grammar
self.value = left or right
- def parseAll(self):
+ def parseAll(self) -> bool:
self.token = next(self.tokens)
self.parseOR()
self.expect(BooleanExpression.END)
diff --git a/llvm/utils/lit/lit/InprocBuiltins.py b/llvm/utils/lit/lit/InprocBuiltins.py
index b20aeb6993726..9e4338b7cefe2 100644
--- a/llvm/utils/lit/lit/InprocBuiltins.py
+++ b/llvm/utils/lit/lit/InprocBuiltins.py
@@ -9,16 +9,17 @@
import lit.util
from lit.ShellEnvironment import (
- InternalShellError,
+ ShellEnvironment, InternalShellError,
ShellCommandResult,
expand_glob_expressions,
kIsWindows,
processRedirects,
updateEnv,
)
+from lit.ShCommands import Command
-def executeBuiltinCd(cmd, shenv):
+def executeBuiltinCd(cmd: Command, shenv: ShellEnvironment) -> ShellCommandResult:
"""executeBuiltinCd - Change the current directory."""
if len(cmd.args) != 2:
raise InternalShellError(cmd, "'cd' supports only one argument")
@@ -29,7 +30,7 @@ def executeBuiltinCd(cmd, shenv):
return ShellCommandResult(cmd, "", "", 0, False)
-def executeBuiltinPushd(cmd, shenv):
+def executeBuiltinPushd(cmd: Command, shenv: ShellEnvironment) -> ShellCommandResult:
"""executeBuiltinPushd - Change the current dir and save the old."""
if len(cmd.args) != 2:
raise InternalShellError(cmd, "'pushd' supports only one argument")
@@ -38,7 +39,7 @@ def executeBuiltinPushd(cmd, shenv):
return ShellCommandResult(cmd, "", "", 0, False)
-def executeBuiltinPopd(cmd, shenv):
+def executeBuiltinPopd(cmd: Command, shenv: ShellEnvironment) -> ShellCommandResult:
"""executeBuiltinPopd - Restore a previously saved working directory."""
if len(cmd.args) != 1:
raise InternalShellError(cmd, "'popd' does not support arguments")
@@ -48,7 +49,7 @@ def executeBuiltinPopd(cmd, shenv):
return ShellCommandResult(cmd, "", "", 0, False)
-def executeBuiltinExport(cmd, shenv):
+def executeBuiltinExport(cmd: Command, shenv: ShellEnvironment) -> ShellCommandResult:
"""executeBuiltinExport - Set an environment variable."""
if len(cmd.args) != 2:
raise InternalShellError(cmd, "'export' supports only one argument")
@@ -56,7 +57,7 @@ def executeBuiltinExport(cmd, shenv):
return ShellCommandResult(cmd, "", "", 0, False)
-def executeBuiltinEcho(cmd, shenv):
+def executeBuiltinEcho(cmd: Command, shenv: ShellEnvironment) -> ShellCommandResult:
"""Interpret a redirected echo or @echo command"""
opened_files = []
stdin, stdout, stderr = processRedirects(cmd, subprocess.PIPE, shenv, opened_files)
@@ -113,7 +114,7 @@ def maybeUnescape(arg):
return ShellCommandResult(cmd, output, "", 0, False)
-def executeBuiltinMkdir(cmd, cmd_shenv):
+def executeBuiltinMkdir(cmd: Command, cmd_shenv: ShellEnvironment) -> ShellCommandResult:
"""executeBuiltinMkdir - Create new directories."""
args = expand_glob_expressions(cmd.args, cmd_shenv.cwd)[1:]
try:
@@ -149,7 +150,7 @@ def executeBuiltinMkdir(cmd, cmd_shenv):
return ShellCommandResult(cmd, "", stderr.getvalue(), exitCode, False)
-def executeBuiltinRm(cmd, cmd_shenv):
+def executeBuiltinRm(cmd: Command, cmd_shenv: ShellEnvironment) -> ShellCommandResult:
"""executeBuiltinRm - Removes (deletes) files or directories."""
args = expand_glob_expressions(cmd.args, cmd_shenv.cwd)[1:]
try:
@@ -310,6 +311,6 @@ def executeBuiltinUlimit(cmd, shenv):
return ShellCommandResult(cmd, "", "", 0, False)
-def executeBuiltinColon(cmd, cmd_shenv):
+def executeBuiltinColon(cmd: Command, cmd_shenv: ShellEnvironment) -> ShellCommandResult:
"""executeBuiltinColon - Discard arguments and exit with status 0."""
return ShellCommandResult(cmd, "", "", 0, False)
diff --git a/llvm/utils/lit/lit/LitConfig.py b/llvm/utils/lit/lit/LitConfig.py
index 331320cf8ebc8..72b98a20f1fc0 100644
--- a/llvm/utils/lit/lit/LitConfig.py
+++ b/llvm/utils/lit/lit/LitConfig.py
@@ -1,3 +1,5 @@
+from __future__ import annotations
+
import inspect
import os
import enum
@@ -9,6 +11,10 @@
import lit.TestingConfig
import lit.util
from lit.DiffUpdater import diff_test_updater
+from typing import TYPE_CHECKING, Any, Dict, List, Optional
+
+if TYPE_CHECKING:
+ from lit.cl_arguments import TestOrder
# LitConfig must be a new style class for properties to work
class LitConfig:
@@ -23,25 +29,25 @@ class LitConfig:
def __init__(
self,
- progname,
- path,
- diagnostic_level,
- useValgrind,
- valgrindLeakCheck,
- valgrindArgs,
- noExecute,
- debug,
- isWindows,
- order,
- params,
- config_prefix=None,
- maxIndividualTestTime=None,
- maxRetriesPerTest=None,
- parallelism_groups={},
- per_test_coverage=False,
- gtest_sharding=True,
- update_tests=False,
- ):
+ progname: str,
+ path: List[str],
+ diagnostic_level: str,
+ useValgrind: bool,
+ valgrindLeakCheck: bool,
+ valgrindArgs: List[str],
+ noExecute: bool,
+ debug: bool,
+ isWindows: bool,
+ order: TestOrder,
+ params: Dict[str, Any],
+ config_prefix: Optional[str] = None,
+ maxIndividualTestTime: Optional[int] = None,
+ maxRetriesPerTest: Optional[int] = None,
+ parallelism_groups: Dict[Any, Any]={},
+ per_test_coverage: bool=False,
+ gtest_sharding: bool=True,
+ update_tests: bool=False,
+ ) -> None:
# The name of the test runner.
self.progname = progname
# The items to add to the PATH environment variable.
@@ -215,7 +221,7 @@ def getToolsPath(self, dir, paths, tools):
return dir
- def _write_message(self, kind, message):
+ def _write_message(self, kind: str, message: str) -> None:
if not self.diagnostic_level_enabled(kind):
return
# Get the file/line where this message was generated.
@@ -245,14 +251,14 @@ def substitute(self, string):
"unable to find %r parameter, use '--param=%s=VALUE'" % (key, key)
)
- def diagnostic_level_enabled(self, kind):
+ def diagnostic_level_enabled(self, kind: str) -> bool:
if kind == "debug":
return self.debug
return DiagnosticLevel.create(self.diagnostic_level) >= DiagnosticLevel.create(
kind
)
- def dbg(self, message):
+ def dbg(self, message: str) -> None:
self._write_message("debug", message)
def note(self, message):
diff --git a/llvm/utils/lit/lit/ProgressBar.py b/llvm/utils/lit/lit/ProgressBar.py
index 9209fe44183d0..b099a879db767 100644
--- a/llvm/utils/lit/lit/ProgressBar.py
+++ b/llvm/utils/lit/lit/ProgressBar.py
@@ -1,4 +1,5 @@
#!/usr/bin/env python
+from __future__ import annotations
# Source: http://code.activestate.com/recipes/475116/, with
# modifications by Daniel Dunbar.
@@ -7,6 +8,8 @@
import re
import sys
import time
+from io import TextIOWrapper
+from typing import Type
def to_bytes(str):
@@ -86,7 +89,7 @@ class TerminalController:
_ANSICOLORS = "BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE".split()
- def __new__(cls, term_stream=sys.stdout):
+ def __new__(cls: Type[TerminalController], term_stream: TextIOWrapper=sys.stdout) -> "_PosixTerminalController":
if cls is TerminalController:
if sys.platform == "win32":
return super().__new__(_WindowsTerminalController)
@@ -117,7 +120,7 @@ class _PosixTerminalController(TerminalController):
HIDE_CURSOR=cinvis SHOW_CURSOR=cnorm""".split()
_COLORS = """BLACK BLUE GREEN CYAN RED MAGENTA YELLOW WHITE""".split()
- def __init__(self, term_stream=sys.stdout):
+ def __init__(self, term_stream: TextIOWrapper=sys.stdout) -> None:
try:
import curses
except:
@@ -249,11 +252,11 @@ class SimpleProgressBar:
'Header: 0.. 10.. 20.. ...'
"""
- def __init__(self, header):
+ def __init__(self, header: str) -> None:
self.header = header
self.atIndex = None
- def update(self, percent, message):
+ def update(self, percent: float, message: str) -> None:
if self.atIndex is None:
sys.stdout.write(self.header)
self.atIndex = 0
@@ -275,7 +278,7 @@ def update(self, percent, message):
sys.stdout.flush()
self.atIndex = next
- def clear(self, interrupted):
+ def clear(self, interrupted: bool) -> None:
if self.atIndex is not None and not interrupted:
sys.stdout.write("\n")
sys.stdout.flush()
@@ -297,7 +300,7 @@ class ProgressBar:
BAR = "%s${%s}[${BOLD}%s%s${NORMAL}${%s}]${NORMAL}%s"
HEADER = "${BOLD}${CYAN}%s${NORMAL}\n\n"
- def __init__(self, term, header, minOutputInterval, useETA=True):
+ def __init__(self, term: _PosixTerminalController, header: str, minOutputInterval: float, useETA: bool=True):
self.term = term
if not (self.term.CLEAR_EOL and self.term.UP and self.term.BOL):
raise ValueError(
diff --git a/llvm/utils/lit/lit/ShCommands.py b/llvm/utils/lit/lit/ShCommands.py
index 68655a41d7934..4882a2d99f1f3 100644
--- a/llvm/utils/lit/lit/ShCommands.py
+++ b/llvm/utils/lit/lit/ShCommands.py
@@ -1,12 +1,16 @@
+from __future__ import annotations
+
+from typing import Any, List, Tuple, Union
+
class Command:
- def __init__(self, args, redirects):
+ def __init__(self, args: List[Union[str, GlobItem]], redirects: List[Union[Tuple[Tuple[str], str], Any]]) -> None:
self.args = list(args)
self.redirects = list(redirects)
- def __repr__(self):
+ def __repr__(self) -> str:
return "Command(%r, %r)" % (self.args, self.redirects)
- def __eq__(self, other):
+ def __eq__(self, other: "Command") -> bool:
if not isinstance(other, Command):
return False
@@ -37,7 +41,7 @@ def toShell(self, file):
class GlobItem:
- def __init__(self, pattern):
+ def __init__(self, pattern: str) -> None:
self.pattern = pattern
def __repr__(self):
@@ -62,15 +66,15 @@ def resolve(self, cwd):
class Pipeline:
- def __init__(self, commands, negate=False, pipe_err=False):
+ def __init__(self, commands: List[Command], negate: bool=False, pipe_err: bool=False) -> None:
self.commands = commands
self.negate = negate
self.pipe_err = pipe_err
- def __repr__(self):
+ def __repr__(self) -> str:
return "Pipeline(%r, %r, %r)" % (self.commands, self.negate, self.pipe_err)
- def __eq__(self, other):
+ def __eq__(self, other: "Pipeline") -> bool:
if not isinstance(other, Pipeline):
return False
@@ -92,13 +96,13 @@ def toShell(self, file, pipefail=False):
class Seq:
- def __init__(self, lhs, op, rhs):
+ def __init__(self, lhs: Pipeline, op: str, rhs: Pipeline) -> None:
assert op in (";", "&", "||", "&&")
self.op = op
self.lhs = lhs
self.rhs = rhs
- def __repr__(self):
+ def __repr__(self) -> str:
return "Seq(%r, %r, %r)" % (self.lhs, self.op, self.rhs)
def __eq__(self, other):
diff --git a/llvm/utils/lit/lit/ShUtil.py b/llvm/utils/lit/lit/ShUtil.py
index 60ba77dae5188..458efb133b6c9 100644
--- a/llvm/utils/lit/lit/ShUtil.py
+++ b/llvm/utils/lit/lit/ShUtil.py
@@ -2,24 +2,25 @@
import lit.util
from lit.ShCommands import Command, GlobItem, Pipeline, Seq
+from typing import Iterator, Optional, Tuple, Union
class ShLexer:
- def __init__(self, data, win32Escapes=False):
+ def __init__(self, data: str, win32Escapes: bool=False) -> None:
self.data = data
self.pos = 0
self.end = len(data)
self.win32Escapes = win32Escapes
- def eat(self):
+ def eat(self) -> str:
c = self.data[self.pos]
self.pos += 1
return c
- def look(self):
+ def look(self) -> str:
return self.data[self.pos]
- def maybe_eat(self, c):
+ def maybe_eat(self, c: str) -> bool:
"""
maybe_eat(c) - Consume the character c if it is the next character,
returning True if a character was consumed."""
@@ -28,7 +29,7 @@ def maybe_eat(self, c):
return True
return False
- def lex_arg_fast(self, c):
+ def lex_arg_fast(self, c: str) -> Optional[Union[GlobItem, str]]:
# Get the leading whitespace free section.
chunk = self.data[self.pos - 1 :].split(None, 1)[0]
@@ -48,7 +49,7 @@ def lex_arg_fast(self, c):
self.pos = self.pos - 1 + len(chunk)
return GlobItem(chunk) if "*" in chunk or "?" in chunk else chunk
- def lex_arg_slow(self, c):
+ def lex_arg_slow(self, c: str) -> str:
if c in "'\"":
parts = [self.lex_arg_quoted(c)]
else:
@@ -110,7 +111,7 @@ def lex_arg_slow(self, c):
token = "".join(parts)
return GlobItem(token) if unquoted_glob_char else token
- def lex_arg_quoted(self, delim):
+ def lex_arg_quoted(self, delim: str) -> str:
parts = []
while self.pos != self.end:
c = self.eat()
@@ -155,10 +156,10 @@ def lex_arg_checked(self, c):
raise ValueError("Fast path failure: %r != %r" % (self.pos, end))
return reference
- def lex_arg(self, c):
+ def lex_arg(self, c: str) -> Union[GlobItem, str]:
return self.lex_arg_fast(c) or self.lex_arg_slow(c)
- def lex_one_token(self):
+ def lex_one_token(self) -> Union[GlobItem, Tuple[str], str]:
"""
lex_one_token - Lex a single 'sh' token."""
@@ -190,7 +191,7 @@ def lex_one_token(self):
return self.lex_arg(c)
- def lex(self):
+ def lex(self) -> Iterator[Union[str, GlobItem, Tuple[str]]]:
while self.pos != self.end:
if self.look().isspace():
self.eat()
@@ -202,23 +203,23 @@ def lex(self):
class ShParser:
- def __init__(self, data, win32Escapes=False, pipefail=False):
+ def __init__(self, data: str, win32Escapes: bool=False, pipefail: bool=False) -> None:
self.data = data
self.pipefail = pipefail
self.tokens = ShLexer(data, win32Escapes=win32Escapes).lex()
- def lex(self):
+ def lex(self) -> Optional[Union[GlobItem, str, Tuple[str]]]:
for item in self.tokens:
return item
return None
- def look(self):
+ def look(self) -> Optional[Union[GlobItem, Tuple[str], str]]:
token = self.lex()
if token is not None:
self.tokens = itertools.chain([token], self.tokens)
return token
- def parse_command(self):
+ def parse_command(self) -> Command:
tok = self.lex()
if not tok:
raise ValueError("empty command!")
@@ -253,7 +254,7 @@ def parse_command(self):
return Command(args, redirects)
- def parse_pipeline(self):
+ def parse_pipeline(self) -> Pipeline:
negate = False
commands = [self.parse_command()]
@@ -262,7 +263,7 @@ def parse_pipeline(self):
commands.append(self.parse_command())
return Pipeline(commands, negate, self.pipefail)
- def parse(self):
+ def parse(self) -> Union[Seq, Pipeline]:
lhs = self.parse_pipeline()
while self.look():
diff --git a/llvm/utils/lit/lit/ShellEnvironment.py b/llvm/utils/lit/lit/ShellEnvironment.py
index ffd58617b42f2..d12258559deed 100644
--- a/llvm/utils/lit/lit/ShellEnvironment.py
+++ b/llvm/utils/lit/lit/ShellEnvironment.py
@@ -4,7 +4,8 @@
import tempfile
import lit.util
-from lit.ShCommands import GlobItem
+from lit.ShCommands import Command, GlobItem
+from typing import Any, Dict, List, Optional, Union
kIsWindows = platform.system() == "Windows"
@@ -32,8 +33,8 @@ class ShellCommandResult:
)
def __init__(
- self, command, stdout, stderr, exitCode, timeoutReached, outputFiles=[]
- ):
+ self, command: Command, stdout: str, stderr: str, exitCode: int, timeoutReached: bool, outputFiles: List[Any]=[]
+ ) -> None:
self.command = command
self.stdout = stdout
self.stderr = stderr
@@ -61,7 +62,7 @@ class ShellEnvironment:
# https://github.com/llvm/llvm-project/issues/200531
__slots__ = ("cwd", "env", "umask", "dirStack", "ulimit", "normalize_slashes")
- def __init__(self, cwd, env, umask=-1, ulimit=None, normalize_slashes=False):
+ def __init__(self, cwd: str, env: Dict[str, str], umask: int = -1, ulimit: Optional[Dict[str, Any]] = None, normalize_slashes: bool = False) -> None:
self.cwd = cwd
self.env = dict(env)
self.umask = umask
@@ -69,7 +70,7 @@ def __init__(self, cwd, env, umask=-1, ulimit=None, normalize_slashes=False):
self.ulimit = ulimit if ulimit else {}
self.normalize_slashes = normalize_slashes
- def change_dir(self, newdir):
+ def change_dir(self, newdir: str) -> None:
if os.path.isabs(newdir):
self.cwd = newdir
else:
@@ -80,7 +81,7 @@ def change_dir(self, newdir):
# Skips the command, and parses its arguments.
# Modifies env accordingly.
# Returns copy of args without the command or its arguments.
-def updateEnv(env, args):
+def updateEnv(env: ShellEnvironment, args: List[str]) -> List[Union[Any, str]]:
arg_idx_next = len(args)
unset_next_env_var = False
for arg_idx, arg in enumerate(args[1:]):
@@ -110,7 +111,7 @@ def updateEnv(env, args):
return args[arg_idx_next:]
-def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
+def processRedirects(cmd: Command, stdin_source: int, cmd_shenv: ShellEnvironment, opened_files: List[Any]) -> List[int]:
"""Return the standard fds for cmd after applying redirects
Returns the three standard file descriptors for the new child process. Each
@@ -205,20 +206,20 @@ def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
return std_fds
-def expand_glob(arg, cwd):
+def expand_glob(arg: str, cwd: str) -> List[str]:
if isinstance(arg, GlobItem):
return sorted(arg.resolve(cwd))
return [arg]
-def expand_glob_expressions(args, cwd):
+def expand_glob_expressions(args: List[str], cwd: str) -> List[str]:
result = [args[0]]
for arg in args[1:]:
result.extend(expand_glob(arg, cwd))
return result
-def quote_windows_command(seq):
+def quote_windows_command(seq: List[str]) -> str:
r"""
Reimplement Python's private subprocess.list2cmdline for MSys compatibility
diff --git a/llvm/utils/lit/lit/Test.py b/llvm/utils/lit/lit/Test.py
index 35da7afc591c0..503e08aa74939 100644
--- a/llvm/utils/lit/lit/Test.py
+++ b/llvm/utils/lit/lit/Test.py
@@ -1,11 +1,18 @@
import abc
+from __future__ import annotations
+
import itertools
import os
from json import JSONEncoder
+from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Type, Union
from lit.BooleanExpression import BooleanExpression
from lit.TestTimes import read_test_times
+if TYPE_CHECKING:
+ from lit.LitConfig import LitConfig
+ from lit.TestingConfig import TestingConfig
+
# Test result codes.
@@ -16,14 +23,14 @@ class ResultCode:
_all_codes = []
@staticmethod
- def all_codes():
+ def all_codes() -> List[ResultCode]:
return ResultCode._all_codes
# We override __new__ and __getnewargs__ to ensure that pickling still
# provides unique ResultCode objects in any particular instance.
_instances = {}
- def __new__(cls, name, label, isFailure):
+ def __new__(cls: Type[ResultCode], name: str, label: str, isFailure: bool) -> "ResultCode":
res = cls._instances.get(name)
if res is None:
cls._instances[name] = res = super().__new__(cls)
@@ -32,7 +39,7 @@ def __new__(cls, name, label, isFailure):
def __getnewargs__(self):
return (self.name, self.label, self.isFailure)
- def __init__(self, name, label, isFailure):
+ def __init__(self, name: str, label: str, isFailure: bool) -> None:
self.name = name
self.label = label
self.isFailure = isFailure
@@ -155,13 +162,13 @@ class Result:
def __init__(
self,
- code,
- output="",
- elapsed=None,
- attempts=1,
- max_allowed_attempts=None,
- test_updater_outputs=[],
- ):
+ code: ResultCode,
+ output: str="",
+ elapsed: None=None,
+ attempts: int=1,
+ max_allowed_attempts: None=None,
+ test_updater_outputs: List[Any]=[],
+ ) -> None:
# The result code.
self.code = code
# The test output.
@@ -223,7 +230,7 @@ class TestSuite:
A test suite groups together a set of logically related tests.
"""
- def __init__(self, name, source_root, exec_root, config, lit_config=None):
+ def __init__(self, name: str, source_root: str, exec_root: str, config: TestingConfig, lit_config: Optional[LitConfig]=None) -> None:
self.name = name
self.source_root = source_root
self.exec_root = exec_root
@@ -232,10 +239,10 @@ def __init__(self, name, source_root, exec_root, config, lit_config=None):
self.test_times = read_test_times(self, lit_config)
- def getSourcePath(self, components):
+ def getSourcePath(self, components: Union[Tuple[str], Tuple[()]]) -> str:
return os.path.join(self.source_root, *components)
- def getExecPath(self, components):
+ def getExecPath(self, components: Tuple[str]) -> str:
return os.path.join(self.exec_root, *components)
@@ -243,8 +250,8 @@ class Test:
"""Test - Information on a single test instance."""
def __init__(
- self, suite, path_in_suite, config, file_path=None, gtest_json_file=None
- ):
+ self, suite: TestSuite, path_in_suite: Union[Tuple[str], Tuple[str, str]], config: TestingConfig, file_path: None=None, gtest_json_file: None=None
+ ) -> None:
self.suite = suite
self.path_in_suite = path_in_suite
self.config = config
@@ -313,14 +320,14 @@ def setResult(self, result):
result.code = XFAIL
self.result = result
- def isFailure(self):
+ def isFailure(self) -> bool:
assert self.result
return self.result.code.isFailure
- def getFullName(self):
+ def getFullName(self) -> str:
return self.suite.config.name + " :: " + "/".join(self.path_in_suite)
- def getSummaryName(self, printPathRelativeCWD):
+ def getSummaryName(self, printPathRelativeCWD: bool) -> str:
if printPathRelativeCWD:
return os.path.relpath(self.getFilePath())
return self.getFullName()
diff --git a/llvm/utils/lit/lit/TestTimes.py b/llvm/utils/lit/lit/TestTimes.py
index 02820016e1d96..07be084bbafae 100644
--- a/llvm/utils/lit/lit/TestTimes.py
+++ b/llvm/utils/lit/lit/TestTimes.py
@@ -1,14 +1,21 @@
+from __future__ import annotations
+
import os
+from typing import TYPE_CHECKING, Any, Dict, List, Optional
+
+if TYPE_CHECKING:
+ from lit.LitConfig import LitConfig
+ from lit.Test import Test, TestSuite
-def _get_test_times_path(suite):
+def _get_test_times_path(suite: TestSuite) -> str:
test_times_file = os.path.join(suite.exec_root, ".lit_test_times.txt")
if not os.path.exists(test_times_file):
test_times_file = os.path.join(suite.source_root, ".lit_test_times.txt")
return test_times_file
-def read_test_times(suite, lit_config=None):
+def read_test_times(suite: TestSuite, lit_config: Optional[LitConfig]=None) -> Dict[Any, Any]:
test_times = {}
test_times_file = _get_test_times_path(suite)
if os.path.exists(test_times_file):
@@ -33,7 +40,7 @@ def read_test_times(suite, lit_config=None):
return test_times
-def record_test_times(tests, lit_config):
+def record_test_times(tests: List[Test], lit_config: LitConfig) -> None:
times_by_suite = {}
for t in tests:
assert t.suite.test_times is None
diff --git a/llvm/utils/lit/lit/TestingConfig.py b/llvm/utils/lit/lit/TestingConfig.py
index 1508b1d71caf2..9ea93d73773a2 100644
--- a/llvm/utils/lit/lit/TestingConfig.py
+++ b/llvm/utils/lit/lit/TestingConfig.py
@@ -1,5 +1,11 @@
+from __future__ import annotations
+
import os
import sys
+from typing import TYPE_CHECKING, Any, Dict, List, Set
+
+if TYPE_CHECKING:
+ from lit.LitConfig import LitConfig
class TestingConfig:
@@ -8,7 +14,7 @@ class TestingConfig:
"""
@staticmethod
- def fromdefaults(litConfig):
+ def fromdefaults(litConfig: LitConfig) -> "TestingConfig":
"""
fromdefaults(litConfig) -> TestingConfig
@@ -128,7 +134,7 @@ def fromdefaults(litConfig):
maxIndividualTestTime=litConfig.maxIndividualTestTime,
)
- def load_from_path(self, path, litConfig):
+ def load_from_path(self, path: str, litConfig: LitConfig) -> None:
"""
load_from_path(path, litConfig)
@@ -169,24 +175,24 @@ def load_from_path(self, path, litConfig):
def __init__(
self,
- parent,
- name,
- suffixes,
- test_format,
- environment,
- substitutions,
- unsupported,
- test_exec_root,
- test_source_root,
- excludes,
- available_features,
- pipefail,
- limit_to_features=[],
- is_early=False,
- parallelism_group=None,
- standalone_tests=False,
- maxIndividualTestTime=0,
- ):
+ parent: None,
+ name: str,
+ suffixes: Set[Any],
+ test_format: None,
+ environment: Dict[str, str],
+ substitutions: List[Any],
+ unsupported: bool,
+ test_exec_root: None,
+ test_source_root: None,
+ excludes: List[Any],
+ available_features: List[Any],
+ pipefail: bool,
+ limit_to_features: List[Any]=[],
+ is_early: bool=False,
+ parallelism_group: None=None,
+ standalone_tests: bool=False,
+ maxIndividualTestTime: None=0,
+ ) -> None:
self.parent = parent
self.name = str(name)
self.suffixes = set(suffixes)
@@ -228,7 +234,7 @@ def recursiveExpansionLimit(self, value):
)
self._recursiveExpansionLimit = value
- def finish(self, litConfig):
+ def finish(self, litConfig: LitConfig) -> None:
"""finish() - Finish this config object, after loading is complete."""
self.name = str(self.name)
diff --git a/llvm/utils/lit/lit/discovery.py b/llvm/utils/lit/lit/discovery.py
index bede6300fb031..7aec537d32baf 100644
--- a/llvm/utils/lit/lit/discovery.py
+++ b/llvm/utils/lit/lit/discovery.py
@@ -8,9 +8,12 @@
from lit.TestingConfig import TestingConfig
from lit import LitConfig, Test, util
+import lit.LitConfig
+import lit.Test
+from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
-def chooseConfigFileFromDir(dir, config_names):
+def chooseConfigFileFromDir(dir: str, config_names: List[str]) -> Optional[str]:
for name in config_names:
p = os.path.join(dir, name)
if os.path.exists(p):
@@ -18,14 +21,14 @@ def chooseConfigFileFromDir(dir, config_names):
return None
-def dirContainsTestSuite(path, lit_config):
+def dirContainsTestSuite(path: str, lit_config: lit.LitConfig.LitConfig) -> Optional[str]:
cfgpath = chooseConfigFileFromDir(path, lit_config.site_config_names)
if not cfgpath:
cfgpath = chooseConfigFileFromDir(path, lit_config.config_names)
return cfgpath
-def getTestSuite(item, litConfig, cache):
+def getTestSuite(item: str, litConfig: lit.LitConfig.LitConfig, cache: Dict[str, Tuple[lit.Test.TestSuite, Tuple[()]]]) -> Tuple[lit.Test.TestSuite, Tuple[()]]:
"""getTestSuite(item, litConfig, cache) -> (suite, relative_path)
Find the test suite containing @arg item.
@@ -95,7 +98,7 @@ def search(path):
return ts, tuple(relative + tuple(components))
-def getLocalConfig(ts, path_in_suite, litConfig, cache):
+def getLocalConfig(ts: lit.Test.TestSuite, path_in_suite: Union[Tuple[()], Tuple[str]], litConfig: lit.LitConfig.LitConfig, cache: Union[Dict[Tuple[lit.Test.TestSuite, Tuple[()]], TestingConfig], Dict[Union[Tuple[lit.Test.TestSuite, Tuple[()]], Tuple[lit.Test.TestSuite, Tuple[str]]], TestingConfig]]) -> TestingConfig:
def search1(path_in_suite):
# Get the parent config.
if not path_in_suite:
@@ -128,7 +131,7 @@ def search(path_in_suite):
return search(path_in_suite)
-def getTests(path, litConfig, testSuiteCache, localConfigCache):
+def getTests(path: str, litConfig: lit.LitConfig.LitConfig, testSuiteCache: Dict[Any, Any], localConfigCache: Dict[Any, Any]) -> Tuple[lit.Test.TestSuite, Iterator[Any]]:
# Find the test suite for this input and its relative path.
ts, path_in_suite = getTestSuite(path, litConfig, testSuiteCache)
if ts is None:
@@ -147,8 +150,8 @@ def getTests(path, litConfig, testSuiteCache, localConfigCache):
def getTestsInSuite(
- ts, path_in_suite, litConfig, testSuiteCache, localConfigCache
-):
+ ts: lit.Test.TestSuite, path_in_suite: Union[Tuple[()], Tuple[str]], litConfig: lit.LitConfig.LitConfig, testSuiteCache: Dict[str, Tuple[lit.Test.TestSuite, Tuple[()]]], localConfigCache: Union[Dict[Tuple[lit.Test.TestSuite, Tuple[()]], TestingConfig], Dict[Union[Tuple[lit.Test.TestSuite, Tuple[()]], Tuple[lit.Test.TestSuite, Tuple[str]]], TestingConfig]]
+) -> Iterator[lit.Test.Test]:
# Check that the source path exists (errors here are reported by the
# caller).
source_path = ts.getSourcePath(path_in_suite)
@@ -247,7 +250,7 @@ def getTestsInSuite(
litConfig.warning("test suite %r contained no tests" % sub_ts.name)
-def find_tests_for_inputs(lit_config, inputs):
+def find_tests_for_inputs(lit_config: lit.LitConfig.LitConfig, inputs: List[str]) -> List[lit.Test.Test]:
"""
find_tests_for_inputs(lit_config, inputs) -> [Test]
diff --git a/llvm/utils/lit/lit/display.py b/llvm/utils/lit/lit/display.py
index 995efe9a174d9..ce5f927b2b6be 100644
--- a/llvm/utils/lit/lit/display.py
+++ b/llvm/utils/lit/lit/display.py
@@ -3,9 +3,13 @@
from argparse import Namespace
from lit.Test import Test
+from typing import TYPE_CHECKING, List, Optional
+if TYPE_CHECKING:
+ from lit.ProgressBar import SimpleProgressBar
-def create_display(opts, tests, total_tests, workers):
+
+def create_display(opts: Namespace, tests: List[Test], total_tests: int, workers: int) -> "Display":
if opts.print_result_after == "off" and not opts.useProgressBar:
return NopDisplay()
@@ -30,7 +34,7 @@ def create_display(opts, tests, total_tests, workers):
class ProgressPredictor:
- def __init__(self, tests):
+ def __init__(self, tests: List[Test]) -> None:
self.completed = 0
self.time_elapsed = 0.0
self.predictable_tests_remaining = 0
@@ -96,10 +100,10 @@ class Display:
def __init__(
self,
opts: Namespace,
- tests: list[Test],
- header: str | None,
- progress_bar,
- ):
+ tests: List[Test],
+ header: Optional[str],
+ progress_bar: Optional[SimpleProgressBar],
+ ) -> None:
self.opts = opts
self.num_tests = len(tests)
self.header = header
@@ -107,7 +111,7 @@ def __init__(
self.progress_bar = progress_bar
self.completed = 0
- def print_header(self):
+ def print_header(self) -> None:
if self.header:
print(self.header)
if self.progress_bar:
@@ -128,7 +132,7 @@ def update(self, test):
percent = self.progress_predictor.update(test)
self.progress_bar.update(percent, test.getFullName())
- def clear(self, interrupted):
+ def clear(self, interrupted: bool) -> None:
if self.progress_bar:
self.progress_bar.clear(interrupted)
diff --git a/llvm/utils/lit/lit/formats/base.py b/llvm/utils/lit/lit/formats/base.py
index 6e53613e7d1cb..875807aabceb8 100644
--- a/llvm/utils/lit/lit/formats/base.py
+++ b/llvm/utils/lit/lit/formats/base.py
@@ -1,8 +1,15 @@
+from __future__ import annotations
+
import os
+from typing import TYPE_CHECKING, Iterator, Tuple, Union
import lit.Test
import lit.util
+if TYPE_CHECKING:
+ from lit.LitConfig import LitConfig
+ from lit.TestingConfig import TestingConfig
+
class TestFormat:
def getTestsForPath(self, testSuite, path_in_suite, litConfig, localConfig):
@@ -21,7 +28,7 @@ def getTestsForPath(self, testSuite, path_in_suite, litConfig, localConfig):
class FileBasedTest(TestFormat):
- def getTestsForPath(self, testSuite, path_in_suite, litConfig, localConfig):
+ def getTestsForPath(self, testSuite: lit.Test.TestSuite, path_in_suite: Union[Tuple[str], Tuple[str, str]], litConfig: LitConfig, localConfig: TestingConfig) -> Iterator[ lit.Test.Test]:
"""
Expand each path in a test suite to a Lit test using that path and assuming
it is a file containing the test. File extensions excluded by the configuration
@@ -36,7 +43,7 @@ def getTestsForPath(self, testSuite, path_in_suite, litConfig, localConfig):
if any(filename.endswith(suffix) for suffix in localConfig.suffixes):
yield lit.Test.Test(testSuite, path_in_suite, localConfig)
- def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
+ def getTestsInDirectory(self, testSuite: lit.Test.TestSuite, path_in_suite: Union[Tuple[()], Tuple[str]], litConfig: LitConfig, localConfig: TestingConfig) -> Iterator[ lit.Test.Test]:
source_path = testSuite.getSourcePath(path_in_suite)
for filename in os.listdir(source_path):
filepath = os.path.join(source_path, filename)
diff --git a/llvm/utils/lit/lit/formats/googletest.py b/llvm/utils/lit/lit/formats/googletest.py
index e567cce541beb..7299a643d329b 100644
--- a/llvm/utils/lit/lit/formats/googletest.py
+++ b/llvm/utils/lit/lit/formats/googletest.py
@@ -9,6 +9,7 @@
import lit.TestRunner
import lit.util
from .base import TestFormat
+from typing import List, Tuple
kIsWindows = sys.platform in ["win32", "cygwin"]
@@ -282,7 +283,7 @@ def prepareCmd(self, cmd):
return cmd
@staticmethod
- def post_process_shard_results(selected_tests, discovered_tests):
+ def post_process_shard_results(selected_tests: List[lit.Test.Test], discovered_tests: List[lit.Test.Test]) -> Tuple[List[lit.Test.Test], List[lit.Test.Test]]:
def remove_gtest(tests):
return [t for t in tests if t.gtest_json_file is None]
diff --git a/llvm/utils/lit/lit/formats/shtest.py b/llvm/utils/lit/lit/formats/shtest.py
index 5529b75f51d4f..51a34c9e351cb 100644
--- a/llvm/utils/lit/lit/formats/shtest.py
+++ b/llvm/utils/lit/lit/formats/shtest.py
@@ -2,6 +2,7 @@
import lit.util
from .base import FileBasedTest
+from typing import Any, List
class ShTest(FileBasedTest):
@@ -17,8 +18,8 @@ class ShTest(FileBasedTest):
"""
def __init__(
- self, execute_external=False, extra_substitutions=[], preamble_commands=[]
- ):
+ self, execute_external: bool=False, extra_substitutions: List[Any]=[], preamble_commands: List[Any]=[]
+ ) -> None:
self.execute_external = execute_external
self.extra_substitutions = extra_substitutions
self.preamble_commands = preamble_commands
diff --git a/llvm/utils/lit/lit/main.py b/llvm/utils/lit/lit/main.py
index a3bd153040a69..99400b7ef5aa4 100755
--- a/llvm/utils/lit/lit/main.py
+++ b/llvm/utils/lit/lit/main.py
@@ -20,9 +20,11 @@
import lit.util
from lit.formats.googletest import GoogleTest
from lit.TestTimes import record_test_times
+from argparse import Namespace
+from typing import Any, Dict, List, Union
-def main(builtin_params={}):
+def main(builtin_params: Dict[Any, Any]={}) -> None:
opts = lit.cl_arguments.parse_args()
params = create_params(builtin_params, opts.user_params)
is_windows = platform.system() == "Windows"
@@ -157,7 +159,7 @@ def main(builtin_params={}):
sys.exit(1)
-def create_params(builtin_params, user_params):
+def create_params(builtin_params: Dict[Any, Any], user_params: List[Any]) -> Dict[Any, Any]:
def parse(p):
return p.split("=", 1) if "=" in p else (p, "")
@@ -190,7 +192,7 @@ def print_discovered(tests, show_suites, show_tests):
print(" %s" % t.getFullName())
-def determine_order(tests, order):
+def determine_order(tests: List[lit.Test.Test], order: lit.cl_arguments.TestOrder) -> None:
from lit.cl_arguments import TestOrder
enum_order = TestOrder(order)
@@ -226,7 +228,7 @@ def filter_by_shard(tests, run, shards, lit_config):
return selected_tests
-def mark_xfail(selected_tests, opts):
+def mark_xfail(selected_tests: List[lit.Test.Test], opts: Namespace) -> None:
for t in selected_tests:
test_file = os.sep.join(t.path_in_suite)
test_full_name = t.getFullName()
@@ -238,7 +240,7 @@ def mark_xfail(selected_tests, opts):
t.exclude_xfail = True
-def mark_unsupported(selected_tests, opts):
+def mark_unsupported(selected_tests: List[lit.Test.Test], opts: Namespace) -> None:
for t in selected_tests:
test_file = os.sep.join(t.path_in_suite)
test_full_name = t.getFullName()
@@ -250,14 +252,14 @@ def mark_unsupported(selected_tests, opts):
t.unsupported_not = True
-def mark_excluded(discovered_tests, selected_tests):
+def mark_excluded(discovered_tests: List[lit.Test.Test], selected_tests: List[lit.Test.Test]) -> None:
excluded_tests = set(discovered_tests) - set(selected_tests)
result = lit.Test.Result(lit.Test.EXCLUDED)
for t in excluded_tests:
t.setResult(result)
-def run_tests(tests, lit_config, opts, discovered_tests):
+def run_tests(tests: List[lit.Test.Test], lit_config: lit.LitConfig.LitConfig, opts: Namespace, discovered_tests: int) -> None:
workers = min(len(tests), opts.workers)
display = lit.display.create_display(opts, tests, discovered_tests, workers)
@@ -284,7 +286,7 @@ def run_tests(tests, lit_config, opts, discovered_tests):
sys.stderr.write("%s, skipping remaining tests\n" % error)
-def execute_in_tmp_dir(run, lit_config):
+def execute_in_tmp_dir(run: lit.run.Run, lit_config: lit.LitConfig.LitConfig) -> None:
# Create a temp directory inside the normal temp directory so that we can
# try to avoid temporary test file leaks. The user can avoid this behavior
# by setting LIT_PRESERVES_TMP in the environment, so they can easily use
@@ -323,7 +325,7 @@ def print_histogram(tests):
lit.util.printHistogram(test_times, title="Tests")
-def print_results(tests, elapsed, opts):
+def print_results(tests: List[lit.Test.Test], elapsed: float, opts: Namespace) -> None:
tests_by_code = {code: [] for code in lit.Test.ResultCode.all_codes()}
total_tests = len(tests)
for test in tests:
@@ -340,7 +342,7 @@ def print_results(tests, elapsed, opts):
print_summary(total_tests, tests_by_code, opts.terse_summary, elapsed)
-def print_group(tests, code, shown_codes, printPathRelativeCWD):
+def print_group(tests: List[Union[Any, lit.Test.Test]], code: lit.Test.ResultCode, shown_codes: List[Any], printPathRelativeCWD: bool) -> None:
if not tests:
return
if not code.isFailure and code not in shown_codes:
@@ -352,7 +354,7 @@ def print_group(tests, code, shown_codes, printPathRelativeCWD):
sys.stdout.write("\n")
-def print_summary(total_tests, tests_by_code, quiet, elapsed):
+def print_summary(total_tests: int, tests_by_code: Dict[lit.Test.ResultCode, List[Union[Any, lit.Test.Test]]], quiet: bool, elapsed: float) -> None:
if not quiet:
print("\nTesting Time: %.2fs" % elapsed)
diff --git a/llvm/utils/lit/lit/reports.py b/llvm/utils/lit/lit/reports.py
index c5b09fe81bb21..747ae918575a3 100755
--- a/llvm/utils/lit/lit/reports.py
+++ b/llvm/utils/lit/lit/reports.py
@@ -9,6 +9,8 @@
from xml.sax.saxutils import quoteattr as quo
import lit.Test
+from io import TextIOWrapper
+from typing import List
def by_suite_and_test_path(test):
@@ -18,12 +20,12 @@ def by_suite_and_test_path(test):
class Report:
- def __init__(self, output_file):
+ def __init__(self, output_file: str) -> None:
self.output_file = output_file
# Set by the option parser later.
self.use_unique_output_file_name = False
- def write_results(self, tests, elapsed):
+ def write_results(self, tests: List[lit.Test.Test], elapsed: float) -> None:
if self.use_unique_output_file_name:
filename, ext = os.path.splitext(os.path.basename(self.output_file))
fd, _ = tempfile.mkstemp(
@@ -44,7 +46,7 @@ def _write_results_to_file(self, tests, elapsed, file):
class JsonReport(Report):
- def _write_results_to_file(self, tests, elapsed, file):
+ def _write_results_to_file(self, tests: List[lit.Test.Test], elapsed: float, file: TextIOWrapper) -> None:
unexecuted_codes = {lit.Test.EXCLUDED, lit.Test.SKIPPED}
tests = [t for t in tests if t.result.code not in unexecuted_codes]
# Construct the data we will write.
More information about the llvm-commits
mailing list