Skip to content

Commit

Permalink
[follow ups] add generator-based do notation
Browse files Browse the repository at this point in the history
  • Loading branch information
JLessinger committed Nov 12, 2023
1 parent 65894a3 commit 99308b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/result/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ def __init__(self, value: Any) -> None:
# If the function type check passes,
# then `e.value` will be a `Result[T, E]` type.
# Therefore, this return is respecting the signature of `wrapper`.
# Unfortunately, mypy does not infer this, so we suppress it.
return e.value # type: ignore
# Pyright infers this, but unfortunately, mypy does not.
return e.value
except TypeError as te:
raise TypeError(
"The function passed to `do` must be a generator.\n"
Expand Down
11 changes: 11 additions & 0 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,17 @@ def no_yield() -> DoResult[int, str]:
assert exp_msg in str(exc_info.value)


def test_do_always_ok() -> None:
if run_do_test():
@do
def always_ok() -> DoResult[int, str]:
_ = yield Ok(1)
return Ok(42)

result = always_ok()
assert isinstance(result, Ok), "Expected Ok, got Err"


def test_do_always_err() -> None:
if run_do_test():
@do
Expand Down

0 comments on commit 99308b8

Please sign in to comment.