Interfaces¶
We provide a lot of generic interfaces to write our bundled and your own custom types.
These interfaces are designed:
To be subclassed
To provide abstract methods to implement in your own types
To enforce correctness on final types
To attach critical laws to be checked
We use Higher Kinded Types to define abstract methods.
Reading about interfaces will be the most useful if you plan to create your own containers.
General information¶
All the non-specific interfaces (e.g. MappableN, BindableN, ApplicativeN) can have Nth types, at the maximum of three possible types. What does this mean?
MappableN interface,
for example, can have one, two or three possible types. See the example below:
>>> from typing_extensions import Never
>>> from returns.interfaces.mappable import (
... MappableN, Mappable1, Mappable2, Mappable3,
... )
>>> one_type: MappableN[int, Never, Never]
>>> two_types: MappableN[int, str, Never]
>>> three_types: MappableN[int, str, bool]
>>> # We have a shortcut for each amount of arguments to reduce the boilerplate
>>> one_type: Mappable1[int]
>>> two_types: Mappable2[int, str]
>>> three_type: Mappable3[int, str, bool]
Note
Useful links before you start here:
Naming convention¶
We follow a very specific naming convention in our interface names.
If interface does not depend on the number of types
it works with and is always the same, we name it as is.
For example, Equable is always the same
and does not depend on the number of type arguments.
We use adjectives to name these interfaces.
Secondly, if interface depends on the number of type arguments,
it is named with N suffix in the end.
It would always have numeric aliases for each number of arguments supported.
For example, MappableN, Mappable1, Mappable2, and Mappable3.
The last criteria we have to decided on naming is
“whether this interface always the same or it can have slight variations”?
That’s why we have ResultLikeN and ResultBasedN interfaces.
Because ResultBasedN has two extra methods compared to ResultLikeN.
We use Like suffix for interfaces that describes some similar types.
We use Based suffix for interfaces that describe almost concrete types.
Laws¶
Some interfaces define its laws as values. These laws can be viewed as tests that are attached to the specific interface.
We are able to check them of any type that implements a given interfaces with laws by our own check_all_laws hypothesis plugin.
In this docs we are going to describe each general interface and its laws.
Mappable¶
Something is considered mappable if we can map it using a function,
generally map is a method that accepts a function.
An example in this library is Maybe,
that implements the Mappable interface:
>>> from returns.maybe import Maybe, Some
>>> def can_be_mapped(string: str) -> str:
... return string + '!'
>>> maybe_str: Maybe[str] = Some('example')
>>> assert maybe_str.map(can_be_mapped) == Some('example!')
MappableN interface helps us to
create our own mappable container like Maybe.
>>> from typing import Callable, TypeVar
>>> from returns.interfaces.mappable import Mappable1
>>> from returns.primitives.hkt import SupportsKind1
>>> from returns.primitives.container import BaseContainer
>>> _NumberType = TypeVar('_NumberType')
>>> _NewNumberType = TypeVar('_NewNumberType')
>>> class Number(
... BaseContainer,
... SupportsKind1['Number', _NumberType],
... Mappable1[_NumberType],
... ):
... def __init__(self, inner_value: _NumberType) -> None:
... super().__init__(inner_value)
...
... def map( # This method is required by Mappable
... self,
... function: Callable[[_NumberType], _NewNumberType]
... ) -> 'Number[_NewNumberType]':
... return Number(function(self._inner_value))
With our Number mappable class we can compose easily math functions with it.
>>> def my_math_function(number: int) -> int:
... return number - 1
>>> number: Number[int] = Number(-41)
>>> assert number.map(my_math_function).map(abs) == Number(42)
Laws¶
To make sure your Mappable implementation is right,
you can apply the Mappable laws on it to test.
Identity Law: When we pass the identity function to themapmethod, theMappableinstance has to be the same, unchanged.
>>> from returns.functions import identity
>>> mappable_number: Number[int] = Number(1)
>>> assert mappable_number.map(identity) == Number(1)
Associative Law: Given two functions,xandy, calling the map method withxfunction and after that calling withyfunction must have the same result if we compose them together.
>>> from returns.functions import compose
>>> def add_one(number: int) -> int:
... return number + 1
>>> def multiply_by_ten(number: int) -> int:
... return number * 10
>>> mappable_number: Number[int] = Number(9)
>>> assert mappable_number.map(
... add_one,
... ).map(
... multiply_by_ten,
... ) == mappable_number.map(
... compose(add_one, multiply_by_ten),
... )
Bindable¶
Bindable is something that we can bind with a function. Like
Maybe, so
BindableN interface will help
us to create our custom bindable.
>>> from typing import Callable, TypeVar
>>> from returns.interfaces.bindable import Bindable1
>>> from returns.primitives.hkt import SupportsKind1, Kind1, dekind
>>> from returns.primitives.container import BaseContainer
>>> _NumberType = TypeVar('_NumberType')
>>> _NewNumberType = TypeVar('_NewNumberType')
>>> class Number(
... BaseContainer,
... SupportsKind1['Number', _NumberType],
... Bindable1[_NumberType],
... ):
... def __init__(self, inner_value: _NumberType) -> None:
... super().__init__(inner_value)
...
... def bind( # This method is required by Bindable
... self,
... function: Kind1[
... 'Number',
... Callable[[_NumberType], 'Number[_NewNumberType]'],
... ],
... ) -> 'Number[_NewNumberType]':
... return dekind(function(self._inner_value))
And here’s how we can use it:
>>> def double(arg: int) -> Number[int]:
... return Number(arg * 2)
>>> number = Number(5)
>>> assert number.bind(double) == Number(10)
Applicative¶
Something is considered applicative
if it is a functor already and,
moreover, we can apply another container to it
and construct a new value with .from_value method.
An example in this library is Maybe,
that implements the Mappable and Applicative interfaces:
>>> from returns.maybe import Maybe, Some
>>> maybe_str = Maybe.from_value('example')
>>> maybe_func = Maybe.from_value(len) # we use function as a value!
>>> assert maybe_str.apply(maybe_func) == Some(7)
As you see, apply takes a container with a function inside
and applies it to the current value inside the container.
This way we really execute Maybe.from_value(len('example')).
ApplicativeN which is a subtype of
MappableN interface helps us to
create our own applicative container like Maybe.
>>> from typing import Callable, TypeVar
>>> from returns.interfaces.applicative import Applicative1
>>> from returns.primitives.hkt import SupportsKind1, Kind1, dekind
>>> from returns.primitives.container import BaseContainer
>>> _NumberType = TypeVar('_NumberType')
>>> _NewNumberType = TypeVar('_NewNumberType')
>>> class Number(
... BaseContainer,
... SupportsKind1['Number', _NumberType],
... Applicative1[_NumberType],
... ):
... def __init__(self, inner_value: _NumberType) -> None:
... super().__init__(inner_value)
...
... def map( # This method is required by Mappable
... self,
... function: Callable[[_NumberType], _NewNumberType]
... ) -> 'Number[_NewNumberType]':
... return Number(function(self._inner_value))
...
... def apply( # This method is required by Applicative
... self,
... container: Kind1[
... 'Number',
... Callable[[_NumberType], _NewNumberType],
... ],
... ) -> 'Number[_NewNumberType]':
... return Number.from_value(
... dekind(container._inner_value(self._inner_value)),
... )
...
... @classmethod
... def from_value( # This method is required by Applicative
... cls,
... inner_value: _NewNumberType,
... ) -> 'Number[_NewNumberType]':
... return Number(inner_value)
With our Number mappable class we can compose easily math functions with it.
>>> def my_math_function(number: int) -> int:
... return number - 1
>>> number = Number(3)
>>> number_function = Number.from_value(my_math_function)
>>> assert number.apply(number_function) == Number(2)
Laws¶
To make sure your Applicative implementation is right,
you can apply the Applicative laws on it to test.
Identity Law: When we pass an applicative instance with wrapped identity function to theapplymethod, theApplicativehas to be the same, unchanged.
>>> from returns.functions import identity
>>> applicative_number: Number[int] = Number(1)
>>> assert applicative_number.apply(
... applicative_number.from_value(identity),
... ) == Number(1)
Interchange Law: We can start our composition with both raw value and a function.
>>> def function(arg: int) -> int:
... return arg + 1
>>> raw_value = 5
>>> assert Number.from_value(raw_value).apply(
... Number.from_value(function),
... ) == Number.from_value(function).apply(
... Number.from_value(lambda inner: inner(raw_value)),
... )
Homomorphism Law: The homomorphism law says that applying a wrapped function to a wrapped value is the same as applying the function to the value in the normal way and then using.from_valueon the result.
>>> def function(arg: int) -> int:
... return arg + 1
>>> raw_value = 5
>>> assert Number.from_value(
... function(raw_value),
... ) == Number.from_value(raw_value).apply(
... Number.from_value(function),
... )
Composition Law: Applying two functions twice is the same as applying their composition once.
>>> from returns.functions import compose
>>> def first(arg: int) -> int:
... return arg * 2
>>> def second(arg: int) -> int:
... return arg + 1
>>> instance = Number(5)
>>> assert instance.apply(
... Number.from_value(compose(first, second)),
... ) == instance.apply(
... Number.from_value(first),
... ).apply(
... Number.from_value(second),
... )
Plus all laws from MappableN interface.
Container¶
ContainerN is a central piece of our library.
It is an interface that combines
ApplicativeN
and
BindableN together.
So, in other words: Container is an Apllicative that you can bind!
>>> from typing import Callable, TypeVar
>>> from returns.interfaces.container import Container1
>>> from returns.primitives.hkt import SupportsKind1, Kind1, dekind
>>> from returns.primitives.container import BaseContainer
>>> _NumberType = TypeVar('_NumberType')
>>> _NewNumberType = TypeVar('_NewNumberType')
>>> class Number(
... BaseContainer,
... SupportsKind1['Number', _NumberType],
... Container1[_NumberType],
... ):
... def __init__(self, inner_value: _NumberType) -> None:
... super().__init__(inner_value)
...
... def map( # This method is required by Mappable
... self,
... function: Callable[[_NumberType], _NewNumberType]
... ) -> 'Number[_NewNumberType]':
... return Number(function(self._inner_value))
...
... def bind( # This method is required by Bindable
... self,
... function: Kind1[
... 'Number',
... Callable[[_NumberType], 'Number[_NewNumberType]'],
... ],
... ) -> 'Number[_NewNumberType]':
... return dekind(function(self._inner_value))
...
... def apply( # This method is required by Applicative
... self,
... container: Kind1[
... 'Number',
... Callable[[_NumberType], _NewNumberType],
... ],
... ) -> 'Number[_NewNumberType]':
... return Number.from_value(
... container._inner_value(self._inner_value),
... )
...
... @classmethod
... def from_value( # This method is required by Applicative
... cls,
... inner_value: _NewNumberType,
... ) -> 'Number[_NewNumberType]':
... return Number(inner_value)
This code gives us an opportunity to use Number
with map, apply, and bind as we already did in the examples above.
Laws¶
To make sure other people will be able to use your implementation, it should respect three new laws.
Left Identity: If webinda function to our bindable must have to be the same result as passing the value directly to the function.
>>> def can_be_bound(value: int) -> Number[int]:
... return Number(value)
>>> assert Number.from_value(5).bind(can_be_bound) == can_be_bound(5)
Right Identity: If we pass the bindable constructor throughbindmust have to be the same result as instantiating the bindable on our own.
>>> number = Number(2)
>>> assert number.bind(Number) == Number(2)
Associative Law: Given two functions,xandy, calling the bind method withxfunction and after that calling withyfunction must have the same result if we bind with a function that passes the value toxand then bind the result withy.
>>> def minus_one(arg: int) -> Number[int]:
... return Number(arg - 1)
>>> def half(arg: int) -> Number[int]:
... return Number(arg // 2)
>>> number = Number(9)
>>> assert number.bind(minus_one).bind(half) == number.bind(
... lambda value: minus_one(value).bind(half),
... )
Plus all laws from MappableN and ApplicativeN interfaces.
More!¶
We have way more interfaces with different features! We have covered all of them in the technical docs.
So, use them to enforce type-safety of your own containers.
Specific interfaces¶
We also have a whole package of different specific interfaces
that will help you to create containers based on our internal types,
like Result.
FAQ¶
Why do you have general and specific interfaces?¶
We have .interfaces.* types that can be applied to any possible type.
There’s nothing they know about other types or returns package.
We also have a special .interfaces.specific package
where we have types that know about other types in returns.
For example, MappableN from .interfaces
only knows about .map method. It does not require anything else.
But, ResultLikeN from .interfaces.specific.result
does require to have .bind_result method
which relies on our Result type.
That’s the only difference. Build your own types with any of those interfaces.
Why some interfaces do not have type alias for 1 or 2 type arguments?¶
Some types like
ResultLikeN
do not have type aliases for one type argument in a form of ResultLike1.
Why does Mappable1 exists and ResultLike1 does not?
Because Mappable1 does make sense.
But, ResultLike1 requires at least two (value and error) types to exist.
The same applies for ReaderLike1 and ReaderResultLike1
and ReaderResultLike2.
We don’t support type aliases for types that won’t make sense.
What’s the difference between MappableN and BindableN?¶
While MappableN you have to pass a pure function, like:
>>> def can_be_mapped(string: str) -> str:
... return string
with Bindable we have to pass a function that returns another container:
>>> from returns.maybe import Maybe
>>> def can_be_bound(string: str) -> Maybe[str]:
... return Some(string + '!')
The main difference is the return type.
The consequence of this is big!
BindableN allows to change the container type.
While MappableN cannot do that.
So, Some.bind(function) can be evaluated to both Some and Nothing.
While Some.map(function) will always stay as Some.
What is the difference between ResultLikeN and ResultBasedN?¶
ResultLikeN is just an intention of having a result
(e.g. FutureResult),
it’s not the result yet. While ResultBasedN is a concrete result
(e.g. IOResult),
it has the desired result value.
Because of this difference between them is why we can’t unwrap a ResultLikeN
container, it does not have the real result yet.
See the example below using FutureResult to get a IOResult:
>>> import anyio
>>> from returns.future import FutureResult
>>> from returns.interfaces.specific.future_result import FutureResultBasedN
>>> from returns.interfaces.specific.ioresult import (
... IOResultBasedN,
... IOResultLikeN,
... )
>>> from returns.interfaces.specific.result import ResultLikeN, ResultBasedN
>>> from returns.io import IOSuccess, IOResult
>>> from returns.result import Success, Result
>>> async def coro(arg: int) -> Result[int, str]:
... return Success(arg + 1)
>>> # `result_like` does not have the result we want (Result[int, str])
>>> # it's just the intention of having one,
>>> # we have to await it to get the real result
>>> result_like: FutureResult[int, str] = FutureResult(coro(1))
>>> assert isinstance(result_like, FutureResultBasedN)
>>> assert isinstance(result_like, IOResultLikeN)
>>> assert isinstance(result_like, ResultLikeN)
>>> # `anyio.run(...)` will await our coroutine and give the real result to us
>>> result: IOResult[int, str] = anyio.run(result_like.awaitable)
>>> assert isinstance(result, IOResultBasedN)
>>> assert isinstance(result, ResultLikeN)
>>> # Compare it with the real result:
>>> assert isinstance(Success(1), ResultBasedN)
Note
The same difference applies to all *ResultLikeN vs *ResultBasedN
(e.g. IOResultLikeN
and IOResultBasedN)
API Reference¶
Overview¶
Here’s a full overview of all our interfaces:
classDiagram
AltableN <|-- BiMappableN
ApplicativeN <|-- ContainerN
AwaitableFutureN <|-- FutureBasedN
BindableN <|-- ContainerN
CallableReader2 <|-- ReaderBased2
CallableReader3 <|-- ReaderFutureResultBasedN
CallableReader3 <|-- ReaderIOResultBasedN
CallableReader3 <|-- ReaderResultBasedN
ContainerN <|-- FailableN
ContainerN <|-- IOLikeN
ContainerN <|-- ReaderLike2
ContainerN <|-- ReaderLike3
Contextable <|-- CallableReader2
Contextable <|-- CallableReader3
DiverseFailableN <|-- ResultLikeN
Equable <|-- IOBasedN
Equable <|-- MaybeBasedN
Equable <|-- UnwrappableResult
FailableN <|-- DiverseFailableN
FailableN <|-- SingleFailableN
FutureBasedN <|-- FutureResultBasedN
FutureLikeN <|-- FutureBasedN
FutureLikeN <|-- FutureResultLikeN
FutureResultLikeN <|-- FutureResultBasedN
FutureResultLikeN <|-- ReaderFutureResultLikeN
Generic <|-- AltableN
Generic <|-- AwaitableFutureN
Generic <|-- BindableN
Generic <|-- Contextable
Generic <|-- LashableN
Generic <|-- MappableN
Generic <|-- Unwrappable
IOBasedN <|-- IOResultBasedN
IOLikeN <|-- FutureLikeN
IOLikeN <|-- IOBasedN
IOLikeN <|-- IOResultLikeN
IOResultLikeN <|-- FutureResultLikeN
IOResultLikeN <|-- IOResultBasedN
IOResultLikeN <|-- ReaderIOResultLikeN
LashableN <|-- FailableN
LawSpecDef <|-- _DiverseFailableLawSpec
LawSpecDef <|-- _FailableLawSpec
LawSpecDef <|-- _LawSpec
LawSpecDef <|-- _SingleFailableLawSpec
Lawful <|-- AltableN
Lawful <|-- ApplicativeN
Lawful <|-- ContainerN
Lawful <|-- DiverseFailableN
Lawful <|-- FailableN
Lawful <|-- MappableN
Lawful <|-- MaybeLikeN
Lawful <|-- ReaderBased2
Lawful <|-- ReaderFutureResultBasedN
Lawful <|-- ReaderIOResultBasedN
Lawful <|-- ReaderResultBasedN
MappableN <|-- ApplicativeN
MappableN <|-- BiMappableN
MaybeLikeN <|-- MaybeBasedN
ReaderFutureResultLikeN <|-- ReaderFutureResultBasedN
ReaderIOResultLikeN <|-- ReaderFutureResultLikeN
ReaderIOResultLikeN <|-- ReaderIOResultBasedN
ReaderLike2 <|-- CallableReader2
ReaderLike3 <|-- CallableReader3
ReaderLike3 <|-- ReaderResultLikeN
ReaderResultLikeN <|-- ReaderIOResultLikeN
ReaderResultLikeN <|-- ReaderResultBasedN
ResultLikeN <|-- IOResultLikeN
ResultLikeN <|-- ReaderResultLikeN
ResultLikeN <|-- UnwrappableResult
SingleFailableN <|-- MaybeLikeN
SwappableN <|-- DiverseFailableN
Unwrappable <|-- MaybeBasedN
Unwrappable <|-- UnwrappableResult
UnwrappableResult <|-- IOResultBasedN
UnwrappableResult <|-- ResultBasedN
Let’s review it one by one.
Equable¶
classDiagram
LawSpecDef <|-- _LawSpec
Lawful <|-- Equable
- final class _LawSpec[source]¶
Bases:
LawSpecDefEquality laws.
Description: https://bit.ly/34D40iT
- static reflexive_law(first)[source]¶
Value should be equal to itself.
- Parameters:
first (
TypeVar(_EqualType, bound= Equable))- Return type:
None
- class Equable[source]¶
-
Interface for types that can be compared with real values.
Not all types can, because some don’t have the value at a time: -
Futurehas to be awaited to get the value -Readerhas to be called to get the value
Mappable¶
classDiagram
Generic <|-- MappableN
LawSpecDef <|-- _LawSpec
Lawful <|-- MappableN
- final class _LawSpec[source]¶
Bases:
LawSpecDefMappable or functor laws.
https://en.wikibooks.org/wiki/Haskell/The_Functor_class#The_functor_laws
- static identity_law(mappable)[source]¶
Mapping identity over a value must return the value unchanged.
- Parameters:
mappable (
MappableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])- Return type:
None
- static associative_law(mappable, first, second)[source]¶
Mapping twice or mapping a composition is the same thing.
- Parameters:
mappable (
MappableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])first (
Callable[[TypeVar(_FirstType)],TypeVar(_NewType1)])second (
Callable[[TypeVar(_NewType1)],TypeVar(_NewType2)])
- Return type:
None
- class MappableN[source]¶
Bases:
Lawful[MappableN[_FirstType, _SecondType, _ThirdType]],Generic[_FirstType,_SecondType,_ThirdType]Allows to chain wrapped values in containers with regular functions.
Behaves like a functor.
-
_laws:
ClassVar[Sequence[Law]] = (<returns.primitives.laws.Law1 object>, <returns.primitives.laws.Law3 object>)¶ Some classes and interfaces might have laws, some might not have any.
- abstractmethod map(function)[source]¶
Allows to run a pure function over a container.
- Parameters:
self (
TypeVar(_MappableType, bound= MappableN))function (
Callable[[TypeVar(_FirstType)],TypeVar(_UpdatedType)])
- Return type:
KindN[TypeVar(_MappableType, bound= MappableN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
-
_laws:
- Mappable1¶
Type alias for kinds with one type argument.
alias of
MappableN[_FirstType,Never,Never]
Bindable¶
classDiagram
Generic <|-- BindableN
- class BindableN[source]¶
Bases:
Generic[_FirstType,_SecondType,_ThirdType]Represents a “context” in which calculations can be executed.
Bindableallows you to bind together a series of calculations while maintaining the context of that specific container.In contrast to
returns.interfaces.lashable.LashableN, works with the first type argument.- abstractmethod bind(function)[source]¶
Applies ‘function’ to the result of a previous calculation.
And returns a new container.
- Parameters:
self (
TypeVar(_BindableType, bound= BindableN))function (
Callable[[TypeVar(_FirstType)],KindN[TypeVar(_BindableType, bound= BindableN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
KindN[TypeVar(_BindableType, bound= BindableN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- Bindable1¶
Type alias for kinds with one type argument.
alias of
BindableN[_FirstType,Never,Never]
Applicative¶
classDiagram
LawSpecDef <|-- _LawSpec
Lawful <|-- ApplicativeN
MappableN <|-- ApplicativeN
- final class _LawSpec[source]¶
Bases:
LawSpecDefApplicative mappable laws.
Definition: https://bit.ly/3hC8F8E Discussion: https://bit.ly/3jffz3L
- static identity_law(container)[source]¶
Identity law.
If we apply wrapped
identityfunction to a container, nothing happens.- Parameters:
container (
ApplicativeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])- Return type:
None
- static interchange_law(raw_value, container, function)[source]¶
Interchange law.
Basically we check that we can start our composition with both
raw_valueandfunction.Great explanation: https://stackoverflow.com/q/27285918/4842742
- Parameters:
raw_value (
TypeVar(_FirstType))container (
ApplicativeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],TypeVar(_NewType1)])
- Return type:
None
- static homomorphism_law(raw_value, container, function)[source]¶
Homomorphism law.
The homomorphism law says that applying a wrapped function to a wrapped value is the same as applying the function to the value in the normal way and then using
.from_valueon the result.- Parameters:
raw_value (
TypeVar(_FirstType))container (
ApplicativeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],TypeVar(_NewType1)])
- Return type:
None
- static composition_law(container, first, second)[source]¶
Composition law.
Applying two functions twice is the same as applying their composition once.
- Parameters:
container (
ApplicativeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])first (
Callable[[TypeVar(_FirstType)],TypeVar(_NewType1)])second (
Callable[[TypeVar(_NewType1)],TypeVar(_NewType2)])
- Return type:
None
- class ApplicativeN[source]¶
Bases:
MappableN[_FirstType,_SecondType,_ThirdType],Lawful[ApplicativeN[_FirstType, _SecondType, _ThirdType]]Allows to create unit containers from raw values and to apply wrapped funcs.
See also
-
_laws:
ClassVar[Sequence[Law]] = (<returns.primitives.laws.Law1 object>, <returns.primitives.laws.Law3 object>, <returns.primitives.laws.Law3 object>, <returns.primitives.laws.Law3 object>)¶ Some classes and interfaces might have laws, some might not have any.
- abstractmethod apply(container)[source]¶
Allows to apply a wrapped function over a container.
- Parameters:
self (
TypeVar(_ApplicativeType, bound= ApplicativeN))container (
KindN[TypeVar(_ApplicativeType, bound= ApplicativeN),Callable[[TypeVar(_FirstType)],TypeVar(_UpdatedType)],TypeVar(_SecondType),TypeVar(_ThirdType)])
- Return type:
KindN[TypeVar(_ApplicativeType, bound= ApplicativeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
-
_laws:
- Applicative1¶
Type alias for kinds with one type argument.
alias of
ApplicativeN[_FirstType,Never,Never]
- Applicative2¶
Type alias for kinds with two type arguments.
alias of
ApplicativeN[_FirstType,_SecondType,Never]
- Applicative3¶
Type alias for kinds with three type arguments.
alias of
ApplicativeN[_FirstType,_SecondType,_ThirdType]
Altable¶
classDiagram
Generic <|-- AltableN
LawSpecDef <|-- _LawSpec
Lawful <|-- AltableN
- final class _LawSpec[source]¶
Bases:
LawSpecDefMappable or functor laws.
https://en.wikibooks.org/wiki/Haskell/The_Functor_class#The_functor_laws
- static identity_law(altable)[source]¶
Mapping identity over a value must return the value unchanged.
- Parameters:
altable (
AltableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])- Return type:
None
- static associative_law(altable, first, second)[source]¶
Mapping twice or mapping a composition is the same thing.
- Parameters:
altable (
AltableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])first (
Callable[[TypeVar(_SecondType)],TypeVar(_NewType1)])second (
Callable[[TypeVar(_NewType1)],TypeVar(_NewType2)])
- Return type:
None
- class AltableN[source]¶
Bases:
Lawful[AltableN[_FirstType, _SecondType, _ThirdType]],Generic[_FirstType,_SecondType,_ThirdType]Modifies the second type argument with a pure function.
-
_laws:
ClassVar[Sequence[Law]] = (<returns.primitives.laws.Law1 object>, <returns.primitives.laws.Law3 object>)¶ Some classes and interfaces might have laws, some might not have any.
- abstractmethod alt(function)[source]¶
Allows to run a pure function over a container.
- Parameters:
self (
TypeVar(_AltableType, bound= AltableN))function (
Callable[[TypeVar(_SecondType)],TypeVar(_UpdatedType)])
- Return type:
KindN[TypeVar(_AltableType, bound= AltableN),TypeVar(_FirstType),TypeVar(_UpdatedType),TypeVar(_ThirdType)]
-
_laws:
BiMappable¶
classDiagram
AltableN <|-- BiMappableN
MappableN <|-- BiMappableN
- class BiMappableN[source]¶
Bases:
MappableN[_FirstType,_SecondType,_ThirdType],AltableN[_FirstType,_SecondType,_ThirdType]Allows to change both types of a container at the same time.
Uses
.mapto change first type and.altto change second type.
- BiMappable2¶
Type alias for kinds with two type arguments.
alias of
BiMappableN[_FirstType,_SecondType,Never]
- BiMappable3¶
Type alias for kinds with three type arguments.
alias of
BiMappableN[_FirstType,_SecondType,_ThirdType]
Swappable¶
classDiagram
BiMappableN <|-- SwappableN
LawSpecDef <|-- _LawSpec
Lawful <|-- SwappableN
- final class _LawSpec[source]¶
Bases:
LawSpecDefLaws for
SwappableNtype.- static double_swap_law(container)[source]¶
Swapping container twice.
It ensure that we get the initial value back. In other words, swapping twice does nothing.
- Parameters:
container (
SwappableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])- Return type:
None
- class SwappableN[source]¶
Bases:
BiMappableN[_FirstType,_SecondType,_ThirdType],Lawful[SwappableN[_FirstType, _SecondType, _ThirdType]]Interface that allows swapping first and second type values.
- Swappable2¶
Type alias for kinds with two type arguments.
alias of
SwappableN[_FirstType,_SecondType,Never]
- Swappable3¶
Type alias for kinds with three type arguments.
alias of
SwappableN[_FirstType,_SecondType,_ThirdType]
Lashable¶
classDiagram
Generic <|-- LashableN
- class LashableN[source]¶
Bases:
Generic[_FirstType,_SecondType,_ThirdType]Represents a “context” in which calculations can be executed.
Rescueableallows you to bind together a series of calculations while maintaining the context of that specific container.In contrast to
returns.interfaces.bindable.BinbdaleN, works with the second type value.- abstractmethod lash(function)[source]¶
Applies ‘function’ to the result of a previous calculation.
And returns a new container.
- Parameters:
self (
TypeVar(_LashableType, bound= LashableN))function (
Callable[[TypeVar(_SecondType)],KindN[TypeVar(_LashableType, bound= LashableN),TypeVar(_FirstType),TypeVar(_UpdatedType),TypeVar(_ThirdType)]])
- Return type:
KindN[TypeVar(_LashableType, bound= LashableN),TypeVar(_FirstType),TypeVar(_UpdatedType),TypeVar(_ThirdType)]
Unwrappable¶
classDiagram
Generic <|-- Unwrappable
- class Unwrappable[source]¶
Bases:
Generic[_FirstType,_SecondType]Represents containers that can unwrap and return its wrapped value.
There are no aliases or
UnwrappableNforUnwrappableinterface. Because it always uses two and just two types.Not all types can be
Unwrappablebecause we do require to raiseUnwrapFailedErrorif unwrap is not possible.- abstractmethod unwrap()[source]¶
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.
Note
As a part of the contract, failed
unwrapcalls must raisereturns.primitives.exceptions.UnwrapFailedErrorexception.This method is the opposite of
failure().- Parameters:
self (
TypeVar(_UnwrappableType, bound= Unwrappable))- Return type:
TypeVar(_FirstType)
- abstractmethod failure()[source]¶
Custom magic method to unwrap inner value from the failed container.
Note
As a part of the contract, failed
failurecalls must raisereturns.primitives.exceptions.UnwrapFailedErrorexception.This method is the opposite of
unwrap().- Parameters:
self (
TypeVar(_UnwrappableType, bound= Unwrappable))- Return type:
TypeVar(_SecondType)
Container¶
classDiagram
ApplicativeN <|-- ContainerN
BindableN <|-- ContainerN
LawSpecDef <|-- _LawSpec
Lawful <|-- ContainerN
- final class _LawSpec[source]¶
Bases:
LawSpecDefContainer laws.
Definition: https://wiki.haskell.org/Monad_laws Good explanation: https://bit.ly/2Qsi5re
- static left_identity_law(raw_value, container, function)[source]¶
Left identity.
The first law states that if we take a value, put it in a default context with return and then feed it to a function by using
bind, it’s the same as just taking the value and applying the function to it.- Parameters:
raw_value (
TypeVar(_FirstType))container (
ContainerN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],KindN[ContainerN,TypeVar(_NewType1),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
None
- static right_identity_law(container)[source]¶
Right identity.
The second law states that if we have a container value and we use
bindto feed it to.from_value, the result is our original container value.- Parameters:
container (
ContainerN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])- Return type:
None
- static associative_law(container, first, second)[source]¶
Associativity law.
The final monad law says that when we have a chain of container functions applications with
bind, it shouldn’t matter how they’re nested.- Parameters:
container (
ContainerN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])first (
Callable[[TypeVar(_FirstType)],KindN[ContainerN,TypeVar(_NewType1),TypeVar(_SecondType),TypeVar(_ThirdType)]])second (
Callable[[TypeVar(_NewType1)],KindN[ContainerN,TypeVar(_NewType2),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
None
- class ContainerN[source]¶
Bases:
ApplicativeN[_FirstType,_SecondType,_ThirdType],BindableN[_FirstType,_SecondType,_ThirdType],Lawful[ContainerN[_FirstType, _SecondType, _ThirdType]]Handy alias for types with
.bind,.map, and.applymethods.Should be a base class for almost any containers you write.
See also
- Container1¶
Type alias for kinds with one type argument.
alias of
ContainerN[_FirstType,Never,Never]
- Container2¶
Type alias for kinds with two type arguments.
alias of
ContainerN[_FirstType,_SecondType,Never]
- Container3¶
Type alias for kinds with three type arguments.
alias of
ContainerN[_FirstType,_SecondType,_ThirdType]
Failable¶
classDiagram
ContainerN <|-- FailableN
FailableN <|-- DiverseFailableN
FailableN <|-- SingleFailableN
LashableN <|-- FailableN
LawSpecDef <|-- _DiverseFailableLawSpec
LawSpecDef <|-- _FailableLawSpec
LawSpecDef <|-- _SingleFailableLawSpec
Lawful <|-- DiverseFailableN
Lawful <|-- FailableN
SwappableN <|-- DiverseFailableN
- final class _FailableLawSpec[source]¶
Bases:
LawSpecDefFailable laws.
We need to be sure that
.lashwon’t lash success types.
- class FailableN[source]¶
Bases:
ContainerN[_FirstType,_SecondType,_ThirdType],LashableN[_FirstType,_SecondType,_ThirdType],Lawful[FailableN[_FirstType, _SecondType, _ThirdType]]Base type for types that can fail.
It is a raw type and should not be used directly. Use
SingleFailableNandDiverseFailableNinstead.
- Failable2¶
Type alias for kinds with two type arguments.
alias of
FailableN[_FirstType,_SecondType,Never]
- Failable3¶
Type alias for kinds with three type arguments.
alias of
FailableN[_FirstType,_SecondType,_ThirdType]
- final class _SingleFailableLawSpec[source]¶
Bases:
LawSpecDefSingle Failable laws.
We need to be sure that
.mapand.bindworks correctly foremptyproperty.- static map_short_circuit_law(container, function)[source]¶
Ensures that you cannot map from the empty property.
- Parameters:
container (
SingleFailableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],TypeVar(_NewFirstType)])
- Return type:
None
- static bind_short_circuit_law(container, function)[source]¶
Ensures that you cannot bind from the empty property.
- Parameters:
container (
SingleFailableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],KindN[SingleFailableN,TypeVar(_NewFirstType),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
None
- static apply_short_circuit_law(container, function)[source]¶
Ensures that you cannot apply from the empty property.
- Parameters:
container (
SingleFailableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],TypeVar(_NewFirstType)])
- Return type:
None
- class SingleFailableN[source]¶
Bases:
FailableN[_FirstType,_SecondType,_ThirdType]Base type for types that have just only one failed value.
Like
Maybetypes where the only failed value isNothing.-
_laws:
ClassVar[Sequence[Law]] = (<returns.primitives.laws.Law2 object>, <returns.primitives.laws.Law2 object>, <returns.primitives.laws.Law2 object>)¶ Some classes and interfaces might have laws, some might not have any.
- abstract property empty: SingleFailableN[_FirstType, _SecondType, _ThirdType]¶
This property represents the failed value. :param self: :type self:
TypeVar(_SingleFailableType, bound= SingleFailableN)
-
_laws:
- SingleFailable2¶
Type alias for kinds with two types arguments.
alias of
SingleFailableN[_FirstType,_SecondType,Never]
- SingleFailable3¶
Type alias for kinds with three type arguments.
alias of
SingleFailableN[_FirstType,_SecondType,_ThirdType]
- final class _DiverseFailableLawSpec[source]¶
Bases:
LawSpecDefDiverse Failable laws.
We need to be sure that
.map,.bind,.applyand.altworks correctly for both success and failure types.- static map_short_circuit_law(raw_value, container, function)[source]¶
Ensures that you cannot map a failure.
- Parameters:
raw_value (
TypeVar(_SecondType))container (
DiverseFailableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],TypeVar(_NewFirstType)])
- Return type:
None
- static bind_short_circuit_law(raw_value, container, function)[source]¶
Ensures that you cannot bind a failure.
See: https://wiki.haskell.org/Typeclassopedia#MonadFail
- Parameters:
raw_value (
TypeVar(_SecondType))container (
DiverseFailableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],KindN[DiverseFailableN,TypeVar(_NewFirstType),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
None
- static apply_short_circuit_law(raw_value, container, function)[source]¶
Ensures that you cannot apply a failure.
- Parameters:
raw_value (
TypeVar(_SecondType))container (
DiverseFailableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],TypeVar(_NewFirstType)])
- Return type:
None
- static alt_short_circuit_law(raw_value, container, function)[source]¶
Ensures that you cannot alt a success.
- Parameters:
raw_value (
TypeVar(_SecondType))container (
DiverseFailableN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_SecondType)],TypeVar(_NewFirstType)])
- Return type:
None
- class DiverseFailableN[source]¶
Bases:
FailableN[_FirstType,_SecondType,_ThirdType],SwappableN[_FirstType,_SecondType,_ThirdType],Lawful[DiverseFailableN[_FirstType, _SecondType, _ThirdType]]Base type for types that have any failed value.
Like
Resulttypes.-
_laws:
ClassVar[Sequence[Law]] = (<returns.primitives.laws.Law3 object>, <returns.primitives.laws.Law3 object>, <returns.primitives.laws.Law3 object>, <returns.primitives.laws.Law3 object>)¶ Some classes and interfaces might have laws, some might not have any.
- abstractmethod classmethod from_failure(inner_value)[source]¶
Unit method to create new containers from any raw value.
- Parameters:
inner_value (
TypeVar(_UpdatedType))- Return type:
KindN[TypeVar(_DiverseFailableType, bound= DiverseFailableN),TypeVar(_FirstType),TypeVar(_UpdatedType),TypeVar(_ThirdType)]
-
_laws:
- DiverseFailable2¶
Type alias for kinds with two type arguments.
alias of
DiverseFailableN[_FirstType,_SecondType,Never]
- DiverseFailable3¶
Type alias for kinds with three type arguments.
alias of
DiverseFailableN[_FirstType,_SecondType,_ThirdType]
Maybe specific¶
classDiagram
Equable <|-- MaybeBasedN
LawSpecDef <|-- _LawSpec
Lawful <|-- MaybeLikeN
MaybeLikeN <|-- MaybeBasedN
SingleFailableN <|-- MaybeLikeN
Unwrappable <|-- MaybeBasedN
- final class _LawSpec[source]¶
Bases:
LawSpecDefMaybe laws.
We need to be sure that
.map,.bind,.bind_optional, and.lashworks correctly for both successful and failed types.- static map_short_circuit_law(container, function)[source]¶
Ensures that you cannot map from failures.
- Parameters:
container (
MaybeLikeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],TypeVar(_NewType1)])
- Return type:
None
- static bind_short_circuit_law(container, function)[source]¶
Ensures that you cannot bind from failures.
- Parameters:
container (
MaybeLikeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],KindN[MaybeLikeN,TypeVar(_NewType1),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
None
- static bind_optional_short_circuit_law(container, function)[source]¶
Ensures that you cannot bind from failures.
- Parameters:
container (
MaybeLikeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],Optional[TypeVar(_NewType1)]])
- Return type:
None
- static lash_short_circuit_law(raw_value, container, function)[source]¶
Ensures that you cannot lash a success.
- Parameters:
raw_value (
TypeVar(_FirstType))container (
MaybeLikeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_SecondType)],KindN[MaybeLikeN,TypeVar(_FirstType),TypeVar(_NewType1),TypeVar(_ThirdType)]])
- Return type:
None
- static unit_structure_law(container, function)[source]¶
Ensures
Noneis treated specially.- Parameters:
container (
MaybeLikeN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])function (
Callable[[TypeVar(_FirstType)],None])
- Return type:
None
- class MaybeLikeN[source]¶
Bases:
SingleFailableN[_FirstType,_SecondType,_ThirdType],Lawful[MaybeLikeN[_FirstType, _SecondType, _ThirdType]]Type for values that do look like a
Maybe.For example,
RequiresContextMaybeshould be created from this interface. Cannot be unwrapped or compared.-
_laws:
ClassVar[Sequence[Law]] = (<returns.primitives.laws.Law2 object>, <returns.primitives.laws.Law2 object>, <returns.primitives.laws.Law2 object>, <returns.primitives.laws.Law3 object>, <returns.primitives.laws.Law2 object>)¶ Some classes and interfaces might have laws, some might not have any.
- abstractmethod bind_optional(function)[source]¶
Binds a function that returns
Optionalvalues.- Parameters:
self (
TypeVar(_MaybeLikeType, bound= MaybeLikeN))function (
Callable[[TypeVar(_FirstType)],Optional[TypeVar(_UpdatedType)]])
- Return type:
KindN[TypeVar(_MaybeLikeType, bound= MaybeLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
-
_laws:
- MaybeLike2¶
Type alias for kinds with two type arguments.
alias of
MaybeLikeN[_FirstType,_SecondType,Never]
- MaybeLike3¶
Type alias for kinds with three type arguments.
alias of
MaybeLikeN[_FirstType,_SecondType,_ThirdType]
- class MaybeBasedN[source]¶
Bases:
MaybeLikeN[_FirstType,_SecondType,_ThirdType],Unwrappable[_FirstType,None],EquableConcrete interface for
Maybetype.Can be unwrapped and compared.
- MaybeBased2¶
Type alias for kinds with two type arguments.
alias of
MaybeBasedN[_FirstType,_SecondType,Never]
- MaybeBased3¶
Type alias for kinds with three type arguments.
alias of
MaybeBasedN[_FirstType,_SecondType,_ThirdType]
Result specific¶
classDiagram
DiverseFailableN <|-- ResultLikeN
Equable <|-- UnwrappableResult
ResultLikeN <|-- UnwrappableResult
Unwrappable <|-- UnwrappableResult
UnwrappableResult <|-- ResultBasedN
An interface that represents a pure computation result.
For impure result see
returns.interfaces.specific.ioresult.IOResultLikeN type.
- class ResultLikeN[source]¶
Bases:
DiverseFailableN[_FirstType,_SecondType,_ThirdType]Base types for types that looks like
Resultbut cannot be unwrapped.Like
RequiresContextResultorFutureResult.
- ResultLike2¶
Type alias for kinds with two type arguments.
alias of
ResultLikeN[_FirstType,_SecondType,Never]
- ResultLike3¶
Type alias for kinds with three type arguments.
alias of
ResultLikeN[_FirstType,_SecondType,_ThirdType]
- class UnwrappableResult[source]¶
Bases:
ResultLikeN[_FirstType,_SecondType,_ThirdType],Unwrappable[_FirstUnwrappableType,_SecondUnwrappableType],EquableIntermediate type with 5 type arguments that represents unwrappable result.
It is a raw type and should not be used directly. Use
ResultBasedNandIOResultBasedNinstead.
- class ResultBasedN[source]¶
Bases:
UnwrappableResult[_FirstType,_SecondType,_ThirdType,_FirstType,_SecondType]Base type for real
Resulttypes.Can be unwrapped.
- ResultBased2¶
Type alias for kinds with two type arguments.
alias of
ResultBasedN[_FirstType,_SecondType,Never]
- ResultBased3¶
Type alias for kinds with three type arguments.
alias of
ResultBasedN[_FirstType,_SecondType,_ThirdType]
IO specific¶
classDiagram
ContainerN <|-- IOLikeN
Equable <|-- IOBasedN
IOLikeN <|-- IOBasedN
- class IOLikeN[source]¶
Bases:
ContainerN[_FirstType,_SecondType,_ThirdType]Represents interface for types that looks like fearless
IO.This type means that
IOcannot fail. Like random numbers, date, etc. Don’t use this type forIOthat can. Instead, usereturns.interfaces.specific.ioresult.IOResultBasedNtype.
- IOLike2¶
Type alias for kinds with two type arguments.
alias of
IOLikeN[_FirstType,_SecondType,Never]
- IOLike3¶
Type alias for kinds with three type arguments.
alias of
IOLikeN[_FirstType,_SecondType,_ThirdType]
- class IOBasedN[source]¶
Bases:
IOLikeN[_FirstType,_SecondType,_ThirdType],EquableRepresents the base interface for types that do fearless
IO.This type means that
IOcannot fail. Like random numbers, date, etc. Don’t use this type forIOthat can. Instead, usereturns.interfaces.specific.ioresult.IOResultBasedNtype.This interface also supports direct comparison of two values. While
IOLikeNis different. It can be lazy and cannot be compared.
IOResult specific¶
classDiagram
IOBasedN <|-- IOResultBasedN
IOLikeN <|-- IOResultLikeN
IOResultLikeN <|-- IOResultBasedN
ResultLikeN <|-- IOResultLikeN
UnwrappableResult <|-- IOResultBasedN
An interface for types that do IO and can fail.
It is a base interface for both sync and async IO stacks.
- class IOResultLikeN[source]¶
Bases:
IOLikeN[_FirstType,_SecondType,_ThirdType],ResultLikeN[_FirstType,_SecondType,_ThirdType]Base type for types that look like
IOResultbut cannot be unwrapped.Like
FutureResultorRequiresContextIOResult.- abstractmethod bind_ioresult(function)[source]¶
Runs
IOResultreturning function over a container.- Parameters:
self (
TypeVar(_IOResultLikeType, bound= IOResultLikeN))function (
Callable[[TypeVar(_FirstType)],IOResult[TypeVar(_UpdatedType),TypeVar(_SecondType)]])
- Return type:
KindN[TypeVar(_IOResultLikeType, bound= IOResultLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod compose_result(function)[source]¶
Allows to compose the underlying
Resultwith a function.- Parameters:
- Return type:
KindN[TypeVar(_IOResultLikeType, bound= IOResultLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- IOResultLike2¶
Type alias for kinds with two type arguments.
alias of
IOResultLikeN[_FirstType,_SecondType,Never]
- IOResultLike3¶
Type alias for kinds with three type arguments.
alias of
IOResultLikeN[_FirstType,_SecondType,_ThirdType]
- class IOResultBasedN[source]¶
Bases:
IOResultLikeN[_FirstType,_SecondType,_ThirdType],IOBasedN[_FirstType,_SecondType,_ThirdType],UnwrappableResult[_FirstType,_SecondType,_ThirdType,IO[_FirstType],IO[_SecondType]]Base type for real
IOResulttypes.Can be unwrapped.
- IOResultBased2¶
Type alias for kinds with two type arguments.
alias of
IOResultBasedN[_FirstType,_SecondType,Never]
- IOResultBased3¶
Type alias for kinds with three type arguments.
alias of
IOResultBasedN[_FirstType,_SecondType,_ThirdType]
Future specific¶
classDiagram
AwaitableFutureN <|-- FutureBasedN
FutureLikeN <|-- FutureBasedN
Generic <|-- AwaitableFutureN
IOLikeN <|-- FutureLikeN
Represents the base interfaces for types that do fearless async operations.
This type means that Future cannot fail.
Don’t use this type for async that can. Instead, use
returns.interfaces.specific.future_result.FutureResultBasedN type.
- class FutureLikeN[source]¶
Bases:
IOLikeN[_FirstType,_SecondType,_ThirdType]Base type for ones that does look like
Future.But at the time this is not a real
Futureand cannot be awaited.- abstractmethod bind_future(function)[source]¶
Allows to bind
Futurereturning function over a container.
- abstractmethod bind_async_future(function)[source]¶
Allows to bind async
Futurereturning function over container.
- abstractmethod bind_async(function)[source]¶
Binds async function returning the same type of container.
- Parameters:
self (
TypeVar(_FutureLikeType, bound= FutureLikeN))function (
Callable[[TypeVar(_FirstType)],Awaitable[KindN[TypeVar(_FutureLikeType, bound= FutureLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]]])
- Return type:
KindN[TypeVar(_FutureLikeType, bound= FutureLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod bind_awaitable(function)[source]¶
Allows to bind async function over container.
- Parameters:
self (
TypeVar(_FutureLikeType, bound= FutureLikeN))function (
Callable[[TypeVar(_FirstType)],Awaitable[TypeVar(_UpdatedType)]])
- Return type:
KindN[TypeVar(_FutureLikeType, bound= FutureLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- FutureLike1¶
Type alias for kinds with one type argument.
alias of
FutureLikeN[_FirstType,Never,Never]
- FutureLike2¶
Type alias for kinds with two type arguments.
alias of
FutureLikeN[_FirstType,_SecondType,Never]
- FutureLike3¶
Type alias for kinds with three type arguments.
alias of
FutureLikeN[_FirstType,_SecondType,_ThirdType]
- class AwaitableFutureN[source]¶
Bases:
Generic[_FirstType,_SecondType,_ThirdType]Type that provides the required API for
Futureto be async.Should not be used directly. Use
FutureBasedNinstead.
- AsyncFuture1¶
Type alias for kinds with one type argument.
alias of
AwaitableFutureN[_FirstType,Never,Never]
- AsyncFuture2¶
Type alias for kinds with two type arguments.
alias of
AwaitableFutureN[_FirstType,_SecondType,Never]
- AsyncFuture3¶
Type alias for kinds with three type arguments.
alias of
AwaitableFutureN[_FirstType,_SecondType,_ThirdType]
- class FutureBasedN[source]¶
Bases:
FutureLikeN[_FirstType,_SecondType,_ThirdType],AwaitableFutureN[_FirstType,_SecondType,_ThirdType]Base type for real
Futureobjects.They can be awaited.
- FutureBased1¶
Type alias for kinds with one type argument.
alias of
FutureBasedN[_FirstType,Never,Never]
- FutureBased2¶
Type alias for kinds with two type arguments.
alias of
FutureBasedN[_FirstType,_SecondType,Never]
- FutureBased3¶
Type alias for kinds with three type arguments.
alias of
FutureBasedN[_FirstType,_SecondType,_ThirdType]
FutureResult specific¶
classDiagram
FutureBasedN <|-- FutureResultBasedN
FutureLikeN <|-- FutureResultLikeN
FutureResultLikeN <|-- FutureResultBasedN
IOResultLikeN <|-- FutureResultLikeN
Represents the base interfaces for types that do fear-some async operations.
This type means that FutureResult can (and will!) fail with exceptions.
Use this type to mark that this specific async opetaion can fail.
- class FutureResultLikeN[source]¶
Bases:
FutureLikeN[_FirstType,_SecondType,_ThirdType],IOResultLikeN[_FirstType,_SecondType,_ThirdType]Base type for ones that does look like
FutureResult.But at the time this is not a real
Futureand cannot be awaited. It is also cannot be unwrapped, because it is not a realIOResult.- abstractmethod bind_future_result(function)[source]¶
Allows to bind
FutureResultfunctions over a container.- Parameters:
self (
TypeVar(_FutureResultLikeType, bound= FutureResultLikeN))function (
Callable[[TypeVar(_FirstType)],FutureResult[TypeVar(_UpdatedType),TypeVar(_SecondType)]])
- Return type:
KindN[TypeVar(_FutureResultLikeType, bound= FutureResultLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod bind_async_future_result(function)[source]¶
Allows to bind async
FutureResultfunctions over container.- Parameters:
self (
TypeVar(_FutureResultLikeType, bound= FutureResultLikeN))function (
Callable[[TypeVar(_FirstType)],Awaitable[FutureResult[TypeVar(_UpdatedType),TypeVar(_SecondType)]]])
- Return type:
KindN[TypeVar(_FutureResultLikeType, bound= FutureResultLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod classmethod from_failed_future(inner_value)[source]¶
Creates new container from a failed
Future.
- classmethod from_future_result(inner_value)[source]¶
Creates container from
FutureResultinstance.- Parameters:
inner_value (
FutureResult[TypeVar(_ValueType),TypeVar(_ErrorType)])- Return type:
KindN[Self,TypeVar(_ValueType),TypeVar(_ErrorType),TypeVar(_ThirdType)]
- FutureResultLike2¶
Type alias for kinds with two type arguments.
alias of
FutureResultLikeN[_FirstType,_SecondType,Never]
- FutureResultLike3¶
Type alias for kinds with three type arguments.
alias of
FutureResultLikeN[_FirstType,_SecondType,_ThirdType]
- class FutureResultBasedN[source]¶
Bases:
FutureBasedN[_FirstType,_SecondType,_ThirdType],FutureResultLikeN[_FirstType,_SecondType,_ThirdType]Base type for real
FutureResultobjects.They can be awaited. Still cannot be unwrapped.
- FutureResultBased2¶
Type alias for kinds with two type arguments.
alias of
FutureResultBasedN[_FirstType,_SecondType,Never]
- FutureResultBased3¶
Type alias for kinds with three type arguments.
alias of
FutureResultBasedN[_FirstType,_SecondType,_ThirdType]
Reader specific¶
classDiagram
CallableReader2 <|-- ReaderBased2
ContainerN <|-- ReaderLike2
ContainerN <|-- ReaderLike3
Contextable <|-- CallableReader2
Contextable <|-- CallableReader3
Generic <|-- Contextable
LawSpecDef <|-- _LawSpec
Lawful <|-- ReaderBased2
ReaderLike2 <|-- CallableReader2
ReaderLike3 <|-- CallableReader3
This module is special.
Reader does not produce ReaderLikeN interface as other containers.
Because Reader can be used with two or three type arguments:
- RequiresContext[value, env]
- RequiresContextResult[value, error, env]
Because the second type argument changes its meaning
based on the used KindN instance,
we need to have two separate interfaces for two separate use-cases:
- ReaderLike2 is used for types where the second type argument is env
- ReaderLike3 is used for types where the third type argument is env
We also have two methods and two poinfree helpers
for bind_context composition: one for each interface.
Furthermore, Reader cannot have ReaderLike1 type,
because we need both value and env types at all cases.
- class Contextable[source]¶
Bases:
Generic[_ValueType,_EnvType]Special type we use as a base one for all callble
Readerinstances.It only has a single method. And is a base type for every single one of them.
But, each
Readerdefines the return type differently. For example:Readerhas just_ReturnTypeReaderResulthasResult[_FirstType, _SecondType]ReaderIOResulthasIOResult[_FirstType, _SecondType]
And so on.
- class ReaderLike2[source]¶
Bases:
ContainerN[_FirstType,_SecondType,Never]Reader interface for
Kind2based types.It has two type arguments and treats the second type argument as env type.
- abstract property no_args: NoDeps¶
Is required to call
Readerwith no explicit arguments. :param self: :type self:TypeVar(_ReaderLike2Type, bound= ReaderLike2)
- abstractmethod bind_context(function)[source]¶
Allows to apply a wrapped function over a
Readercontainer.- Parameters:
self (
TypeVar(_ReaderLike2Type, bound= ReaderLike2))function (
Callable[[TypeVar(_FirstType)],RequiresContext[TypeVar(_UpdatedType),TypeVar(_SecondType)]])
- Return type:
KindN[TypeVar(_ReaderLike2Type, bound= ReaderLike2),TypeVar(_UpdatedType),TypeVar(_SecondType),Any]
- abstractmethod modify_env(function)[source]¶
Transforms the environment before calling the container.
- Parameters:
self (
TypeVar(_ReaderLike2Type, bound= ReaderLike2))function (
Callable[[TypeVar(_UpdatedType)],TypeVar(_SecondType)])
- Return type:
KindN[TypeVar(_ReaderLike2Type, bound= ReaderLike2),TypeVar(_FirstType),TypeVar(_UpdatedType),Any]
- abstractmethod classmethod ask()[source]¶
Returns the dependencies inside the container.
- Return type:
KindN[TypeVar(_ReaderLike2Type, bound= ReaderLike2),TypeVar(_SecondType),TypeVar(_SecondType),Any]
- abstractmethod classmethod from_context(inner_value)[source]¶
Unit method to create new containers from successful
Reader.- Parameters:
inner_value (
RequiresContext[TypeVar(_ValueType),TypeVar(_EnvType)])- Return type:
KindN[TypeVar(_ReaderLike2Type, bound= ReaderLike2),TypeVar(_ValueType),TypeVar(_EnvType),Any]
- class CallableReader2[source]¶
Bases:
ReaderLike2[_FirstType,_SecondType],Contextable[_ValueType,_EnvType]Intermediate interface for
ReaderLike2+__call__method.Has 4 type variables to type
Readerand__call__independently. Since, we don’t have any other fancy ways of doing it.Should not be used directly other than defining your own
Readerinterfaces.
- class ReaderLike3[source]¶
Bases:
ContainerN[_FirstType,_SecondType,_ThirdType]Reader interface for
Kind3based types.It has three type arguments and treats the third type argument as env type. The second type argument is not used here.
- abstract property no_args: Any¶
Is required to call
Readerwith no explicit arguments. :param self: :type self:TypeVar(_ReaderLike3Type, bound= ReaderLike3)
- abstractmethod bind_context(function)[source]¶
Allows to apply a wrapped function over a
Readercontainer.- Parameters:
self (
TypeVar(_ReaderLike3Type, bound= ReaderLike3))function (
Callable[[TypeVar(_FirstType)],RequiresContext[TypeVar(_UpdatedType),TypeVar(_ThirdType)]])
- Return type:
KindN[TypeVar(_ReaderLike3Type, bound= ReaderLike3),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod modify_env(function)[source]¶
Transforms the environment before calling the container.
- Parameters:
self (
TypeVar(_ReaderLike3Type, bound= ReaderLike3))function (
Callable[[TypeVar(_UpdatedType)],TypeVar(_ThirdType)])
- Return type:
KindN[TypeVar(_ReaderLike3Type, bound= ReaderLike3),TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_UpdatedType)]
- abstractmethod classmethod ask()[source]¶
Returns the dependencies inside the container.
- Return type:
KindN[TypeVar(_ReaderLike3Type, bound= ReaderLike3),TypeVar(_ThirdType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod classmethod from_context(inner_value)[source]¶
Unit method to create new containers from successful
Reader.- Parameters:
inner_value (
RequiresContext[TypeVar(_ValueType),TypeVar(_EnvType)])- Return type:
KindN[TypeVar(_ReaderLike3Type, bound= ReaderLike3),TypeVar(_ValueType),TypeVar(_SecondType),TypeVar(_EnvType)]
- class CallableReader3[source]¶
Bases:
ReaderLike3[_FirstType,_SecondType,_ThirdType],Contextable[_ValueType,_EnvType]Intermediate interface for
ReaderLike3+__call__method.Has 5 type variables to type
Readerand__call__independently. Since, we don’t have any other fancy ways of doing it.Should not be used directly other than defining your own
Readerinterfaces.
- final class _LawSpec[source]¶
Bases:
LawSpecDefConcrete laws for
ReaderBased2.See: https://github.com/haskell/mtl/pull/61/files
- static purity_law(container, env)[source]¶
Calling a
Readertwice has the same result with the same env.- Parameters:
container (
ReaderBased2[TypeVar(_FirstType),TypeVar(_SecondType)])env (
TypeVar(_SecondType))
- Return type:
None
- static asking_law(container, env)[source]¶
Asking for an env, always returns the env.
- Parameters:
container (
ReaderBased2[TypeVar(_FirstType),TypeVar(_SecondType)])env (
TypeVar(_SecondType))
- Return type:
None
- class ReaderBased2[source]¶
Bases:
CallableReader2[_FirstType,_SecondType,_FirstType,_SecondType],Lawful[ReaderBased2[_FirstType, _SecondType]]This interface is very specific to our
Readertype.The only thing that differs from
ReaderLike2is that we know the specific types for its__call__method.
ReaderResult specific¶
classDiagram
CallableReader3 <|-- ReaderResultBasedN
LawSpecDef <|-- _LawSpec
Lawful <|-- ReaderResultBasedN
ReaderLike3 <|-- ReaderResultLikeN
ReaderResultLikeN <|-- ReaderResultBasedN
ResultLikeN <|-- ReaderResultLikeN
- class ReaderResultLikeN[source]¶
Bases:
ReaderLike3[_FirstType,_SecondType,_ThirdType],ResultLikeN[_FirstType,_SecondType,_ThirdType]Base interface for all types that do look like
ReaderResultinstance.Cannot be called.
- abstractmethod bind_context_result(function)[source]¶
Binds a
ReaderResultreturning function over a container.- Parameters:
self (
TypeVar(_ReaderResultLikeType, bound= ReaderResultLikeN))function (
Callable[[TypeVar(_FirstType)],RequiresContextResult[TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
KindN[TypeVar(_ReaderResultLikeType, bound= ReaderResultLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod classmethod from_failed_context(inner_value)[source]¶
Unit method to create new containers from failed
Reader.- Parameters:
inner_value (
RequiresContext[TypeVar(_ErrorType),TypeVar(_EnvType)])- Return type:
KindN[TypeVar(_ReaderResultLikeType, bound= ReaderResultLikeN),TypeVar(_FirstType),TypeVar(_ErrorType),TypeVar(_EnvType)]
- abstractmethod classmethod from_result_context(inner_value)[source]¶
Unit method to create new containers from
ReaderResult.- Parameters:
inner_value (
RequiresContextResult[TypeVar(_ValueType),TypeVar(_ErrorType),TypeVar(_EnvType)])- Return type:
KindN[TypeVar(_ReaderResultLikeType, bound= ReaderResultLikeN),TypeVar(_ValueType),TypeVar(_ErrorType),TypeVar(_EnvType)]
- ReaderResultLike3¶
Type alias for kinds with three type arguments.
alias of
ReaderResultLikeN[_FirstType,_SecondType,_ThirdType]
- final class _LawSpec[source]¶
Bases:
LawSpecDefConcrete laws for
ReaderResulBasedN.See: https://github.com/haskell/mtl/pull/61/files
- static purity_law(container, env)[source]¶
Calling a
Readertwice has the same result with the same env.- Parameters:
container (
ReaderResultBasedN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])env (
TypeVar(_ThirdType))
- Return type:
None
- static asking_law(container, env)[source]¶
Asking for an env, always returns the env.
- Parameters:
container (
ReaderResultBasedN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])env (
TypeVar(_ThirdType))
- Return type:
None
- class ReaderResultBasedN[source]¶
Bases:
ReaderResultLikeN[_FirstType,_SecondType,_ThirdType],CallableReader3[_FirstType,_SecondType,_ThirdType,Result[_FirstType, _SecondType],_ThirdType],Lawful[ReaderResultBasedN[_FirstType, _SecondType, _ThirdType]]This interface is very specific to our
ReaderResulttype.The only thing that differs from
ReaderResultLikeNis that we know the specific types for its__call__method.In this case the return type of
__call__isResult.
- ReaderResultBased3¶
Type alias for kinds with three type arguments.
alias of
ReaderResultBasedN[_FirstType,_SecondType,_ThirdType]
ReaderIOResult specific¶
classDiagram
CallableReader3 <|-- ReaderIOResultBasedN
IOResultLikeN <|-- ReaderIOResultLikeN
LawSpecDef <|-- _LawSpec
Lawful <|-- ReaderIOResultBasedN
ReaderIOResultLikeN <|-- ReaderIOResultBasedN
ReaderResultLikeN <|-- ReaderIOResultLikeN
- class ReaderIOResultLikeN[source]¶
Bases:
ReaderResultLikeN[_FirstType,_SecondType,_ThirdType],IOResultLikeN[_FirstType,_SecondType,_ThirdType]Base interface for all types that do look like
ReaderIOResultinstance.Cannot be called.
- abstractmethod bind_context_ioresult(function)[source]¶
Binds a
ReaderIOResultreturning function over a container.- Parameters:
self (
TypeVar(_ReaderIOResultLikeType, bound= ReaderIOResultLikeN))function (
Callable[[TypeVar(_FirstType)],RequiresContextIOResult[TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
KindN[TypeVar(_ReaderIOResultLikeType, bound= ReaderIOResultLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod classmethod from_ioresult_context(inner_value)[source]¶
Unit method to create new containers from
ReaderIOResult.- Parameters:
inner_value (
RequiresContextIOResult[TypeVar(_ValueType),TypeVar(_ErrorType),TypeVar(_EnvType)])- Return type:
KindN[TypeVar(_ReaderIOResultLikeType, bound= ReaderIOResultLikeN),TypeVar(_ValueType),TypeVar(_ErrorType),TypeVar(_EnvType)]
- ReaderIOResultLike3¶
Type alias for kinds with three type arguments.
alias of
ReaderIOResultLikeN[_FirstType,_SecondType,_ThirdType]
- final class _LawSpec[source]¶
Bases:
LawSpecDefConcrete laws for
ReaderIOResultBasedN.See: https://github.com/haskell/mtl/pull/61/files
- static asking_law(container, env)[source]¶
Asking for an env, always returns the env.
- Parameters:
container (
ReaderIOResultBasedN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])env (
TypeVar(_ThirdType))
- Return type:
None
- class ReaderIOResultBasedN[source]¶
Bases:
ReaderIOResultLikeN[_FirstType,_SecondType,_ThirdType],CallableReader3[_FirstType,_SecondType,_ThirdType,IOResult[_FirstType, _SecondType],_ThirdType],Lawful[ReaderIOResultBasedN[_FirstType, _SecondType, _ThirdType]]This interface is very specific to our
ReaderIOResulttype.The only thing that differs from
ReaderIOResultLikeNis that we know the specific types for its__call__method.In this case the return type of
__call__isIOResult.
- ReaderIOResultBased3¶
Type alias for kinds with three type arguments.
alias of
ReaderIOResultBasedN[_FirstType,_SecondType,_ThirdType]
ReaderFutureResult specific¶
classDiagram
CallableReader3 <|-- ReaderFutureResultBasedN
FutureResultLikeN <|-- ReaderFutureResultLikeN
LawSpecDef <|-- _LawSpec
Lawful <|-- ReaderFutureResultBasedN
ReaderFutureResultLikeN <|-- ReaderFutureResultBasedN
ReaderIOResultLikeN <|-- ReaderFutureResultLikeN
- class ReaderFutureResultLikeN[source]¶
Bases:
ReaderIOResultLikeN[_FirstType,_SecondType,_ThirdType],FutureResultLikeN[_FirstType,_SecondType,_ThirdType]Interface for all types that do look like
ReaderFutureResultinstance.Cannot be called.
- abstractmethod bind_context_future_result(function)[source]¶
Bind a
ReaderFutureResultreturning function over a container.- Parameters:
self (
TypeVar(_ReaderFutureResultLikeType, bound= ReaderFutureResultLikeN))function (
Callable[[TypeVar(_FirstType)],RequiresContextFutureResult[TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]])
- Return type:
KindN[TypeVar(_ReaderFutureResultLikeType, bound= ReaderFutureResultLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod bind_async_context_future_result(function)[source]¶
Bind async
ReaderFutureResultfunction.- Parameters:
self (
TypeVar(_ReaderFutureResultLikeType, bound= ReaderFutureResultLikeN))function (
Callable[[TypeVar(_FirstType)],Awaitable[RequiresContextFutureResult[TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]]])
- Return type:
KindN[TypeVar(_ReaderFutureResultLikeType, bound= ReaderFutureResultLikeN),TypeVar(_UpdatedType),TypeVar(_SecondType),TypeVar(_ThirdType)]
- abstractmethod classmethod from_future_result_context(inner_value)[source]¶
Unit method to create new containers from
ReaderFutureResult.- Parameters:
inner_value (
RequiresContextFutureResult[TypeVar(_ValueType),TypeVar(_ErrorType),TypeVar(_EnvType)])- Return type:
KindN[TypeVar(_ReaderFutureResultLikeType, bound= ReaderFutureResultLikeN),TypeVar(_ValueType),TypeVar(_ErrorType),TypeVar(_EnvType)]
- ReaderFutureResultLike3¶
Type alias for kinds with three type arguments.
alias of
ReaderFutureResultLikeN[_FirstType,_SecondType,_ThirdType]
- final class _LawSpec[source]¶
Bases:
LawSpecDefConcrete laws for
ReaderFutureResultBasedN.See: https://github.com/haskell/mtl/pull/61/files
- static asking_law(container, env)[source]¶
Asking for an env, always returns the env.
- Parameters:
container (
ReaderFutureResultBasedN[TypeVar(_FirstType),TypeVar(_SecondType),TypeVar(_ThirdType)])env (
TypeVar(_ThirdType))
- Return type:
None
- class ReaderFutureResultBasedN[source]¶
Bases:
ReaderFutureResultLikeN[_FirstType,_SecondType,_ThirdType],CallableReader3[_FirstType,_SecondType,_ThirdType,FutureResult[_FirstType, _SecondType],_ThirdType],Lawful[ReaderFutureResultBasedN[_FirstType, _SecondType, _ThirdType]]This interface is very specific to our
ReaderFutureResulttype.The only thing that differs from
ReaderFutureResultLikeNis that we know the specific types for its__call__method.In this case the return type of
__call__isFutureResult.
- ReaderFutureResultBased3¶
Type alias for kinds with three type arguments.
alias of
ReaderFutureResultBasedN[_FirstType,_SecondType,_ThirdType]