-
Notifications
You must be signed in to change notification settings - Fork 12
Add Result type #2721
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
Open
plajjan
wants to merge
2
commits into
main
Choose a base branch
from
result
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Result type #2721
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,71 @@ | ||
|
|
||
|
|
||
| class Result[T,E] (value): | ||
| def is_ok(self) -> bool: | ||
| raise NotImplementedError("Result.is_ok") | ||
| def is_err(self) -> bool: | ||
| return not self.is_ok() | ||
| def ok(self) -> ?T: | ||
| raise NotImplementedError("Result.ok") | ||
| def err(self) -> ?E: | ||
| raise NotImplementedError("Result.err") | ||
| def unwrap(self) -> T: | ||
| raise NotImplementedError("Result.unwrap") | ||
| def unwrap_err(self) -> E: | ||
| raise NotImplementedError("Result.unwrap_err") | ||
| def __bool__(self) -> bool: | ||
| return self.is_ok() | ||
|
|
||
| class Ok[T,E] (Result[T,E]): | ||
| value: T | ||
|
|
||
| def __init__(self, value: T) -> None: | ||
| self.value = value | ||
|
|
||
| def is_ok(self) -> bool: | ||
| return True | ||
|
|
||
| def ok(self) -> ?T: | ||
| return self.value | ||
|
|
||
| def err(self) -> ?E: | ||
| return None | ||
|
|
||
| def unwrap(self) -> T: | ||
| return self.value | ||
|
|
||
| def unwrap_err(self) -> E: | ||
| raise ValueError("Called unwrap_err on {repr(self)}") | ||
|
|
||
| def __str__(self) -> str: | ||
| return "Ok({self.value})" | ||
|
|
||
| def __repr__(self) -> str: | ||
| return "Ok({repr(self.value)})" | ||
|
|
||
| class Err[T,E] (Result[T,E]): | ||
| error: E | ||
|
|
||
| def __init__(self, error: E) -> None: | ||
| self.error = error | ||
|
|
||
| def is_ok(self) -> bool: | ||
| return False | ||
|
|
||
| def ok(self) -> ?T: | ||
| return None | ||
|
|
||
| def err(self) -> ?E: | ||
| return self.error | ||
|
|
||
| def unwrap(self) -> T: | ||
| raise ValueError("Called unwrap on {repr(self)}") | ||
|
|
||
| def unwrap_err(self) -> E: | ||
| return self.error | ||
|
|
||
| def __str__(self) -> str: | ||
| return "Err({self.error})" | ||
|
|
||
| def __repr__(self) -> str: | ||
| return "Err({repr(self.error)})" |
This file contains hidden or 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,44 @@ | ||
| import result | ||
| import testing | ||
|
|
||
| def _test_result_ok(): | ||
| ok = result.Ok(7) | ||
|
|
||
| testing.assertTrue(ok.is_ok()) | ||
| testing.assertFalse(ok.is_err()) | ||
| testing.assertTrue(bool(ok)) | ||
| testing.assertEqual(ok.ok(), 7) | ||
| testing.assertEqual(ok.err(), None) | ||
| testing.assertEqual(ok.unwrap(), 7) | ||
| testing.assertEqual(str(ok), "Ok(7)") | ||
| testing.assertEqual(repr(ok), "Ok(7)") | ||
|
|
||
|
|
||
| def _test_result_err(): | ||
| err = result.Err("boom") | ||
|
|
||
| testing.assertFalse(err.is_ok()) | ||
| testing.assertTrue(err.is_err()) | ||
| testing.assertFalse(bool(err)) | ||
| testing.assertEqual(err.ok(), None) | ||
| testing.assertEqual(err.err(), "boom") | ||
| testing.assertEqual(err.unwrap_err(), "boom") | ||
| testing.assertEqual(str(err), "Err(boom)") | ||
| testing.assertEqual(repr(err), "Err('boom')") | ||
|
|
||
|
|
||
| def _test_result_unwrap_errors(): | ||
| ok = result.Ok(7) | ||
| err = result.Err("boom") | ||
|
|
||
| try: | ||
| ok.unwrap_err() | ||
| raise testing.NotRaisesError("Ok.unwrap_err() should raise ValueError") | ||
| except ValueError: | ||
| pass | ||
|
|
||
| try: | ||
| err.unwrap() | ||
| raise testing.NotRaisesError("Err.unwrap() should raise ValueError") | ||
| except ValueError: | ||
| pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Changing
test-stdlibto depend ondistributionmakes this target pull in the full release graph (distribution1), including backend artifacts and$(DEPS)(viaDIST_BACKEND_FILES/dist/bin/actondb), before running stdlib tests. That is a behavioral regression for developers/CI jobs that only expect stdlib tests and can now fail in offline/minimal environments or become much slower; a narrower prerequisite such asdist/base(plusdist/bin/acton) would preserve the test target’s scope.Useful? React with 👍 / 👎.