-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wip: Add is_ok() and is_err() typing.TypeGuard shorthands #72
Conversation
blocked by for the time being |
a66a98e
to
fe063d6
Compare
Do you have any updates now that the issue was successfully closed, as I would want this to be implemented as well? |
fe063d6
to
6518995
Compare
this indeed works these days using $ mypy --version
mypy 0.971 (compiled: yes) the ci failure on
is due to tholo/pytest-flake8#87 |
i've updated and rebased and simplified this (draft) PR now that upstream mypy issues have been resolved |
6518995
to
2a26c07
Compare
2a26c07
to
a9a8441
Compare
fixed via #101 |
def test_is_ok_is_err() -> None: | ||
"""The module-level check functions work.""" | ||
o: Result[str, int] = Ok('yay') | ||
n: Result[str, int] = Err(100) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using pyright and it's reporting these as Ok
instead of Result
. A simple fix is to define a identify function that makes the typechecker not report these as the literal types,
def passthrough(o: Result[str, int]) -> Result[str, int]:
return o
o = passthrough(Ok('yay'))
n = passthrough(Err(100))
Without this passthrough function, you don't get proper type checking in this case,
"""The module-level type guard shorthands work."""
def passthrough(o: Result[str, int]) -> Result[str, int]:
return o
o = passthrough(Ok('yay'))
n = passthrough(Err(100))
# Type checkers supporting typing.TypeGuard should accept the code below:
if not is_ok(o):
_ = o.value + "" # works only for str
^^^^^^^^^^^
Operator "+" not supported for types "str | int" and "Literal['']"
if not is_err(n):
_ = n.value + 0 # works only for int
^^^^^^^^^^^
Operator "+" not supported for types "str | int" and "Literal[0]"
Closing in favor of 135 |
See #69