|
17 | 17 | from returns._generated.futures import _future_result
|
18 | 18 | from returns._generated.iterable import iterable
|
19 | 19 | from returns.context import NoDeps
|
20 |
| -from returns.future import FutureResult |
| 20 | +from returns.future import Future, FutureResult |
21 | 21 | from returns.io import IO, IOResult
|
22 | 22 | from returns.primitives.container import BaseContainer
|
23 | 23 | from returns.primitives.types import Immutable
|
|
26 | 26 | if TYPE_CHECKING:
|
27 | 27 | from returns.context.requires_context import RequiresContext
|
28 | 28 | from returns.context.requires_context_result import RequiresContextResult
|
| 29 | + from returns.context.requires_context_ioresult import ( |
| 30 | + RequiresContextIOResult, |
| 31 | + ) |
29 | 32 |
|
30 | 33 | # Context:
|
31 | 34 | _EnvType = TypeVar('_EnvType', contravariant=True)
|
@@ -740,6 +743,90 @@ def from_ioresult(
|
740 | 743 | lambda _: FutureResult.from_ioresult(inner_value),
|
741 | 744 | )
|
742 | 745 |
|
| 746 | + @classmethod |
| 747 | + def from_future( |
| 748 | + cls, |
| 749 | + inner_value: Future[_ValueType], |
| 750 | + ) -> 'RequiresContextFutureResult[NoDeps, _ValueType, Any]': |
| 751 | + """ |
| 752 | + Creates new container with successful ``Future`` as a unit value. |
| 753 | +
|
| 754 | + .. code:: python |
| 755 | +
|
| 756 | + >>> import anyio |
| 757 | + >>> from returns.context import RequiresContextFutureResult |
| 758 | + >>> from returns.future import Future |
| 759 | + >>> from returns.io import IOSuccess |
| 760 | +
|
| 761 | + >>> assert anyio.run( |
| 762 | + ... RequiresContextFutureResult.from_future(Future.from_value(1)), |
| 763 | + ... RequiresContextFutureResult.empty, |
| 764 | + ... ) == IOSuccess(1) |
| 765 | +
|
| 766 | + """ |
| 767 | + return RequiresContextFutureResult( |
| 768 | + lambda _: FutureResult.from_future(inner_value), |
| 769 | + ) |
| 770 | + |
| 771 | + @classmethod |
| 772 | + def from_failed_future( |
| 773 | + cls, |
| 774 | + inner_value: Future[_ErrorType], |
| 775 | + ) -> 'RequiresContextFutureResult[NoDeps, Any, _ErrorType]': |
| 776 | + """ |
| 777 | + Creates new container with failed ``Future`` as a unit value. |
| 778 | +
|
| 779 | + .. code:: python |
| 780 | +
|
| 781 | + >>> import anyio |
| 782 | + >>> from returns.context import RequiresContextFutureResult |
| 783 | + >>> from returns.future import Future |
| 784 | + >>> from returns.io import IOSuccess |
| 785 | +
|
| 786 | + >>> assert anyio.run( |
| 787 | + ... RequiresContextFutureResult.from_failed_future( |
| 788 | + ... Future.from_value(1), |
| 789 | + ... ), |
| 790 | + ... RequiresContextFutureResult.empty, |
| 791 | + ... ) == IOSuccess(1) |
| 792 | +
|
| 793 | + """ |
| 794 | + return RequiresContextFutureResult( |
| 795 | + lambda _: FutureResult.from_failed_future(inner_value), |
| 796 | + ) |
| 797 | + |
| 798 | + @classmethod |
| 799 | + def from_future_result( |
| 800 | + cls, |
| 801 | + inner_value: FutureResult[_ValueType, _ErrorType], |
| 802 | + ) -> 'RequiresContextFutureResult[NoDeps, _ValueType, _ErrorType]': |
| 803 | + """ |
| 804 | + Creates new container with ``FutureResult`` as a unit value. |
| 805 | +
|
| 806 | + .. code:: python |
| 807 | +
|
| 808 | + >>> import anyio |
| 809 | + >>> from returns.context import RequiresContextFutureResult |
| 810 | + >>> from returns.future import FutureResult |
| 811 | + >>> from returns.io import IOSuccess, IOFailure |
| 812 | +
|
| 813 | + >>> assert anyio.run( |
| 814 | + ... RequiresContextFutureResult.from_future_result( |
| 815 | + ... FutureResult.from_value(1), |
| 816 | + ... ), |
| 817 | + ... RequiresContextFutureResult.empty, |
| 818 | + ... ) == IOSuccess(1) |
| 819 | +
|
| 820 | + >>> assert anyio.run( |
| 821 | + ... RequiresContextFutureResult.from_future_result( |
| 822 | + ... FutureResult.from_failure(1), |
| 823 | + ... ), |
| 824 | + ... RequiresContextFutureResult.empty, |
| 825 | + ... ) == IOFailure(1) |
| 826 | +
|
| 827 | + """ |
| 828 | + return RequiresContextFutureResult(lambda _: inner_value) |
| 829 | + |
743 | 830 | @classmethod
|
744 | 831 | def from_typecast(
|
745 | 832 | cls,
|
@@ -861,6 +948,40 @@ def from_result_context(
|
861 | 948 | lambda deps: FutureResult.from_result(inner_value(deps)),
|
862 | 949 | )
|
863 | 950 |
|
| 951 | + @classmethod |
| 952 | + def from_ioresult_context( |
| 953 | + cls, |
| 954 | + inner_value: |
| 955 | + 'RequiresContextIOResult[_EnvType, _ValueType, _ErrorType]', |
| 956 | + ) -> 'RequiresContextFutureResult[_EnvType, _ValueType, _ErrorType]': |
| 957 | + """ |
| 958 | + Creates new container from ``RequiresContextIOResult`` as a unit value. |
| 959 | +
|
| 960 | + .. code:: python |
| 961 | +
|
| 962 | + >>> import anyio |
| 963 | + >>> from returns.context import RequiresContextIOResult |
| 964 | + >>> from returns.io import IOSuccess, IOFailure |
| 965 | +
|
| 966 | + >>> assert anyio.run( |
| 967 | + ... RequiresContextFutureResult.from_ioresult_context( |
| 968 | + ... RequiresContextIOResult.from_value(1), |
| 969 | + ... ), |
| 970 | + ... RequiresContextFutureResult.empty, |
| 971 | + ... ) == IOSuccess(1) |
| 972 | +
|
| 973 | + >>> assert anyio.run( |
| 974 | + ... RequiresContextFutureResult.from_ioresult_context( |
| 975 | + ... RequiresContextIOResult.from_failure(1), |
| 976 | + ... ), |
| 977 | + ... RequiresContextFutureResult.empty, |
| 978 | + ... ) == IOFailure(1) |
| 979 | +
|
| 980 | + """ |
| 981 | + return RequiresContextFutureResult( |
| 982 | + lambda deps: FutureResult.from_ioresult(inner_value(deps)), |
| 983 | + ) |
| 984 | + |
864 | 985 | @classmethod
|
865 | 986 | def from_value(
|
866 | 987 | cls, inner_value: _FirstType,
|
|
0 commit comments