You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for this great python module! I'd like to submit a requested change to resolve an issue with Pylance. This bug will become more difficult to deal with once the .value method is removed, per this issue: #123
Description
When using the .ok_value and .err_value - Pylance detects an error where there is none. This does not raise any Exceptions, nor does it lead to unexpected code behavior, but it means that if you don't use the currently deprecated .value property then the type checker will identify an error where no error exists.
Example
Here is some code which runs as expected.
fromresultimportResult, Ok, Errdefvalidate_number_is_even(x: int) ->Result[str, str]:
""" NOTE: This is a sample function purely for the purpose of demonstrating result/pylance incongruence. Returns `Ok` if number is even, `Err` if number is odd. """returnErr("Failed: Odd Number") ifx%2elseOk("Passed: Even Number")
expected_ok=validate_number_is_even(x=2)
ifexpected_ok.is_ok():
print(expected_ok.ok_value)
expected_err=validate_number_is_even(x=3)
ifexpected_err.is_err():
print(expected_err.err_value)
Here is what the code looks like in VSCode, with Pylance and Error Lens enabled:
Proposed Fix
This can be resolved by adding the respective properties to the Ok and Err class, but having them simply raise Exceptions. Pylance no longer detects an error if we add the following properties. I've omitted most of the code from the Ok and Err class to avoid clutter.
New Ok Property
classOk(Generic[T]):
@propertydeferr_value(self) ->NoReturn:
""" Raise an error since this type is `Ok` """raiseAttributeError("Cannot access 'err_value' on 'Ok'")
New Err Property
classErr(Generic[E]):
@propertydefok_value(self) ->NoReturn:
""" Raise an error since this type is `Err` """raiseAttributeError("Cannot access 'ok_value' on 'Err'")
Temporary Fix
Currently, I'm using the .value property and simply adding this code to all the __init__.py files in the modules I'm developing:
importwarningswarnings.filterwarnings(
"ignore",
category=DeprecationWarning,
message="Accessing `.value` on Result type is deprecated, please use `.ok_value` or `.err_value` instead",
)
This ensures Pylance/VSCode do not detect non-existent problems, without raising the deprecation warning. I thought I would mention this now, as in reviewing the other GitHub issues, I found the note about how the .value method will soon be removed.
The text was updated successfully, but these errors were encountered:
This is documented in the README, but it's in the middle, so it may be easy to miss. Search for "The benefit of isinstance is better type checking that type guards currently do not offer," in the README to find that section.
This can be resolved by adding the respective properties to the Ok and Err class, but having them simply raise Exceptions
This is not the direction we're going towards with this library. The design of having ok_value and err_value only exist on their respective result types was intentional to make the type checker report errors if you try to access the property on the wrong type.
Thanks for this great python module! I'd like to submit a requested change to resolve an issue with Pylance. This bug will become more difficult to deal with once the
.value
method is removed, per this issue: #123Description
When using the
.ok_value
and.err_value
- Pylance detects an error where there is none. This does not raise any Exceptions, nor does it lead to unexpected code behavior, but it means that if you don't use the currently deprecated.value
property then the type checker will identify an error where no error exists.Example
Here is some code which runs as expected.
Here is what the code looks like in VSCode, with Pylance and Error Lens enabled:
Proposed Fix
This can be resolved by adding the respective properties to the
Ok
andErr
class, but having them simply raise Exceptions. Pylance no longer detects an error if we add the following properties. I've omitted most of the code from theOk
andErr
class to avoid clutter.New
Ok
PropertyNew
Err
PropertyTemporary Fix
Currently, I'm using the
.value
property and simply adding this code to all the__init__.py
files in the modules I'm developing:This ensures Pylance/VSCode do not detect non-existent problems, without raising the deprecation warning. I thought I would mention this now, as in reviewing the other GitHub issues, I found the note about how the
.value
method will soon be removed.The text was updated successfully, but these errors were encountered: