Skip to content

Commit

Permalink
Update docs for deprecated .value field (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
francium authored Dec 8, 2023
1 parent 3994af7 commit d77f3f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
8 changes: 8 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Migration guides

## 0.11.0 -> 0.12 migration

``.value`` is now deprecated. New code should use ``.ok_value`` on instances of
``Ok`` and ``.err_value`` on instances of ``Err``. Existing code using
``.value`` will continue to work, but will result in a deprecation warning being
logged. Users of this library are encouraged to migrate away from ``.value``
before it is removed in a future version.

## 0.10 -> 0.11 migration

The 0.11 migration includes one breaking change:
Expand Down
17 changes: 10 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ To something like this:

user_result = get_user_by_email(email)
if isinstance(user_result, Ok): # or `is_ok(user_result)`
# type(user_result.value) == User
do_something(user_result.value)
# type(user_result.ok_value) == User
do_something(user_result.ok_value)
else: # or `elif is_err(user_result)`
# type(user_result.value) == str
raise RuntimeError('Could not fetch user: %s' % user_result.value)
# type(user_result.err_value) == str
raise RuntimeError('Could not fetch user: %s' % user_result.err_value)

Note that ``.ok_value`` exists only on an instance of ``Ok`` and ``.err_value``
exists only on an instance of ``Err``.

And if you're using python version ``3.10`` or later, you can use the elegant ``match`` statement as well:

Expand Down Expand Up @@ -186,9 +189,9 @@ Access the value directly, without any other checks:

>>> res1 = Ok('yay')
>>> res2 = Err('nay')
>>> res1.value
>>> res1.ok_value
'yay'
>>> res2.value
>>> res2.err_value
'nay'

Note that this is a property, you cannot assign to it. Results are immutable.
Expand Down Expand Up @@ -341,7 +344,7 @@ unconventional syntax (without the usual ``@``):

res = safe_do_something(...) # Ok(...) or Err(...)
if isinstance(res, Ok):
print(res.value)
print(res.ok_value)


Do notation
Expand Down
2 changes: 1 addition & 1 deletion src/result/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def value(self) -> T:
"""
warn(
"Accessing `.value` on Result type is deprecated, please use "
+ "`.ok_value` or '.err_value' instead",
+ "`.ok_value` or `.err_value` instead",
DeprecationWarning,
stacklevel=2,
)
Expand Down

0 comments on commit d77f3f6

Please sign in to comment.