PK!_returns/__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, raise_exception from returns.io import IO, impure from returns.primitives.exceptions import UnwrapFailedError from returns.result import ( Failure, Result, Success, is_successful, pipeline, safe, ) __all__ = ( # noqa: Z410 # Functions: 'compose', 'raise_exception', # IO: 'IO', 'impure', # Result: 'is_successful', 'safe', 'pipeline', 'Failure', 'Result', 'Success', 'UnwrapFailedError', ) PK!{Eereturns/functions.py# -*- coding: utf-8 -*- 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!ӤZreturns/functions.pyi# -*- coding: utf-8 -*- from typing import Callable, NoReturn, TypeVar # Just aliases: _FirstType = TypeVar('_FirstType') _SecondType = TypeVar('_SecondType') _ThirdType = TypeVar('_ThirdType') def compose( first: Callable[[_FirstType], _SecondType], second: Callable[[_SecondType], _ThirdType], ) -> Callable[[_FirstType], _ThirdType]: ... def raise_exception(exception: Exception) -> NoReturn: ... PK!x returns/io.py# -*- coding: utf-8 -*- from functools import wraps from inspect import iscoroutinefunction from typing import TypeVar from returns.primitives.container import GenericContainerOneSlot _ValueType = TypeVar('_ValueType') class IO(GenericContainerOneSlot[_ValueType]): """ Explicit marker for impure function results. We call it "marker" since once it is marked, it cannot be unmarked. ``IO`` is also a container. But, it is different in a way that it cannot be unwrapped / rescued / fixed. There's no way to directly get its internal value. """ def map(self, function): # noqa: A003 """ Applies function to the inner value. Applies 'function' to the contents of the IO instance and returns a new IO object containing the result. 'function' should accept a single "normal" (non-container) argument and return a non-container result. """ return IO(function(self._inner_value)) def bind(self, function): """ Applies 'function' to the result of a previous calculation. 'function' should accept a single "normal" (non-container) argument and return IO type object. """ return function(self._inner_value) def impure(function): """ Decorator to mark function that it returns :py:class:`IO` container. Supports both async and regular functions. """ if iscoroutinefunction(function): async def decorator(*args, **kwargs): return IO(await function(*args, **kwargs)) else: def decorator(*args, **kwargs): return IO(function(*args, **kwargs)) return wraps(function)(decorator) PK!(nXXreturns/io.pyi# -*- coding: utf-8 -*- from typing import Callable, Coroutine, TypeVar, overload from typing_extensions import final from returns.primitives.container import GenericContainerOneSlot _ValueType = TypeVar('_ValueType') _NewValueType = TypeVar('_NewValueType') # Helpers: _FirstType = TypeVar('_FirstType') _SecondType = TypeVar('_SecondType') @final class IO(GenericContainerOneSlot[_ValueType]): _inner_value: _ValueType def __init__(self, inner_value: _ValueType) -> None: ... def map( # noqa: A003 self, function: Callable[[_ValueType], _NewValueType], ) -> 'IO[_NewValueType]': ... def bind( self, function: Callable[[_ValueType], 'IO[_NewValueType]'], ) -> 'IO[_NewValueType]': ... @overload # noqa: Z320 def impure( # type: ignore function: Callable[..., Coroutine[_FirstType, _SecondType, _NewValueType]], ) -> Callable[ ..., Coroutine[_FirstType, _SecondType, IO[_NewValueType]], ]: ... @overload def impure( function: Callable[..., _NewValueType], ) -> Callable[..., IO[_NewValueType]]: ... PK!uhreturns/primitives/__init__.py# -*- coding: utf-8 -*- PK!΂  returns/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 containers immutable.""" raise ImmutableStateError() def __delattr__(self, attr_name): # noqa: Z434 """Makes inner state of the containers 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. containers allow you to bind together a series of calculations while maintaining the context of that specific container. 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 containers 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 container. Works for containers that represent success. Is the opposite of :meth:`~rescue`. """ raise NotImplementedError() class GenericContainerOneSlot(Generic[_ValueType], Container): """ Base class for containers 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 containers with two typed slot. Use this type for generic inheritance only. Use :class:`~Container` as a general type for polymorphism. """ PK!V 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): ... 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!H4returns/result.py# -*- coding: utf-8 -*- from abc import ABCMeta, abstractmethod from functools import wraps from inspect import iscoroutinefunction 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.""" @abstractmethod def fix(self, function): # pragma: no cover """ Applies 'function' to the contents of the functor. And returns a new functor value. Works for containers 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 container. Works for containers 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 container or return a default.""" raise NotImplementedError() @abstractmethod def unwrap(self): # pragma: no cover """ Custom magic method to unwrap inner value from container. 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 container. This method is the opposite of :meth:`~unwrap`. """ raise NotImplementedError() 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-container) argument and return a non-container 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-container) 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 container.""" 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-container) argument and return a non-container 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-container) 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 container.""" return self._inner_value def failure(self): """Raises an exception, since it does not have an error inside.""" raise UnwrapFailedError(self) 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' container. 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) PK!returns/result.pyi# -*- coding: utf-8 -*- from abc import ABCMeta from typing import Any, Callable, Coroutine, NoReturn, TypeVar, Union, overload from typing_extensions import Literal, 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') # Just aliases: _FirstType = TypeVar('_FirstType') _SecondType = TypeVar('_SecondType') _ThirdType = TypeVar('_ThirdType') # Hacks for functions: _ReturnsContainerType = TypeVar( '_ReturnsContainerType', bound=Callable[..., Container], ) _ReturnsAsyncContainerType = TypeVar( '_ReturnsAsyncContainerType', bound=Callable[..., Coroutine[_FirstType, _SecondType, Container]], ) 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: ... @overload def is_successful(container: Success) -> Literal[True]: ... @overload def is_successful(container: Failure) -> Literal[False]: ... # 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[_ValueType, _ErrorType, _NewValueType]], ) -> Callable[ ..., Coroutine[_ValueType, _ErrorType, Result[_NewValueType, Exception]], ]: ... @overload def safe( function: Callable[..., _NewValueType], ) -> Callable[..., Result[_NewValueType, Exception]]: ... PK!YO{\[[returns/unsafe.py# -*- coding: utf-8 -*- def unsafe_perform_io(wrapped_in_io): """ Compatibility utility and escape mechanism from ``IO`` world. Just unwraps the internal value from :py:class:`IO ` container. Should be used with caution! Since it might be overused by tired developers. It is recommended to have only one place (module / file) in your program where you allow unsafe operations. We recommend to use ``import-linter`` to enforce this rule: - https://github.com/seddonym/import-linter """ return wrapped_in_io._inner_value # noqa: Z441 PK!h4returns/unsafe.pyi# -*- coding: utf-8 -*- from typing import TypeVar from returns.io import IO _ValueType = TypeVar('_ValueType') def unsafe_perform_io(wrapped_in_io: IO[_ValueType]) -> _ValueType: ... PK!q/  returns-0.7.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.7.0.dist-info/WHEEL A н#Z;/"d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8MB/^+k˃ZXm݆(M(m< ͬ؅Ѧdm@%ĥoz_,x+ { А2 L *j.&Ns@Pbᆎ_,+NZGд oݢy|KOuIos!C bu}y@NtܓwZZruvСM^OƳqO=[Հ4FO=ltlOI3}6 qt.RVB͸{kWz fg3$N;$fݜ_XUwq$ $͇3X%,$,4Kc *-o╡]9yG(^'&Ϊpi2>X@I-3ǯ~v[3 zi'}8W$ ߞ|u_'_M+;NS*8EFUndf~.s}f6PK!H<*Zreturns-0.7.0.dist-info/RECORDǶjyveMi2a!)R~W28xNvVnYWqٖ0? Z7_=g,VYjR}?7 S^¾ RԦ|5gjdꖕF[Z˷p6fOw}>OvKrY%_._85"V>$0Z*;FX1r#͉^\Ap>8UYcx[M!MQnf ;`5_alJXޗKaO c>?U2~Z&fqd  M-L o*< *~㺻n8*N`<= ʈ}uE}>QVŒF/$sk6v.& 3W.XSmxci&;]lYU(;FOw5S!4wȧy$2iN7!.4pv6A $6n}:++Nw5!JV mF*YxS 꼝}oX0ԟ c CP&/@ciV-I)l?!9{fͨ- (n zU?[m\:eLDnՑkO4Md^#[[LDNWW^13pׂT .‘GU+#ȷPK!_returns/__init__.pyPK!{Eereturns/functions.pyPK!ӤZ;returns/functions.pyiPK!x returns/io.pyPK!(nXXreturns/io.pyiPK!uhereturns/primitives/__init__.pyPK!΂  returns/primitives/container.pyPK!V  returns/primitives/container.pyiPK!B $returns/primitives/exceptions.pyPK!'@``!c&returns/primitives/exceptions.pyiPK!(returns/py.typedPK!H40(returns/result.pyPK!KDreturns/result.pyiPK!YO{\[['Ureturns/unsafe.pyPK!h4Wreturns/unsafe.pyiPK!q/  Xreturns-0.7.0.dist-info/LICENSEPK!HڽTU]returns-0.7.0.dist-info/WHEELPK!H2I w^returns-0.7.0.dist-info/METADATAPK!H<*Zjreturns-0.7.0.dist-info/RECORDPK/5n