Skip to content

Instantly share code, notes, and snippets.

@openbrian
Created April 21, 2021 13:59
Show Gist options
  • Save openbrian/5c92ed9e3bdb981823673c81c616dd85 to your computer and use it in GitHub Desktop.
Save openbrian/5c92ed9e3bdb981823673c81c616dd85 to your computer and use it in GitHub Desktop.
Dictionary incompatible types
from typing import Dict, TypeVar
from returns.curry import partial
from returns.result import Failure, Success, Result
T = TypeVar("T")
def get_dict_item(needle: str, haystack: Dict[str, T]) -> Result[T, str]:
if needle not in haystack:
return Failure(f"Dictionary did not have key {needle}.")
return Success(haystack[needle])
reveal_type(get_dict_item)
get_a = partial(get_dict_item, 'a')
reveal_type(get_a)
#get_b = partial(get_dict_item, 'b')
#reveal_type(get_b)
Success({'a': {'b': 1}}).bind(get_a)
#Success({'a': {'b': 1}}).bind(get_a).bind(get_b)
# Would be nice to compose somehting like xpath thing.bind(get_path("a/b"))
@openbrian
Copy link
Author

get_dict_item.py:18: note: Revealed type is 'def [T] (needle: builtins.str, haystack: builtins.dict[builtins.str, T`-1]) -> returns.result.Result[T`-1, builtins.str]'
get_dict_item.py:22: note: Revealed type is 'def [T] (haystack: builtins.dict[builtins.str, T`-1]) -> returns.result.Result[T`-1, builtins.str]'
get_dict_item.py:25: note: Revealed type is 'def [T] (haystack: builtins.dict[builtins.str, T`-1]) -> returns.result.Result[T`-1, builtins.str]'
get_dict_item.py:27: error: Argument 1 to "bind" of "Result" has incompatible type "Callable[[Dict[str, T]], Result[T, str]]"; expected "Callable[[Dict[str, Dict[str, int]]], KindN[Result[Any, Any], T, Any, Any]]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment