Skip to content

Commit ac26744

Browse files
committed
Typed exception
1 parent 43cdd24 commit ac26744

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

dry_monads/do_notation.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
# -*- coding: utf-8 -*-
22

33
from functools import wraps
4-
from typing import TYPE_CHECKING, Callable, TypeVar, Union
4+
from typing import Callable
55

6-
from dry_monads.either import Either
76
from dry_monads.primitives.exceptions import UnwrapFailedError
8-
9-
if TYPE_CHECKING: # pragma: no cover
10-
from dry_monads.primitives.monad import Monad # noqa: Z435, F401
11-
12-
# We need to have this ugly type because there is no other way around it:
13-
_MonadType = TypeVar('_MonadType', bound=Union['Monad', Either])
7+
from dry_monads.primitives.types import MonadType
148

159

1610
def do_notation(
17-
function: Callable[..., _MonadType],
18-
) -> Callable[..., _MonadType]:
11+
function: Callable[..., MonadType],
12+
) -> Callable[..., MonadType]:
1913
"""Decorator to enable 'do-notation' context."""
2014
@wraps(function)
21-
def decorator(*args, **kwargs) -> _MonadType:
15+
def decorator(*args, **kwargs) -> MonadType:
2216
try:
2317
return function(*args, **kwargs)
2418
except UnwrapFailedError as exc:

dry_monads/primitives/exceptions.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# -*- coding: utf-8 -*-
22

3+
from typing import TYPE_CHECKING
4+
5+
if TYPE_CHECKING: # pragma: no cover
6+
from dry_monads.primitives.types import MonadType # noqa: Z435, F401
7+
38

49
class UnwrapFailedError(Exception):
510
"""Raised when a monad can not be unwrapped into a meaningful value."""
611

7-
def __init__(self, monad) -> None:
8-
"""Saves halted monad in the inner state."""
12+
def __init__(self, monad: 'MonadType') -> None:
13+
"""
14+
Saves halted monad in the inner state.
15+
16+
So, this monad can later be unpacked from this exception
17+
and used as a regular value.
18+
"""
919
super().__init__()
1020
self.halted_monad = monad

dry_monads/primitives/types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from typing import TYPE_CHECKING, TypeVar, Union
4+
5+
if TYPE_CHECKING: # pragma: no cover
6+
from dry_monads.either import Either # noqa: Z435, F401
7+
from dry_monads.primitives.monad import Monad # noqa: Z435, F401
8+
9+
# We need to have this ugly type because there is no other way around it:
10+
MonadType = TypeVar('MonadType', bound=Union['Monad', 'Either'])

tests/test_do_notation/test_do_notation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@do_notation
88
def _example1(number: int) -> Either[int, str]:
99
first = Success(1).unwrap()
10-
second = Success(number).unwrap() if number % 2 else Failure('E').unwrap()
10+
second = Success(number).unwrap() if number else Failure('E').unwrap()
1111
return Success(first + second)
1212

1313

@@ -24,5 +24,5 @@ def test_do_notation_success():
2424

2525
def test_do_notation_failure():
2626
"""Ensures that do notation works well for Failure."""
27-
assert _example1(6) == Failure('E')
27+
assert _example1(0) == Failure('E')
2828
assert _example2(0) == Failure(0)

0 commit comments

Comments
 (0)