-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split result type into Ok and Err classes (#27)
This allows for much better type safety in code that uses results, since code can now use `isinstance` as a replacement for matching on the type of a result. See #17 for details. A migration guide has been provided. Fixes #17, replaces #18. Co-authored-by: Emerentius <emerentius@arcor.de> Co-authored-by: francium <francium@users.noreply.github.com> Co-authored-by: Danilo Bargen <mail@dbrgn.ch>
- Loading branch information
1 parent
25a3507
commit 1dd8dd4
Showing
7 changed files
with
302 additions
and
185 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
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,60 @@ | ||
# Migration guides | ||
|
||
## 0.5 -> 0.6 migration | ||
|
||
The 0.6 migration includes two breaking changes and some useful new functionality: | ||
|
||
1\. The `Result.Ok()` and `Result.Err()` class methods have been removed. | ||
These should be replaced by direct use of `Ok()` and `Err()`. As an example, the following code: | ||
|
||
```python | ||
from result import Result | ||
res1 = Result.Ok('yay') | ||
res2 = Result.Err('nay') | ||
``` | ||
|
||
should be replaced by: | ||
|
||
```python | ||
from result import Ok, Err | ||
res1 = Ok('yay') | ||
res2 = Err('nay') | ||
``` | ||
|
||
2\. Result is now a Union type between `Ok[T]` and `Err[E]`. As such, you cannot use `isinstance(res, Result)` anymore. | ||
These should be replaced by `isinstance(res, Result)`. As an example, the following code: | ||
|
||
```python | ||
from result import Ok, Result | ||
res = Ok('yay') | ||
if isinstance(res, Result): | ||
print("Result type!") | ||
``` | ||
|
||
should be replaced with: | ||
|
||
```python | ||
from result import Ok, OkErr | ||
res = Ok('yay') | ||
if isinstance(res, OkErr): | ||
print("Result type!") | ||
``` | ||
|
||
3\. Because `Result` is now a union type MyPy can statically check the Result type. | ||
In previous versions MyPy saw the following types: | ||
|
||
```python | ||
r: Result[int, str] = Ok(2) | ||
if r.is_ok(): | ||
reveal_type(r.value) # returns Union[int, str] | ||
``` | ||
|
||
but now, by using `isinstance`: | ||
|
||
```python | ||
r: Result[int, str] = Ok(2) # == Union[Ok[int], Err[str]] | ||
if isinstance(r, Ok): | ||
reveal_type(r.value) # returns int | ||
``` | ||
|
||
This allows for better type checking, but requires the use of `isinstance` instead of `is_ok()` or `is_err()`. |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .result import Result, Ok, Err, UnwrapError | ||
__all__ = ['Result', 'Ok', 'Err', 'UnwrapError'] | ||
from .result import Result, Ok, Err, UnwrapError, OkErr | ||
__all__ = ['Result', 'Ok', 'Err', 'UnwrapError', 'OkErr'] |
Oops, something went wrong.