Skip to content

Commit 69b1311

Browse files
committed
Type redone
1 parent 24df71b commit 69b1311

File tree

13 files changed

+100
-68
lines changed

13 files changed

+100
-68
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ We follow Semantic Versions since the `0.1.0` release.
99

1010
- Moves all types to `.pyi` files
1111
- Renames all classes according to new naming pattern
12+
- **HUGE** improvement of types
1213

1314

1415
## Version 0.3.1

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Contents
1818

1919
pages/monad.rst
2020
pages/maybe.rst
21-
pages/Result.rst
21+
pages/result.rst
2222
pages/do-notation.rst
2323
pages/functions.rst
2424

docs/pages/either.rst renamed to docs/pages/result.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
Result
22
======
33

4-
Also known as ``Result``.
5-
64
``Result`` is obviously a result of some series of computations.
75
It might succeed with some resulting value.
86
Or it might return an error with some extra details.

returns/do_notation.pyi

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# -*- coding: utf-8 -*-
22

3-
from typing import Callable
3+
from typing import Callable, TypeVar
44

5-
from returns.primitives.types import MonadType
5+
from returns.primitives.monad import Monad
66

7-
# Typing decorators is not an easy task, see:
8-
# https://github.com/python/mypy/issues/3157
7+
_ReturnsMonad = TypeVar('_ReturnsMonad', bound=Callable[..., Monad])
98

109

1110
def do_notation(
12-
function: Callable[..., MonadType],
13-
) -> Callable[..., MonadType]:
11+
function: _ReturnsMonad,
12+
) -> _ReturnsMonad:
1413
...

returns/functions.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
from typing import Callable, TypeVar
44

5-
from returns.primitives.types import MonadType
5+
from returns.primitives.monad import Monad
66
from returns.result import Result
77

8+
_MonadType = TypeVar('_MonadType', bound=Monad)
89
_ReturnType = TypeVar('_ReturnType')
910

1011

11-
def is_successful(monad: MonadType) -> bool:
12+
def is_successful(monad: _MonadType) -> bool:
1213
...
1314

1415

16+
# Typing decorators is not an easy task, see:
17+
# https://github.com/python/mypy/issues/3157
18+
1519
def safe(
1620
function: Callable[..., _ReturnType],
1721
) -> Callable[..., Result[_ReturnType, Exception]]:

returns/maybe.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# -*- coding: utf-8 -*-
22

33
from abc import ABCMeta
4-
from typing import Generic, TypeVar
4+
from typing import TypeVar
55

66
from typing_extensions import Literal
77

88
from returns.primitives.exceptions import UnwrapFailedError
9-
from returns.primitives.monad import Monad
9+
from returns.primitives.monad import GenericMonadOneSlot
1010

1111
_ValueType = TypeVar('_ValueType')
1212

1313

