-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add ok_or
and ok_or_else
methods
#9
Comments
Doing it with the extras approach makes sense for now I think. We can revisit the decision if the two become more interdependent in future, but for now I think that's fine. You're welcome to push up a PR. Thanks |
@francium, I see that the github actions in
|
To avoid cluttering up this PR, I opened a separate issue to address the outdated github actions: #10 I'll address that one first. |
Yes to all :) |
@francium, do you have any preference on how you'd like to see things coded to allow for I'm doing the following, but it seems a bit awkward, so I was wondering if you have any ideas for something better (although this does indeed do the trick): Near the top of try:
import result
RESULT_INSTALLED = True
except ImportError: # pragma: no cover
RESULT_INSTALLED = False Then within both the class Some(Generic[T]):
...
if RESULT_INSTALLED:
def ok_or(self, _error: object) -> ...
def ok_or_else(self, _op: object) -> ... |
I didn't even know you could do The only alternative I can think of, and I guess I've definitely seen this in real codebases,
However, the way you've done it in your example above with a conditional However, what does Also what about mypy/pyright/ide's/language servers? Do they hide/show those methods correctly depending on if the result package is installed? If not, then maybe we'd want to instead (a) keep imports at the top and (b) raise some kind of exception if the import isn't available to avoid confusion? @wbolster any opinions on this? |
@francium, there are a few other bits I didn't include in my earlier comment to keep the comment from being too verbose, but I have managed to get everything to work as expected (tests, linting, docs), whether Of course, contributors to this repo should have Regarding the docstrings for the new methods, how would you like to see things noted regarding the need for the Actually, let me submit the PR so you and @wbolster can see everything and make comments on the PR itself. It's probably easier to do that at this point. |
Also, yes, I've taken care of things working well in the IDE (at least in VSCode, both with mypy and pyright). |
I'd like to add
ok_or
andok_or_else
methods, following Rust's behavior for them:I realize that this means that this library would then require the rustedpy
result
library as a dependency. Although both are quite small, I'd be happy to makeresult
an "extras", if you want to allow users to avoid the extra dependency, if they're not using it.My motivation here is that I have a function that returns a
Maybe
, but there are cases where I want to convert it to aResult
. In particular, this would allow me to make use of it within aresult.do
call.For example, if function
f
returns aMaybe
, I can make use of a call tof
in aresult.do
by usingok_or
(orok_or_else
), such as in this dummy scenario:The text was updated successfully, but these errors were encountered: