Skip to content

Commit 2fb4116

Browse files
committed
Update README.md
1 parent 430f073 commit 2fb4116

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

README.md

+41-19
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def get_user_by_email(email: str) -> Result[User, str]:
6464
return Ok(user)
6565

6666
user_result = get_user_by_email(email)
67-
if is_ok(user_result): # or `isinstance(user_result, Ok)`
67+
if is_ok(user_result):
6868
# type(user_result.ok_value) == User
6969
do_something(user_result.ok_value)
7070
else:
@@ -114,25 +114,29 @@ Creating an instance:
114114
>>> res2 = Err('nay')
115115
```
116116

117-
Checking whether a result is `Ok` or `Err`. You can either use `is_ok`
118-
and `is_err` type guard **functions** or `isinstance`. This way you get
119-
type safe access that can be checked with MyPy (compared to the `is_ok`
120-
and `is_err` **methods**).
117+
Checking whether a result is `Ok` or `Err`:
121118

122119
``` python
123-
>>> res = Ok('yay')
124-
>>> is_ok(res)
125-
True
126-
>>> isinstance(res, Ok)
127-
True
128-
>>> is_err(res)
129-
False
130-
>>> isinstance(res, Err)
131-
False
132-
>>> res.is_ok()
133-
True
134-
>>> res.is_err()
135-
False
120+
if is_err(result):
121+
raise RuntimeError(result.err_value)
122+
do_something(result.ok_value)
123+
```
124+
or
125+
``` python
126+
if is_ok(result):
127+
do_something(result.ok_value)
128+
else:
129+
raise RuntimeError(result.err_value)
130+
```
131+
132+
Alternatively, `isinstance` can be used (interchangeably to type guard functions
133+
`is_ok` and `is_err`). However, relying on `isinstance` may result in code that
134+
is slightly less readable and less concise:
135+
136+
``` python
137+
if isinstance(result, Err):
138+
raise RuntimeError(result.err_value)
139+
do_something(result.ok_value)
136140
```
137141

138142
You can also check if an object is `Ok` or `Err` by using the `OkErr`
@@ -333,7 +337,7 @@ x = third_party.do_something(...) # could raise; who knows?
333337
safe_do_something = as_result(Exception)(third_party.do_something)
334338

335339
res = safe_do_something(...) # Ok(...) or Err(...)
336-
if isinstance(res, Ok):
340+
if is_ok(res):
337341
print(res.ok_value)
338342
```
339343

@@ -443,6 +447,24 @@ from the non-unix shell you're using on Windows.
443447

444448
## FAQ
445449

450+
- **Why should I use the `is_ok` (`is_err`) type guard function over the `is_ok` (`is_err`) method?**
451+
452+
As you can see in the following example, MyPy can only narrow the type correctly
453+
while using the type guard **functions**:
454+
```python
455+
result: Result[int, str]
456+
457+
if is_ok(result):
458+
reveal_type(result) # "result.result.Ok[builtins.int]"
459+
else:
460+
reveal_type(result) # "result.result.Err[builtins.str]"
461+
462+
if result.is_ok():
463+
reveal_type(result) # "Union[result.result.Ok[builtins.int], result.result.Err[builtins.str]]"
464+
else:
465+
reveal_type(result) # "Union[result.result.Ok[builtins.int], result.result.Err[builtins.str]]"
466+
```
467+
446468
- **Why do I get the "Cannot infer type argument" error with MyPy?**
447469

448470
There is [a bug in MyPy](https://github.com/python/mypy/issues/230)

0 commit comments

Comments
 (0)