Skip to content

Commit 7982391

Browse files
authored
Fix ValidationResult to raise all errors (#581)
Updated the `raise_if_invalid` method to raise an `ExceptionGroup` containing all validation errors, with a formatted message for better clarity. Addressing [this issue](#566).
1 parent 955771e commit 7982391

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/kirin/validation/validationpass.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ class ValidationSuite:
5454
Caches analysis results to avoid redundant computation when multiple
5555
validation passes depend on the same underlying analysis.
5656
57+
fail_fast: If True, stops at the first validation pass that fails.
58+
5759
Example:
5860
suite = ValidationSuite([
5961
NoCloningValidation,
6062
AnotherValidation,
6163
])
6264
result = suite.validate(my_kernel)
63-
print(result.format_errors())
65+
result.raise_if_invalid()
6466
"""
6567

6668
passes: list[type[ValidationPass]] = field(default_factory=list)
@@ -154,7 +156,7 @@ def get_frame(self, pass_name: str) -> Any:
154156
"""Get the analysis frame for a specific pass."""
155157
return self.frames.get(pass_name)
156158

157-
def format_errors(self) -> str:
159+
def _format_errors(self) -> str:
158160
"""Format all errors with their pass names."""
159161
if self.is_valid:
160162
return "\n\033[32mAll validation passes succeeded\033[0m"
@@ -177,5 +179,10 @@ def format_errors(self) -> str:
177179
def raise_if_invalid(self):
178180
"""Raise an exception if validation failed."""
179181
if not self.is_valid:
180-
first_errors = next(iter(self.errors.values()))
181-
raise first_errors[0]
182+
exceptions = []
183+
for pass_name, pass_errors in self.errors.items():
184+
for err in pass_errors:
185+
exceptions.append(err)
186+
187+
message = self._format_errors()
188+
raise ExceptionGroup(message, exceptions)

0 commit comments

Comments
 (0)