Skip to content

Commit bd8622b

Browse files
committed
Initial pyi stubs are created
1 parent 11a04d8 commit bd8622b

15 files changed

+325
-14
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ indent_size = 2
1212

1313
[*.py]
1414
indent_size = 4
15+
16+
[*.pyi]
17+
indent_size = 4

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ install:
1919
script:
2020
- poetry run flake8 returns tests docs
2121
- poetry run mypy returns tests/**/*.py
22+
- poetry run mypy returns/**/*.pyi
2223
- poetry run pytest
2324
- poetry run doc8 -q docs
2425
- poetry check

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![wemake.services](https://img.shields.io/badge/%20-wemake.services-green.svg?label=%20&logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC%2FxhBQAAAAFzUkdCAK7OHOkAAAAbUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP%2F%2F%2F5TvxDIAAAAIdFJOUwAjRA8xXANAL%2Bv0SAAAADNJREFUGNNjYCAIOJjRBdBFWMkVQeGzcHAwksJnAPPZGOGAASzPzAEHEGVsLExQwE7YswCb7AFZSF3bbAAAAABJRU5ErkJggg%3D%3D)](https://wemake.services) [![Build Status](https://travis-ci.org/dry-python/returns.svg?branch=master)](https://travis-ci.org/dry-python/returns) [![Coverage Status](https://coveralls.io/repos/github/dry-python/returns/badge.svg?branch=master)](https://coveralls.io/github/dry-python/returns?branch=master) [![Documentation Status](https://readthedocs.org/projects/returns/badge/?version=latest)](https://returns.readthedocs.io/en/latest/?badge=latest) [![Python Version](https://img.shields.io/pypi/pyversions/returns.svg)](https://pypi.org/project/returns/) [![wemake-python-styleguide](https://img.shields.io/badge/style-wemake-000000.svg)](https://github.com/wemake-services/wemake-python-styleguide)
44

55

6-
Make your functions return something meaningful and safe!and safe.
6+
Make your functions return something meaningful and safe!
77

88

99
## Features

poetry.lock

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ sphinxcontrib-mermaid = "^0.3.1"
4949
doc8 = "^0.8.0"
5050
m2r = "^0.2.1"
5151
tomlkit = "^0.5.3"
52+
flake8-pyi = "^18.3"
5253

5354
[build-system]
5455
requires = ["poetry>=0.12"]

returns/do_notation.pyi

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from typing import Callable
4+
5+
from returns.primitives.types import MonadType
6+
7+
8+
def do_notation(
9+
function: Callable[..., MonadType],
10+
) -> Callable[..., MonadType]:
11+
...

returns/either.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
from returns.primitives.monad import Monad, NewValueType, ValueType
1010
from returns.primitives.types import MonadType
1111

12-
ErrorType = TypeVar('ErrorType')
12+
_ErrorType = TypeVar('_ErrorType')
1313

1414

1515
# That's the most ugly part.
1616
# We need to express `Either` with two type parameters and
1717
# Left and Right with just one parameter.
1818
# And that's how we do it. Any other and more cleaner ways are appreciated.
19-
class Either(Generic[ValueType, ErrorType], metaclass=ABCMeta):
19+
class Either(Generic[ValueType, _ErrorType], metaclass=ABCMeta):
2020
"""
2121
Represents a calculation that may either fail or succeed.
2222
@@ -26,7 +26,7 @@ class Either(Generic[ValueType, ErrorType], metaclass=ABCMeta):
2626
and 'Left' (or its alias 'Failure').
2727
"""
2828

29-
_inner_value: Union[ValueType, ErrorType]
29+
_inner_value: Union[ValueType, _ErrorType]
3030

3131
@abstractmethod
3232
def unwrap(self) -> ValueType: # pragma: no cover
@@ -40,33 +40,33 @@ def unwrap(self) -> ValueType: # pragma: no cover
4040

4141

4242
@final
43-
class Left(Either[Any, ErrorType], Monad[ErrorType]):
43+
class Left(Either[Any, _ErrorType], Monad[_ErrorType]):
4444
"""
4545
Represents a calculation which has failed.
4646
4747
It should contain an error code or message.
4848
To help with readability you may alternatively use the alias 'Failure'.
4949
"""
5050

51-
def __init__(self, inner_value: ErrorType) -> None:
51+
def __init__(self, inner_value: _ErrorType) -> None:
5252
"""
5353
Wraps the given value in the Container.
5454
5555
'value' is any arbitrary value of any type including functions.
5656
"""
5757
object.__setattr__(self, '_inner_value', inner_value)
5858

59-
def fmap(self, function) -> 'Left[ErrorType]':
59+
def fmap(self, function) -> 'Left[_ErrorType]':
6060
"""Returns the 'Left' instance that was used to call the method."""
6161
return self
6262

63-
def bind(self, function) -> 'Left[ErrorType]':
63+
def bind(self, function) -> 'Left[_ErrorType]':
6464
"""Returns the 'Left' instance that was used to call the method."""
6565
return self
6666

6767
def efmap(
6868
self,
69-
function: Callable[[ErrorType], NewValueType],
69+
function: Callable[[_ErrorType], NewValueType],
7070
) -> 'Right[NewValueType]':
7171
"""
7272
Applies function to the inner value.
@@ -78,7 +78,7 @@ def efmap(
7878
"""
7979
return Right(function(self._inner_value))
8080

81-
def ebind(self, function: Callable[[ErrorType], MonadType]) -> MonadType:
81+
def ebind(self, function: Callable[[_ErrorType], MonadType]) -> MonadType:
8282
"""
8383
Applies 'function' to the result of a previous calculation.
8484
@@ -95,7 +95,7 @@ def unwrap(self) -> NoReturn:
9595
"""Raises an exception, since it does not have a value inside."""
9696
raise UnwrapFailedError(self)
9797

98-
def failure(self) -> ErrorType:
98+
def failure(self) -> _ErrorType:
9999
"""Unwraps inner error value from failed monad."""
100100
return self._inner_value
101101

returns/either.pyi

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from abc import ABCMeta, abstractmethod
4+
from typing import Any, Callable, Generic, NoReturn, TypeVar, Union
5+
6+
from typing_extensions import final
7+
8+
from returns.primitives.monad import Monad, NewValueType, ValueType
9+
from returns.primitives.types import MonadType
10+
11+
_ErrorType = TypeVar('_ErrorType')
12+
13+
14+
class Either(Generic[ValueType, _ErrorType], metaclass=ABCMeta):
15+
_inner_value: Union[ValueType, _ErrorType]
16+
17+
@abstractmethod
18+
def unwrap(self) -> ValueType: # pragma: no cover
19+
...
20+
21+
22+
@final
23+
class Left(Either[Any, _ErrorType], Monad[_ErrorType]):
24+
_inner_value: _ErrorType
25+
26+
def __init__(self, inner_value: _ErrorType) -> None:
27+
...
28+
29+
def fmap(self, function) -> 'Left[_ErrorType]':
30+
...
31+
32+
def bind(self, function) -> 'Left[_ErrorType]':
33+
...
34+
35+
def efmap(
36+
self,
37+
function: Callable[[_ErrorType], NewValueType],
38+
) -> 'Right[NewValueType]':
39+
...
40+
41+
def ebind(self, function: Callable[[_ErrorType], MonadType]) -> MonadType:
42+
...
43+
44+
def value_or(self, default_value: NewValueType) -> NewValueType:
45+
...
46+
47+
def unwrap(self) -> NoReturn:
48+
...
49+
50+
def failure(self) -> _ErrorType:
51+
...
52+
53+
54+
@final
55+
class Right(Either[ValueType, Any], Monad[ValueType]):
56+
_inner_value: ValueType
57+
58+
def __init__(self, inner_value: ValueType) -> None:
59+
...
60+
61+
def fmap(
62+
self,
63+
function: Callable[[ValueType], NewValueType],
64+
) -> 'Right[NewValueType]':
65+
...
66+
67+
def bind(
68+
self,
69+
function: Callable[[ValueType], MonadType],
70+
) -> MonadType:
71+
...
72+
73+
def efmap(self, function) -> 'Right[ValueType]':
74+
...
75+
76+
def ebind(self, function) -> 'Right[ValueType]':
77+
...
78+
79+
def value_or(self, default_value: NewValueType) -> ValueType:
80+
...
81+
82+
def unwrap(self) -> ValueType:
83+
...
84+
85+
def failure(self) -> NoReturn:
86+
...
87+
88+
89+
# Useful aliases for end users:
90+
91+
Result = Either
92+
Success = Right
93+
Failure = Left

returns/functions.pyi

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from typing import Callable, TypeVar
4+
5+
from returns.either import Either
6+
from returns.primitives.types import MonadType
7+
8+
_ReturnType = TypeVar('_ReturnType')
9+
10+
11+
def is_successful(monad: MonadType) -> bool:
12+
...
13+
14+
15+
def safe(
16+
function: Callable[..., _ReturnType],
17+
) -> Callable[..., Either[_ReturnType, Exception]]:
18+
...

returns/maybe.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
from abc import ABCMeta
34
from typing import Callable, NoReturn, Union, overload
45

56
from typing_extensions import Literal, final
@@ -9,7 +10,7 @@
910
from returns.primitives.types import MonadType
1011

1112

12-
class Maybe(Monad[ValueType]):
13+
class Maybe(Monad[ValueType], metaclass=ABCMeta):
1314
"""
1415
Represents a result of a series of commutation that can return ``None``.
1516

returns/maybe.pyi

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from abc import ABCMeta
4+
from typing import Callable, NoReturn, overload
5+
6+
from typing_extensions import Literal, final
7+
8+
from returns.primitives.monad import Monad, NewValueType, ValueType
9+
from returns.primitives.types import MonadType
10+
11+
12+
class Maybe(Monad[ValueType], metaclass=ABCMeta):
13+
@overload
14+
@classmethod
15+
def new(cls, inner_value: Literal[None]) -> 'Nothing': # type: ignore
16+
...
17+
18+
@overload # noqa: F811
19+
@classmethod
20+
def new(cls, inner_value: ValueType) -> 'Some[ValueType]':
21+
...
22+
23+
24+
@final
25+
class Nothing(Maybe[Literal[None]]):
26+
_inner_value: Literal[None]
27+
28+
def __init__(self, inner_value: Literal[None] = ...) -> None:
29+
...
30+
31+
def fmap(self, function) -> 'Nothing':
32+
...
33+
34+
def bind(self, function) -> 'Nothing':
35+
...
36+
37+
def efmap(
38+
self,
39+
function: Callable[[Literal[None]], 'NewValueType'],
40+
) -> 'Some[NewValueType]':
41+
...
42+
43+
def ebind(
44+
self,
45+
function: Callable[[Literal[None]], MonadType],
46+
) -> MonadType:
47+
...
48+
49+
def value_or(self, default_value: NewValueType) -> NewValueType:
50+
...
51+
52+
def unwrap(self) -> NoReturn:
53+
...
54+
55+
def failure(self) -> None:
56+
...
57+
58+
59+
@final
60+
class Some(Maybe[ValueType]):
61+
_inner_value: ValueType
62+
63+
def __init__(self, inner_value: ValueType) -> None:
64+
...
65+
66+
def fmap(
67+
self,
68+
function: Callable[[ValueType], NewValueType],
69+
) -> 'Some[NewValueType]':
70+
...
71+
72+
def bind(
73+
self,
74+
function: Callable[[ValueType], MonadType],
75+
) -> MonadType:
76+
...
77+
78+
def efmap(self, function) -> 'Some[ValueType]':
79+
...
80+
81+
def ebind(self, function) -> 'Some[ValueType]':
82+
...
83+
84+
def value_or(self, default_value: NewValueType) -> ValueType:
85+
...
86+
87+
def unwrap(self) -> ValueType:
88+
...
89+
90+
def failure(self) -> NoReturn:
91+
...

returns/primitives/exceptions.pyi

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from returns.primitives.types import MonadType
4+
5+
6+
class UnwrapFailedError(Exception):
7+
def __init__(self, monad: MonadType) -> None:
8+
self.halted_monad = monad
9+
10+
11+
class ImmutableStateError(Exception):
12+
...

0 commit comments

Comments
 (0)