-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# built-in | ||
from typing import Callable | ||
|
||
# app | ||
from .._exceptions import ReasonContractError | ||
from .._types import ExceptionType | ||
from .base import Base | ||
|
||
|
||
class Reason(Base): | ||
exception: ExceptionType = ReasonContractError | ||
|
||
def __init__(self, event: Exception, validator: Callable, *, | ||
message: str = None, exception: ExceptionType = None, debug: bool = False): | ||
""" | ||
Step 1. Set allowed exceptions list. | ||
""" | ||
self.event = event | ||
super().__init__( | ||
validator=validator, | ||
message=message, | ||
exception=exception, | ||
debug=debug, | ||
) | ||
|
||
def patched_function(self, *args, **kwargs): | ||
""" | ||
Step 3. Wrapped function calling. | ||
""" | ||
try: | ||
return self.function(*args, **kwargs) | ||
except self.event as origin: | ||
try: | ||
self.validate(*args, **kwargs) | ||
except self.exception: | ||
raise self.exception from origin | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
|
||
import deal | ||
|
||
|
||
@deal.reason(ValueError, lambda x: x == 1) | ||
def func(x): | ||
if x == 0: | ||
raise ZeroDivisionError | ||
if x == 1: | ||
raise ValueError | ||
if x == 2: | ||
raise ValueError | ||
return x | ||
|
||
|
||
def test_reason_just_works(): | ||
assert func(3) == 3 | ||
|
||
|
||
@pytest.mark.parametrize('value, exc', [ | ||
(0, ZeroDivisionError), | ||
(1, ValueError), | ||
(2, deal.ReasonContractError), | ||
]) | ||
def test_reason_exception(value, exc): | ||
with pytest.raises(exc): | ||
func(value) |