@@ -64,7 +64,7 @@ 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 is_ok(user_result): # or `isinstance(user_result, Ok)`
67
+ if is_ok(user_result):
68
68
# type(user_result.ok_value) == User
69
69
do_something(user_result.ok_value)
70
70
else :
@@ -114,25 +114,29 @@ Creating an instance:
114
114
>> > res2 = Err(' nay' )
115
115
```
116
116
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 ` :
121
118
122
119
``` 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)
136
140
```
137
141
138
142
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?
333
337
safe_do_something = as_result(Exception )(third_party.do_something)
334
338
335
339
res = safe_do_something(... ) # Ok(...) or Err(...)
336
- if isinstance (res, Ok ):
340
+ if is_ok (res):
337
341
print (res.ok_value)
338
342
```
339
343
@@ -443,6 +447,24 @@ from the non-unix shell you're using on Windows.
443
447
444
448
## FAQ
445
449
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
+
446
468
- ** Why do I get the "Cannot infer type argument" error with MyPy?**
447
469
448
470
There is [ a bug in MyPy] ( https://github.com/python/mypy/issues/230 )
0 commit comments