PK!1returns/__init__.py# -*- coding: utf-8 -*- """ We define public API here. So, later our code can be used like so: .. code:: python import returns result: returns.Result[int, str] See: https://github.com/dry-python/returns/issues/73 """ from returns.functions import compose, pipeline, safe from returns.primitives.exceptions import UnwrapFailedError from returns.result import Failure, Result, Success __all__ = ( # noqa: Z410 'compose', 'safe', 'pipeline', 'Failure', 'Result', 'Success', 'UnwrapFailedError', ) PK!VH5 returns/functions.py# -*- coding: utf-8 -*- from functools import wraps from inspect import iscoroutinefunction from returns.primitives.exceptions import UnwrapFailedError from returns.result import Failure, Success def is_successful(container): """ Determins if a container was successful or not. We treat container that raise ``UnwrapFailedError`` on ``.unwrap()`` not successful. """ try: container.unwrap() except UnwrapFailedError: return False else: return True def safe(function): # noqa: C901 """ Decorator to covert exception throwing function to 'Result' monad. Show be used with care, since it only catches 'Exception' subclasses. It does not catch 'BaseException' subclasses. Supports both async and regular functions. """ if iscoroutinefunction(function): async def decorator(*args, **kwargs): try: return Success(await function(*args, **kwargs)) except Exception as exc: return Failure(exc) else: def decorator(*args, **kwargs): try: return Success(function(*args, **kwargs)) except Exception as exc: return Failure(exc) return wraps(function)(decorator) def pipeline(function): # noqa: C901 """ Decorator to enable 'do-notation' context. Should be used for series of computations that rely on ``.unwrap`` method. Supports both async and regular functions. """ if iscoroutinefunction(function): async def decorator(*args, **kwargs): try: return await function(*args, **kwargs) except UnwrapFailedError as exc: return exc.halted_container else: def decorator(*args, **kwargs): try: return function(*args, **kwargs) except UnwrapFailedError as exc: return exc.halted_container return wraps(function)(decorator) def compose(first, second): """ Allows function composition. Works as: ``second . first`` You can read it as "second after first". We can only compose functions with one argument and one return. """ return lambda argument: second(first(argument)) def raise_exception(exception): """ Helper function to raise exceptions as a function. That's how it can be used: .. code:: python from returns.functions import raise_exception # Some operation result: user: Failure[UserDoesNotExistError] # Here we unwrap internal exception and raise it: user.fix(raise_exception) See: https://github.com/dry-python/returns/issues/56 """ raise exception PK!0++returns/functions.pyi# -*- coding: utf-8 -*- from typing import Callable, Coroutine, NoReturn, TypeVar, overload from typing_extensions import Literal from returns.primitives.container import Container from returns.result import Failure, Result, Success # Main types: _ContainerType = TypeVar('_ContainerType', bound=Container) _ReturnType = TypeVar('_ReturnType') # Just aliases: _FirstType = TypeVar('_FirstType') _SecondType = TypeVar('_SecondType') _ThirdType = TypeVar('_ThirdType') # Hacks: _ReturnsContainerType = TypeVar( '_ReturnsContainerType', bound=Callable[..., Container], ) _ReturnsAsyncContainerType = TypeVar( '_ReturnsAsyncContainerType', bound=Callable[..., Coroutine[_FirstType, _SecondType, Container]], ) @overload def is_successful(container: Success) -> Literal[True]: ... @overload def is_successful(container: Failure) -> Literal[False]: ... @overload def is_successful(container: _ContainerType) -> bool: ... # Typing decorators is not an easy task, see: # https://github.com/python/mypy/issues/3157 @overload def pipeline( function: _ReturnsAsyncContainerType, ) -> _ReturnsAsyncContainerType: ... @overload def pipeline(function: _ReturnsContainerType) -> _ReturnsContainerType: ... @overload # noqa: Z320 def safe( # type: ignore function: Callable[..., Coroutine[_FirstType, _SecondType, _ReturnType]], ) -> Callable[ ..., Coroutine[_FirstType, _SecondType, Result[_ReturnType, Exception]], ]: ... @overload def safe( function: Callable[..., _ReturnType], ) -> Callable[..., Result[_ReturnType, Exception]]: ... def compose( first: Callable[[_FirstType], _SecondType], second: Callable[[_SecondType], _ThirdType], ) -> Callable[[_FirstType], _ThirdType]: ... def raise_exception(exception: Exception) -> NoReturn: ... PK!uhreturns/primitives/__init__.py# -*- coding: utf-8 -*- PK!NO\Dreturns/primitives/container.py# -*- coding: utf-8 -*- from abc import ABCMeta, abstractmethod from typing import Generic, TypeVar from returns.primitives.exceptions import ImmutableStateError _ValueType = TypeVar('_ValueType') _ErrorType = TypeVar('_ErrorType') class _BaseContainer(object, metaclass=ABCMeta): """Utility class to provide all needed magic methods to the contest.""" __slots__ = ('_inner_value',) def __init__(self, inner_value): """ Wraps the given value in the Container. 'value' is any arbitrary value of any type including functions. """ object.__setattr__(self, '_inner_value', inner_value) def __setattr__(self, attr_name, attr_value): """Makes inner state of the monads immutable.""" raise ImmutableStateError() def __delattr__(self, attr_name): # noqa: Z434 """Makes inner state of the monads immutable.""" raise ImmutableStateError() def __str__(self): """Converts to string.""" return '{0}: {1}'.format( self.__class__.__qualname__, str(self._inner_value), ) def __eq__(self, other): """Used to compare two 'Container' objects.""" if not isinstance(other, _BaseContainer): return False if type(self) != type(other): return False return self._inner_value == other._inner_value # noqa: Z441 class Container(_BaseContainer, metaclass=ABCMeta): """ Represents a "context" in which calculations can be executed. You won't create 'Container' instances directly. Instead, sub-classes implement specific contexts. Monads allow you to bind together a series of calculations while maintaining the context of that specific monad. This is an abstract class with the API declaration. Attributes: _inner_value: Wrapped internal immutable state. """ @abstractmethod # noqa: A003 def map(self, function): # pragma: no cover """ Applies 'function' to the contents of the functor. And returns a new functor value. Works for monads that represent success. Is the opposite of :meth:`~fix`. """ raise NotImplementedError() @abstractmethod def bind(self, function): # pragma: no cover """ Applies 'function' to the result of a previous calculation. And returns a new monad. Works for monads that represent success. Is the opposite of :meth:`~rescue`. """ raise NotImplementedError() @abstractmethod def fix(self, function): # pragma: no cover """ Applies 'function' to the contents of the functor. And returns a new functor value. Works for monads that represent failure. Is the opposite of :meth:`~map`. """ raise NotImplementedError() @abstractmethod def rescue(self, function): # pragma: no cover """ Applies 'function' to the result of a previous calculation. And returns a new monad. Works for monads that represent failure. Is the opposite of :meth:`~bind`. """ raise NotImplementedError() @abstractmethod def value_or(self, default_value): # pragma: no cover """Forces to unwrap value from monad or return a default.""" raise NotImplementedError() @abstractmethod def unwrap(self): # pragma: no cover """ Custom magic method to unwrap inner value from monad. Should be redefined for ones that actually have values. And for ones that raise an exception for no values. This method is the opposite of :meth:`~failure`. """ raise NotImplementedError() @abstractmethod def failure(self): # pragma: no cover """ Custom magic method to unwrap inner value from the failed monad. This method is the opposite of :meth:`~unwrap`. """ raise NotImplementedError() class GenericContainerOneSlot(Generic[_ValueType], Container): """ Base class for monads with one typed slot. Use this type for generic inheritance only. Use :class:`~Container` as a general type for polymorphism. """ class GenericContainerTwoSlots(Generic[_ValueType, _ErrorType], Container): """ Base class for monads with two typed slot. Use this type for generic inheritance only. Use :class:`~Container` as a general type for polymorphism. """ PK!ϖ returns/primitives/container.pyi# -*- coding: utf-8 -*- from abc import ABCMeta, abstractmethod from typing import Any, Generic, NoReturn, TypeVar _ValueType = TypeVar('_ValueType') _ErrorType = TypeVar('_ErrorType') class _BaseContainer(object, metaclass=ABCMeta): __slots__ = ('_inner_value',) _inner_value: Any def __setattr__(self, attr_name: str, attr_value) -> NoReturn: ... def __delattr__(self, attr_name: str) -> NoReturn: # noqa: Z434 ... def __str__(self) -> str: ... def __eq__(self, other) -> bool: ... class Container(_BaseContainer, metaclass=ABCMeta): @abstractmethod # noqa: A003 def map(self, function): ... @abstractmethod def bind(self, function): ... @abstractmethod def fix(self, function): ... @abstractmethod def rescue(self, function): ... @abstractmethod def value_or(self, default_value): ... @abstractmethod def unwrap(self): ... @abstractmethod def failure(self): ... class GenericContainerOneSlot( Generic[_ValueType], Container, metaclass=ABCMeta, ): ... class GenericContainerTwoSlots( Generic[_ValueType, _ErrorType], Container, metaclass=ABCMeta, ): ... PK!B returns/primitives/exceptions.py# -*- coding: utf-8 -*- class UnwrapFailedError(Exception): """Raised when a container can not be unwrapped into a meaningful value.""" def __init__(self, container): """ Saves halted container in the inner state. So, this container can later be unpacked from this exception and used as a regular value. """ super().__init__() self.halted_container = container class ImmutableStateError(Exception): """Raised when a container is forced to be mutated.""" PK!'@``!returns/primitives/exceptions.pyi# -*- coding: utf-8 -*- from typing import TypeVar from returns.primitives.container import Container _ContainerType = TypeVar('_ContainerType', bound=Container) class UnwrapFailedError(Exception): def __init__(self, container: _ContainerType) -> None: self.halted_container = container class ImmutableStateError(Exception): ... PK!returns/py.typedPK!Q+returns/result.py# -*- coding: utf-8 -*- from abc import ABCMeta from typing import Any, TypeVar from returns.primitives.container import GenericContainerTwoSlots from returns.primitives.exceptions import UnwrapFailedError _ValueType = TypeVar('_ValueType') _ErrorType = TypeVar('_ErrorType') class Result( GenericContainerTwoSlots[_ValueType, _ErrorType], metaclass=ABCMeta, ): """Base class for Failure and Success.""" class Failure(Result[Any, _ErrorType]): """ Represents a calculation which has failed. It should contain an error code or message. To help with readability you may alternatively use the alias 'Failure'. """ def map(self, function): # noqa: A003 """Returns the 'Failure' instance that was used to call the method.""" return self def bind(self, function): """Returns the 'Failure' instance that was used to call the method.""" return self def fix(self, function): """ Applies function to the inner value. Applies 'function' to the contents of the 'Success' instance and returns a new 'Success' object containing the result. 'function' should accept a single "normal" (non-monad) argument and return a non-monad result. """ return Success(function(self._inner_value)) def rescue(self, function): """ Applies 'function' to the result of a previous calculation. 'function' should accept a single "normal" (non-monad) argument and return Result a 'Failure' or 'Success' type object. """ return function(self._inner_value) def value_or(self, default_value): """Returns the value if we deal with 'Success' or default otherwise.""" return default_value def unwrap(self): """Raises an exception, since it does not have a value inside.""" if isinstance(self._inner_value, Exception): raise UnwrapFailedError(self) from self._inner_value raise UnwrapFailedError(self) def failure(self): """Unwraps inner error value from failed monad.""" return self._inner_value class Success(Result[_ValueType, Any]): """ Represents a calculation which has succeeded and contains the result. To help with readability you may alternatively use the alias 'Success'. """ def map(self, function): # noqa: A003 """ Applies function to the inner value. Applies 'function' to the contents of the 'Success' instance and returns a new 'Success' object containing the result. 'function' should accept a single "normal" (non-monad) argument and return a non-monad result. """ return Success(function(self._inner_value)) def bind(self, function): """ Applies 'function' to the result of a previous calculation. 'function' should accept a single "normal" (non-monad) argument and return Result a 'Failure' or 'Success' type object. """ return function(self._inner_value) def fix(self, function): """Returns the 'Success' instance that was used to call the method.""" return self def rescue(self, function): """Returns the 'Success' instance that was used to call the method.""" return self def value_or(self, default_value): """Returns the value if we deal with 'Success' or default otherwise.""" return self._inner_value def unwrap(self): """Returns the unwrapped value from the inside of this monad.""" return self._inner_value def failure(self): """Raises an exception, since it does not have an error inside.""" raise UnwrapFailedError(self) PK!} returns/result.pyi# -*- coding: utf-8 -*- from abc import ABCMeta from typing import Any, Callable, NoReturn, TypeVar, Union from typing_extensions import final from returns.primitives.container import Container, GenericContainerTwoSlots _ContainerType = TypeVar('_ContainerType', bound=Container) _ResultType = TypeVar('_ResultType', bound='Result') # Regular type vars, work correctly: _ValueType = TypeVar('_ValueType') _NewValueType = TypeVar('_NewValueType') _ErrorType = TypeVar('_ErrorType') class Result( GenericContainerTwoSlots[_ValueType, _ErrorType], metaclass=ABCMeta, ): _inner_value: Union[_ValueType, _ErrorType] def map( # noqa: A003 self, function: Callable[[_ValueType], _NewValueType], ) -> Union['Success[_NewValueType]', 'Result[_ValueType, _ErrorType]']: ... def bind( self, function: Callable[[_ValueType], _ContainerType], ) -> Union[_ContainerType, 'Result[_ValueType, _ErrorType]']: ... def fix( self, function: Callable[[_ErrorType], _NewValueType], ) -> Union['Success[_ValueType]', 'Success[_NewValueType]']: ... def rescue( self, function: Callable[[_ErrorType], _ContainerType], ) -> Union[_ContainerType, 'Result[_ValueType, _ErrorType]']: ... def value_or( self, default_value: _NewValueType, ) -> Union[_NewValueType, _ValueType]: ... def unwrap(self) -> Union[NoReturn, _ValueType]: ... def failure(self) -> Union[NoReturn, _ErrorType]: ... @final class Failure(Result[Any, _ErrorType]): _inner_value: _ErrorType def __init__(self, inner_value: _ErrorType) -> None: ... def map(self, function) -> 'Failure[_ErrorType]': # noqa: A003 ... def bind(self, function) -> 'Failure[_ErrorType]': ... def fix( self, function: Callable[[_ErrorType], _NewValueType], ) -> 'Success[_NewValueType]': ... def rescue( self, function: Callable[[_ErrorType], _ContainerType], ) -> _ContainerType: ... def value_or(self, default_value: _NewValueType) -> _NewValueType: ... def unwrap(self) -> NoReturn: ... def failure(self) -> _ErrorType: ... @final class Success(Result[_ValueType, Any]): _inner_value: _ValueType def __init__(self, inner_value: _ValueType) -> None: ... def map( # noqa: A003 self, function: Callable[[_ValueType], _NewValueType], ) -> 'Success[_NewValueType]': ... def bind( self, function: Callable[[_ValueType], _ContainerType], ) -> _ContainerType: ... def fix(self, function) -> 'Success[_ValueType]': ... def rescue(self, function) -> 'Success[_ValueType]': ... def value_or(self, default_value: _NewValueType) -> _ValueType: ... def unwrap(self) -> _ValueType: ... def failure(self) -> NoReturn: ... PK!q/  returns-0.6.0.dist-info/LICENSECopyright 2016-2019 dry-python organization Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. PK!HڽTUreturns-0.6.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!H t returns-0.6.0.dist-info/METADATAXko_1N>2$ʻBjMbNB`ȑ41ag!)ʏ$nYi=+/So:m9) s5V.atQ年DWJԦbQgrWX\.l(|]t(d 'j/zkR.al}&qc4J~RXNTp诪IgXffiepQs|ϐ Ȥy]C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=ɑ>\/5uӐ)^ L_\ +mӑo-$.Rn&@}>IYG)kJݽ 1E5W{_g%,8 Vs]ya'fjݼe0%)Ȫ싢8cf)zbUm-x ߞ"!H׹xq+e[7XRP!G0)*B.*j;|n"2E 9% ߚWuW_,4u7u1,apRzi )JD[3;OPK!1returns/__init__.pyPK!VH5 Greturns/functions.pyPK!0++# returns/functions.pyiPK!uhreturns/primitives/__init__.pyPK!NO\Dreturns/primitives/container.pyPK!ϖ &returns/primitives/container.pyiPK!B +returns/primitives/exceptions.pyPK!'@``!=.returns/primitives/exceptions.pyiPK!/returns/py.typedPK!Q+ 0returns/result.pyPK!} >returns/result.pyiPK!q/  Jreturns-0.6.0.dist-info/LICENSEPK!HڽTU,Preturns-0.6.0.dist-info/WHEELPK!H t Preturns-0.6.0.dist-info/METADATAPK!HEcZreturns-0.6.0.dist-info/RECORDPK9]