Skip to content

Commit

Permalink
Improve types for ok_or and ok_or_else methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckwondo committed Aug 19, 2024
1 parent 4d3e649 commit b334f6a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/maybe/maybe.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def unwrap(self) -> T:
"""
return self._value

def unwrap_or(self, _default: object) -> T:
def unwrap_or(self, _default: U) -> T: # pyright: ignore[reportInvalidTypeVarUse]
"""
Return the value.
"""
Expand Down Expand Up @@ -143,7 +143,7 @@ def or_else(self, _op: object) -> Some[T]:

if _RESULT_INSTALLED:

def ok_or(self, _error: object) -> result.Ok[T]:
def ok_or(self, _error: E) -> result.Ok[T]: # pyright: ignore[reportInvalidTypeVarUse]
"""
Return a `result.Ok` with the inner value.
Expand All @@ -152,7 +152,7 @@ def ok_or(self, _error: object) -> result.Ok[T]:
"""
return result.Ok(self._value)

def ok_or_else(self, _op: object) -> result.Ok[T]:
def ok_or_else(self, _op: Callable[[], E]) -> result.Ok[T]:
"""
Return a `result.Ok` with the inner value.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_maybe.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_some_ok_or() -> None:


def test_some_ok_or_else() -> None:
assert Some(1).ok_or_else('error') == result.Ok(1)
assert Some(1).ok_or_else(lambda: 'error') == result.Ok(1)


def test_nothing_ok_or() -> None:
Expand Down
34 changes: 34 additions & 0 deletions tests/type-checking/test_maybe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,40 @@
some = personalized_greeting.some()
reveal_type(some) # N: Revealed type is "Union[builtins.str, None]"
- case: ok_or
disable_cache: false
main: |
from maybe import Maybe, Some, Nothing
from result import Ok, Err
greeting: Maybe[str] = Some("Hello")
ok_greeting = greeting.ok_or("error")
reveal_type(ok_greeting) # N: Revealed type is "Union[result.result.Ok[builtins.str], result.result.Err[builtins.str]]"
nothing: Maybe[str] = Nothing()
no_greeting = nothing.ok_or("error")
reveal_type(no_greeting) # N: Revealed type is "Union[result.result.Ok[builtins.str], result.result.Err[builtins.str]]"
- case: ok_or_else
disable_cache: false
main: |
from maybe import Maybe, Some, Nothing
from result import Ok, Err, Result
greeting: Maybe[str] = Some("Hello")
ok_greeting = greeting.ok_or_else(lambda: "error")
reveal_type(ok_greeting) # N: Revealed type is "Union[result.result.Ok[builtins.str], result.result.Err[builtins.str]]"
greeting.ok_or_else("error") # E: Argument 1 to "ok_or_else" of "Some" has incompatible type "str"; expected "Callable[[], Never]" [arg-type] # E: Argument 1 to "ok_or_else" of "Nothing" has incompatible type "str"; expected "Callable[[], Never]" [arg-type]
nothing: Maybe[str] = Nothing()
no_greeting: Result[str, ValueError] = nothing.ok_or_else(lambda: ValueError("error"))
reveal_type(no_greeting) # N: Revealed type is "Union[result.result.Ok[builtins.str], result.result.Err[builtins.ValueError]]"
nothing.ok_or_else("error") # E: Argument 1 to "ok_or_else" of "Some" has incompatible type "str"; expected "Callable[[], Never]" [arg-type] # E: Argument 1 to "ok_or_else" of "Nothing" has incompatible type "str"; expected "Callable[[], Never]" [arg-type]
- case: typeguard
disable_cache: false
main: |
Expand Down

0 comments on commit b334f6a

Please sign in to comment.