-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generator-based do notation (#150)
- Loading branch information
1 parent
35d6a5c
commit aa84edb
Showing
6 changed files
with
166 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ build/ | |
.mypy_cache/ | ||
venv/ | ||
/.tox/ | ||
.vscode | ||
pyrightconfig.json |
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
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,48 @@ | ||
from __future__ import annotations | ||
|
||
|
||
import pytest | ||
|
||
from result import Err, Ok, Result, do | ||
|
||
|
||
def test_result_do_general() -> None: | ||
def resx(is_suc: bool) -> Result[str, int]: | ||
return Ok("hello") if is_suc else Err(1) | ||
|
||
def resy(is_suc: bool) -> Result[bool, int]: | ||
return Ok(True) if is_suc else Err(2) | ||
|
||
def _get_output(is_suc1: bool, is_suc2: bool) -> Result[float, int]: | ||
out: Result[float, int] = do( | ||
Ok(len(x) + int(y) + 0.5) for x in resx(is_suc1) for y in resy(is_suc2) | ||
) | ||
return out | ||
|
||
assert _get_output(True, True) == Ok(6.5) | ||
assert _get_output(True, False) == Err(2) | ||
assert _get_output(False, True) == Err(1) | ||
assert _get_output(False, False) == Err(1) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_result_do_general_async() -> None: | ||
async def get_resx(is_suc: bool) -> Result[str, int]: | ||
return Ok("hello") if is_suc else Err(1) | ||
|
||
async def get_resy(is_suc: bool) -> Result[bool, int]: | ||
return Ok(True) if is_suc else Err(2) | ||
|
||
async def _get_output(is_suc1: bool, is_suc2: bool) -> Result[float, int]: | ||
resx, resy = await get_resx(is_suc1), await get_resy(is_suc2) | ||
out: Result[float, int] = do( | ||
Ok(len(x) + int(y) + 0.5) | ||
for x in resx | ||
for y in resy | ||
) | ||
return out | ||
|
||
assert await _get_output(True, True) == Ok(6.5) | ||
assert await _get_output(True, False) == Err(2) | ||
assert await _get_output(False, True) == Err(1) | ||
assert await _get_output(False, False) == Err(1) |