14-
class Maybe(Generic[_ValueType], Monad[_ValueType], metaclass=ABCMeta):
14+
class Maybe(GenericMonadOneSlot[_ValueType], metaclass=ABCMeta):
1515
"""
1616
Represents a result of a series of commutation that can return ``None``.
1717

returns/maybe.pyi

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ from typing import Any, Callable, NoReturn, TypeVar, Union, overload
55

66
from typing_extensions import Literal, final
77

8-
from returns.primitives.monad import Monad
8+
from returns.primitives.monad import GenericMonadOneSlot, Monad
99

10-
_MonadType = TypeVar('_MonadType', bound='Monad')
10+
_MonadType = TypeVar('_MonadType', bound=Monad)
1111
_ValueType = TypeVar('_ValueType')
1212
_NewValueType = TypeVar('_NewValueType')
1313

1414

15-
class Maybe(Monad[_ValueType], metaclass=ABCMeta):
15+
class Maybe(GenericMonadOneSlot[_ValueType], metaclass=ABCMeta):
1616
@overload
1717
@classmethod
1818
def new(cls, inner_value: Literal[None]) -> 'Nothing': # type: ignore
@@ -26,19 +26,25 @@ class Maybe(Monad[_ValueType], metaclass=ABCMeta):
2626
def fmap(
2727
self,
2828
function: Callable[[_ValueType], _NewValueType],
29-
) -> _MonadType:
29+
) -> Union['Some[_NewValueType]', 'Maybe[_ValueType]']:
3030
...
3131

3232
def bind(
3333
self,
3434
function: Callable[[_ValueType], _MonadType],
35-
) -> _MonadType:
35+
) -> Union[_MonadType, 'Maybe[_ValueType]']:
3636
...
3737

38-
def efmap(self, function) -> 'Some[_NewValueType]':
38+
def efmap(
39+
self,
40+
function: Callable[[Literal[None]], '_NewValueType'],
41+
) -> Union['Some[_ValueType]', 'Some[_NewValueType]']:
3942
...
4043

41-
def ebind(self, function) -> _MonadType:
44+
def ebind(
45+
self,
46+
function: Callable[[Literal[None]], _MonadType],
47+
) -> Union[_MonadType, 'Maybe[_ValueType]']:
4248
...
4349

4450
def value_or(
@@ -61,10 +67,10 @@ class Nothing(Maybe[Any]):
6167
def __init__(self, inner_value: Literal[None] = ...) -> None:
6268
...
6369

64-
def fmap(self, function) -> 'Nothing': # type: ignore
70+
def fmap(self, function) -> 'Nothing':
6571
...
6672

67-
def bind(self, function) -> 'Nothing': # type: ignore
73+
def bind(self, function) -> 'Nothing':
6874
...
6975

7076
def efmap(
@@ -96,7 +102,7 @@ class Some(Maybe[_ValueType]):
96102
def __init__(self, inner_value: _ValueType) -> None:
97103
...
98104

99-
def fmap( # type: ignore
105+
def fmap(
100106
self,
101107
function: Callable[[_ValueType], _NewValueType],
102108
) -> 'Some[_NewValueType]':
@@ -108,10 +114,10 @@ class Some(Maybe[_ValueType]):
108114
) -> _MonadType:
109115
...
110116

111-
def efmap(self, function) -> 'Some[_ValueType]': # type: ignore
117+
def efmap(self, function) -> 'Some[_ValueType]':
112118
...
113119

114-
def ebind(self, function) -> 'Some[_ValueType]': # type: ignore
120+
def ebind(self, function) -> 'Some[_ValueType]':
115121
...
116122

117123
def value_or(self, default_value: _NewValueType) -> _ValueType:

returns/primitives/exceptions.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3-
from returns.primitives.types import MonadType
3+
from typing import TypeVar
4+
5+
from returns.primitives.monad import Monad
6+
7+
_MonadType = TypeVar('_MonadType', bound=Monad)
48

59

610
class UnwrapFailedError(Exception):
7-
def __init__(self, monad: MonadType) -> None:
11+
def __init__(self, monad: _MonadType) -> None:
812
self.halted_monad = monad
913

1014

returns/primitives/monad.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from returns.primitives.exceptions import ImmutableStateError
77

88
_ValueType = TypeVar('_ValueType')
9+
_ErrorType = TypeVar('_ErrorType')
910

1011

11-
class _BaseMonad(Generic[_ValueType], metaclass=ABCMeta):
12+
class _BaseMonad(object, metaclass=ABCMeta):
1213
"""Utility class to provide all needed magic methods to the contest."""
1314

1415
__slots__ = ('_inner_value',)
@@ -45,7 +46,7 @@ def __eq__(self, other):
4546
return self._inner_value == other._inner_value # noqa: Z441
4647

4748

48-
class Monad(_BaseMonad[_ValueType]):
49+
class Monad(_BaseMonad, metaclass=ABCMeta):
4950
"""
5051
Represents a "context" in which calculations can be executed.
5152
@@ -131,3 +132,21 @@ def failure(self): # pragma: no cover
131132
This method is the opposite of :meth:`~unwrap`.
132133
"""
133134
raise NotImplementedError()
135+
136+
137+
class GenericMonadOneSlot(Generic[_ValueType], Monad):
138+
"""
139+
Base class for monads with one typed slot.
140+
141+
Use this type for generic inheritance only.
142+
Use :class:`~Monad` as a general type for polymorphism.
143+
"""
144+
145+
146+
class GenericMonadTwoSlots(Generic[_ValueType, _ErrorType], Monad):
147+
"""
148+
Base class for monads with two typed slot.
149+
150+
Use this type for generic inheritance only.
151+
Use :class:`~Monad` as a general type for polymorphism.
152+
"""

returns/primitives/monad.pyi

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
from abc import ABCMeta, abstractmethod
44
from typing import Any, Generic, NoReturn, TypeVar
55

6-
# These type variables are widely used in our source code.
76
_ValueType = TypeVar('_ValueType')
8-
_NewValueType = TypeVar('_NewValueType')
7+
_ErrorType = TypeVar('_ErrorType')
98

109

11-
class _BaseMonad(Generic[_ValueType], metaclass=ABCMeta):
10+
class _BaseMonad(object, metaclass=ABCMeta):
1211
__slots__ = ('_inner_value',)
1312
_inner_value: Any
1413

@@ -25,7 +24,7 @@ class _BaseMonad(Generic[_ValueType], metaclass=ABCMeta):
2524
...
2625

2726

28-
class Monad(_BaseMonad[_ValueType]):
27+
class Monad(_BaseMonad, metaclass=ABCMeta):
2928
@abstractmethod
3029
def fmap(self, function):
3130
...
@@ -47,9 +46,25 @@ class Monad(_BaseMonad[_ValueType]):
4746
...
4847

4948
@abstractmethod
50-
def unwrap(self) -> _ValueType:
49+
def unwrap(self):
5150
...
5251

5352
@abstractmethod
5453
def failure(self):
5554
...
55+
56+
57+
class GenericMonadOneSlot(
58+
Generic[_ValueType],
59+
Monad,
60+
metaclass=ABCMeta,
61+
):
62+
...
63+
64+
65+
class GenericMonadTwoSlots(
66+
Generic[_ValueType, _ErrorType],
67+
Monad,
68+
metaclass=ABCMeta,
69+
):
70+
...

0 commit comments

Comments
 (0)