From 17ef934d82d0747ded4b98cecc3166f3755e9f6e Mon Sep 17 00:00:00 2001 From: Glinte <96855131+Glinte@users.noreply.github.com> Date: Sun, 2 Nov 2025 15:57:34 +0800 Subject: [PATCH] Allow all subclasses of listed exceptions to be raised without violating validator --- deal/_runtime/_decorators.py | 4 ++-- deal/_runtime/_validators.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/deal/_runtime/_decorators.py b/deal/_runtime/_decorators.py index 099cd1d7..c04e54d1 100644 --- a/deal/_runtime/_decorators.py +++ b/deal/_runtime/_decorators.py @@ -172,10 +172,10 @@ def raises( """Decorator listing the exceptions which the function can raise. Implements [exception] contract. - If the function raises an exception not listed in the decorator, + If the function raises an exception not a subclass of the ones listed in the decorator, `RaisesContractError` will be raised. - :param exceptions: exceptions which the function can raise. + :param exceptions: exceptions which the function can raise, including their subclasses. :param message: error message for the exception raised on contract violation. No error message by default. :param exception: exception type to raise on the contract violation. diff --git a/deal/_runtime/_validators.py b/deal/_runtime/_validators.py index f2d3f4de..6a426cad 100644 --- a/deal/_runtime/_validators.py +++ b/deal/_runtime/_validators.py @@ -259,8 +259,9 @@ def _init(self, args: Args, kwargs: Kwargs, exc=None) -> None: def _validate(self, args: Args, kwargs: Kwargs, exc: Exception | None = None) -> None: assert exc is not None exc_type = type(exc) - if exc_type in self.exceptions: - return + for exception in self.exceptions: + if issubclass(exc_type, exception): + return raise self._exception() from exc_type