Skip to content

Commit 430f073

Browse files
committed
Proposal: Mention type guard functons before isinstance
1 parent f20e323 commit 430f073

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

README.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ 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 isinstance(user_result, Ok): # or `is_ok(user_result)`
67+
if is_ok(user_result): # or `isinstance(user_result, Ok)`
6868
# type(user_result.ok_value) == User
6969
do_something(user_result.ok_value)
70-
else: # or `elif is_err(user_result)`
70+
else:
7171
# type(user_result.err_value) == str
7272
raise RuntimeError('Could not fetch user: %s' % user_result.err_value)
7373
```
@@ -97,12 +97,9 @@ for a, b in values:
9797

9898
Not all methods
9999
(<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.
106103

107104
## API
108105

@@ -119,20 +116,19 @@ Creating an instance:
119116

120117
Checking whether a result is `Ok` or `Err`. You can either use `is_ok`
121118
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**).
125121

126122
``` python
127123
>>> res = Ok('yay')
128-
>>> isinstance(res, Ok)
129-
True
130124
>>> is_ok(res)
131125
True
132-
>>> isinstance(res, Err)
133-
False
126+
>>> isinstance(res, Ok)
127+
True
134128
>>> is_err(res)
135129
False
130+
>>> isinstance(res, Err)
131+
False
136132
>>> res.is_ok()
137133
True
138134
>>> res.is_err()

0 commit comments

Comments
 (0)