Skip to content

Commit

Permalink
Merge pull request #42 from przemyslawjanpietrzak/feature/mypy2
Browse files Browse the repository at this point in the history
Feature/mypy2
  • Loading branch information
Przemyslaw Pietrzak committed Jun 1, 2018
2 parents 4c57732 + 3c7639e commit 643dfb2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
12 changes: 6 additions & 6 deletions pymonet/either.py
@@ -1,4 +1,4 @@
from typing import TypeVar, Generic, Callable
from typing import TypeVar, Generic, Callable, Any


T = TypeVar('T')
Expand All @@ -14,7 +14,7 @@ class Either(Generic[T]):
def __init__(self, value: T) -> None:
self.value = value

def __eq__(self, other) -> bool:
def __eq__(self, other: object) -> bool:
return isinstance(other, Either) and\
self.value == other.value and\
self.is_right() == other.is_right()
Expand Down Expand Up @@ -82,10 +82,10 @@ def is_right(self):
pass


class Left(Either):
class Left(Either, Generic[T]):
"""Not successfully Either"""

def map(self, _):
def map(self, _: Callable[[Any], Any]) -> 'Left[T]':
"""
Take mapper function and return new instance of Left with the same value.
Expand All @@ -94,7 +94,7 @@ def map(self, _):
"""
return Left(self.value)

def bind(self, _):
def bind(self, _) -> 'Left[T]':
"""
Take mapper function and return value of Left.
Expand Down Expand Up @@ -150,7 +150,7 @@ def to_validation(self):
class Right(Either):
"""Not successfully Either"""

def map(self, mapper: Callable[[T], U]):
def map(self, mapper: Callable[[T], U]) -> Either[U]:
"""
Take mapper function and return new instance of Right with mapped value.
Expand Down
20 changes: 14 additions & 6 deletions pymonet/lazy.py
@@ -1,10 +1,18 @@
class Lazy:
from typing import TypeVar, Generic, Callable


T = TypeVar('T')
U = TypeVar('U')
W = TypeVar('W')


class Lazy(Generic[T, U]):
"""
Data type for storage any type of function.
This function (and all his mappers) will be called only during calling fold method
"""

def __init__(self, constructor_fn):
def __init__(self, constructor_fn: Callable[[T], U]) -> None:
"""
:param constructor_fn: function to call during fold method call
:type constructor_fn: Function() -> A
Expand All @@ -16,7 +24,7 @@ def __init__(self, constructor_fn):
def __str__(self) -> str: # pragma: no cover
return 'Lazy[fn={}, value={}, is_evaluated={}]'.format(self.constructor_fn, self.value, self.is_evaluated)

def __eq__(self, other)-> bool:
def __eq__(self, other: object) -> bool:
"""
Two Lazy are equals where both are evaluated both have the same value and constructor functions.
"""
Expand All @@ -28,7 +36,7 @@ def __eq__(self, other)-> bool:
)

@classmethod
def of(cls, value):
def of(cls, value: U) -> 'Lazy[T, U]':
"""
Returns Lazy with function returning argument.
Expand All @@ -45,7 +53,7 @@ def _compute_value(self, *args):

return self.value

def map(self, mapper):
def map(self, mapper: Callable[[U], W]) -> 'Lazy[T, W]':
"""
Take function Function(A) -> B and returns new Lazy with mapped result of Lazy constructor function.
Both mapper end constructor will be called only during calling fold method.
Expand All @@ -69,7 +77,7 @@ def ap(self, applicative):
"""
return Lazy(lambda *args: self.constructor_fn(applicative.get(*args)))

def bind(self, fn):
def bind(self, fn: 'Callable[[U], Lazy[U, W]]') -> 'Lazy[T, W]':
"""
Take function and call constructor function passing returned value to fn function.
Expand Down
2 changes: 1 addition & 1 deletion pymonet/maybe.py
Expand Up @@ -98,7 +98,7 @@ def filter(self, filterer: Callable[[T], bool]) -> Union['Maybe[T]', 'Maybe[None
return Maybe.nothing()
return Maybe.just(self.value)

def get_or_else(self, default_value):
def get_or_else(self, default_value: U) -> Union[T, U]:
"""
If Maybe is empty return default_value, in other case.
Expand Down

0 comments on commit 643dfb2

Please sign in to comment.