From d77f3f6313acbeeb4cf936d1d57fe075b0bdd43e Mon Sep 17 00:00:00 2001 From: francium Date: Thu, 7 Dec 2023 22:09:46 -0500 Subject: [PATCH] Update docs for deprecated .value field (#159) --- MIGRATING.md | 8 ++++++++ README.rst | 17 ++++++++++------- src/result/result.py | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/MIGRATING.md b/MIGRATING.md index 6916142..efc9447 100644 --- a/MIGRATING.md +++ b/MIGRATING.md @@ -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: diff --git a/README.rst b/README.rst index a66c3e3..e214992 100644 --- a/README.rst +++ b/README.rst @@ -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: @@ -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. @@ -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 diff --git a/src/result/result.py b/src/result/result.py index afc5493..099d8d1 100644 --- a/src/result/result.py +++ b/src/result/result.py @@ -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, )