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