-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add .inspect()
and .inspect_err()
methods
#185
Conversation
+1 |
Good suggestion. Will review later today |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. Just a few small changes are needed and we can get this merged.
CHANGELOG.md
Outdated
@@ -13,6 +13,8 @@ Possible log types: | |||
|
|||
## [Unreleased] | |||
|
|||
- `[added]` Add `inspect()` and `inspect_err()` methods |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `[added]` Add `inspect()` and `inspect_err()` methods | |
- `[added]` Add `inspect()` and `inspect_err()` methods (#185) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
tests/test_result.py
Outdated
@@ -1,6 +1,6 @@ | |||
from __future__ import annotations | |||
|
|||
from typing import Callable | |||
from typing import Callable, List |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the from __future__ import annotations
already at line 1, you should be able to use list[...]
directly and no need to import List
. I'm not 100% sure, but try to make the change and see if mypy and tests pass on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure if 3.8 has already added this :)
Done
src/result/result.py
Outdated
@@ -202,6 +202,19 @@ def or_else(self, op: object) -> Ok[T]: | |||
""" | |||
return self | |||
|
|||
def inspect(self, op: Callable[[T], None]) -> Result[T, E]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change all of the op
types from returning None
to Any
, because we shouldn't unnecessarily restrict the user from using a functions that can also return values, we can just ignore any returned value.
def inspect(self, op: Callable[[T], None]) -> Result[T, E]: | |
def inspect(self, op: Callable[[T], Any]) -> Result[T, E]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, nice take!
tests/test_result.py
Outdated
@@ -213,6 +213,26 @@ def test_and_then() -> None: | |||
assert Err(3).and_then(sq_lambda).and_then(sq_lambda).err() == 3 | |||
|
|||
|
|||
def test_inspect() -> None: | |||
oks: List[int] = [] | |||
assert Ok(2).inspect(lambda x: oks.append(x)) == Ok(2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add lines to test using a regular function as well, you'll discover the issue of None
vs Any
as I mentioned in my earlier comment,
# Move to bottom of this file along side the other similar functions
def concat_str(x: str) -> str:
return x + x
def test_inspect() -> None:
Err("e").inspect_err(concat_str)
Err("e").inspect(concat_str)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a test with a regular function
tests/test_result.py
Outdated
@@ -213,6 +213,26 @@ def test_and_then() -> None: | |||
assert Err(3).and_then(sq_lambda).and_then(sq_lambda).err() == 3 | |||
|
|||
|
|||
def test_inspect() -> None: | |||
oks: List[int] = [] | |||
assert Ok(2).inspect(lambda x: oks.append(x)) == Ok(2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can't be moved to the bottom of the file since every fn is a closure there. We can't test a pure function, because a pure function in inspect*
methods is no-op.
But I type-hinted them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. My mistake
.inspect()
and .inspect_err()
methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. Looks great.
Version 0.17.0 published with this included. |
Hello! In Rust 1.76
inspect
andinspect_err
methods were stabilized what are very handy when an error (or a value) must be logged. So I implemented them here.Instead of doing this:
You can now do that: