From 3fe151f5aefc2eadc36bf88789ed0a92e9ca236b Mon Sep 17 00:00:00 2001 From: Dennis Liew Date: Thu, 20 Nov 2025 16:40:23 -0500 Subject: [PATCH] Due to Python 3.10 not having ExceptionGroup, added ValidationErrorGroup for handling multiple validation errors --- src/kirin/ir/exception.py | 8 ++++++++ src/kirin/validation/validationpass.py | 9 ++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/kirin/ir/exception.py b/src/kirin/ir/exception.py index 5044f84eb..2659e3e9f 100644 --- a/src/kirin/ir/exception.py +++ b/src/kirin/ir/exception.py @@ -80,3 +80,11 @@ class DefiniteValidationError(ValidationError): """Indicates a definite violation that will occur at runtime.""" pass + + +class ValidationErrorGroup(BaseException): + """Container for multiple validation errors (Python 3.10+ compatible).""" + + def __init__(self, message: str, errors: list[ValidationError]) -> None: + super().__init__(message) + self.errors = errors diff --git a/src/kirin/validation/validationpass.py b/src/kirin/validation/validationpass.py index 17671cda7..8bd743f98 100644 --- a/src/kirin/validation/validationpass.py +++ b/src/kirin/validation/validationpass.py @@ -3,7 +3,7 @@ from dataclasses import field, dataclass from kirin import ir -from kirin.ir.exception import ValidationError +from kirin.ir.exception import ValidationError, ValidationErrorGroup T = TypeVar("T") @@ -180,9 +180,8 @@ def raise_if_invalid(self): """Raise an exception if validation failed.""" if not self.is_valid: exceptions = [] - for pass_name, pass_errors in self.errors.items(): - for err in pass_errors: - exceptions.append(err) + for _, pass_errors in self.errors.items(): + exceptions.extend(pass_errors) message = self._format_errors() - raise ExceptionGroup(message, exceptions) + raise ValidationErrorGroup(message, errors=exceptions)