@@ -64,10 +64,10 @@ def get_user_by_email(email: str) -> Result[User, str]:
64
64
return Ok(user)
65
65
66
66
user_result = get_user_by_email(email)
67
- if isinstance (user_result, Ok ): # or `is_ok (user_result)`
67
+ if is_ok (user_result): # or `isinstance (user_result, Ok )`
68
68
# type(user_result.ok_value) == User
69
69
do_something(user_result.ok_value)
70
- else : # or `elif is_err(user_result)`
70
+ else :
71
71
# type(user_result.err_value) == str
72
72
raise RuntimeError (' Could not fetch user: %s ' % user_result.err_value)
73
73
```
@@ -97,12 +97,9 @@ for a, b in values:
97
97
98
98
Not all methods
99
99
(< https://doc.rust-lang.org/std/result/enum.Result.html > ) have been
100
- implemented, only the ones that make sense in the Python context. By
101
- using ` isinstance ` to check for ` Ok ` or ` Err ` you get type safe access
102
- to the contained value when using [ MyPy] ( https://mypy.readthedocs.io/ )
103
- to typecheck your code. All of this in a package allowing easier
104
- handling of values that can be OK or not, without resorting to custom
105
- exceptions.
100
+ implemented, only the ones that make sense in the Python context.
101
+ All of this in a package allowing easier handling of values that can
102
+ be OK or not, without resorting to custom exceptions.
106
103
107
104
## API
108
105
@@ -119,20 +116,19 @@ Creating an instance:
119
116
120
117
Checking whether a result is ` Ok ` or ` Err ` . You can either use ` is_ok `
121
118
and ` is_err ` type guard ** functions** or ` isinstance ` . This way you get
122
- type safe access that can be checked with MyPy. The ` is_ok() ` or
123
- ` is_err() ` ** methods** can be used if you don't need the type safety
124
- with MyPy:
119
+ type safe access that can be checked with MyPy (compared to the ` is_ok `
120
+ and ` is_err ` ** methods** ).
125
121
126
122
``` python
127
123
>> > res = Ok(' yay' )
128
- >> > isinstance (res, Ok)
129
- True
130
124
>> > is_ok(res)
131
125
True
132
- >> > isinstance (res, Err )
133
- False
126
+ >> > isinstance (res, Ok )
127
+ True
134
128
>> > is_err(res)
135
129
False
130
+ >> > isinstance (res, Err)
131
+ False
136
132
>> > res.is_ok()
137
133
True
138
134
>> > res.is_err()
0 commit comments