|
6 | 6 | Callable,
|
7 | 7 | ClassVar,
|
8 | 8 | Generic,
|
| 9 | + Iterable, |
| 10 | + Sequence, |
9 | 11 | TypeVar,
|
10 | 12 | Union,
|
11 | 13 | )
|
12 | 14 |
|
13 | 15 | from typing_extensions import final
|
14 | 16 |
|
15 | 17 | from returns._generated.futures import _future_result
|
| 18 | +from returns._generated.iterable import iterable |
16 | 19 | from returns.context import NoDeps
|
17 | 20 | from returns.future import FutureResult
|
18 | 21 | from returns.io import IO, IOResult
|
@@ -632,6 +635,184 @@ def failure(self) -> Callable[[_EnvType], Awaitable[IO[_ErrorType]]]:
|
632 | 635 | """
|
633 | 636 | return lambda deps: _future_result.async_failure(self(deps))
|
634 | 637 |
|
| 638 | + @classmethod |
| 639 | + def from_result( |
| 640 | + cls, inner_value: Result[_ValueType, _ErrorType], |
| 641 | + ) -> 'RequiresContextFutureResult[NoDeps, _ValueType, _ErrorType]': |
| 642 | + """ |
| 643 | + Creates new container with ``Result`` as a unit value. |
| 644 | +
|
| 645 | + .. code:: python |
| 646 | +
|
| 647 | + >>> import anyio |
| 648 | + >>> from returns.context import RequiresContextFutureResult |
| 649 | + >>> from returns.result import Success, Failure |
| 650 | + >>> from returns.io import IOSuccess, IOFailure |
| 651 | +
|
| 652 | + >>> assert anyio.run( |
| 653 | + ... RequiresContextFutureResult.from_result(Success(1)), |
| 654 | + ... RequiresContextFutureResult.empty, |
| 655 | + ... ) == IOSuccess(1) |
| 656 | +
|
| 657 | + >>> assert anyio.run( |
| 658 | + ... RequiresContextFutureResult.from_result(Failure(1)), |
| 659 | + ... RequiresContextFutureResult.empty, |
| 660 | + ... ) == IOFailure(1) |
| 661 | +
|
| 662 | + """ |
| 663 | + return RequiresContextFutureResult( |
| 664 | + lambda _: FutureResult.from_result(inner_value), |
| 665 | + ) |
| 666 | + |
| 667 | + @classmethod |
| 668 | + def from_ioresult( |
| 669 | + cls, inner_value: IOResult[_ValueType, _ErrorType], |
| 670 | + ) -> 'RequiresContextFutureResult[NoDeps, _ValueType, _ErrorType]': |
| 671 | + """ |
| 672 | + Creates new container with ``IOResult`` as a unit value. |
| 673 | +
|
| 674 | + .. code:: python |
| 675 | +
|
| 676 | + >>> import anyio |
| 677 | + >>> from returns.context import RequiresContextFutureResult |
| 678 | + >>> from returns.io import IOSuccess, IOFailure |
| 679 | +
|
| 680 | + >>> assert anyio.run( |
| 681 | + ... RequiresContextFutureResult.from_ioresult(IOSuccess(1)), |
| 682 | + ... RequiresContextFutureResult.empty, |
| 683 | + ... ) == IOSuccess(1) |
| 684 | +
|
| 685 | + >>> assert anyio.run( |
| 686 | + ... RequiresContextFutureResult.from_ioresult(IOFailure(1)), |
| 687 | + ... RequiresContextFutureResult.empty, |
| 688 | + ... ) == IOFailure(1) |
| 689 | +
|
| 690 | + """ |
| 691 | + return RequiresContextFutureResult( |
| 692 | + lambda _: FutureResult.from_ioresult(inner_value), |
| 693 | + ) |
| 694 | + |
| 695 | + @classmethod |
| 696 | + def from_typecast( |
| 697 | + cls, |
| 698 | + container: 'RequiresContext[' |
| 699 | + '_EnvType, FutureResult[_NewValueType, _NewErrorType]]', |
| 700 | + ) -> 'RequiresContextFutureResult[_EnvType, _NewValueType, _NewErrorType]': |
| 701 | + """ |
| 702 | + You might end up with ``RequiresContext[FutureResult]`` as a value. |
| 703 | +
|
| 704 | + This method is designed to turn it into ``RequiresContextFutureResult``. |
| 705 | + It will save all the typing information. |
| 706 | +
|
| 707 | + It is just more useful! |
| 708 | +
|
| 709 | + .. code:: python |
| 710 | +
|
| 711 | + >>> import anyio |
| 712 | + >>> from returns.context import RequiresContext |
| 713 | + >>> from returns.future import FutureResult |
| 714 | + >>> from returns.io import IOSuccess, IOFailure |
| 715 | +
|
| 716 | + >>> assert anyio.run( |
| 717 | + ... RequiresContextFutureResult.from_typecast( |
| 718 | + ... RequiresContext.from_value(FutureResult.from_value(1)), |
| 719 | + ... ), |
| 720 | + ... RequiresContextFutureResult.empty, |
| 721 | + ... ) == IOSuccess(1) |
| 722 | +
|
| 723 | + >>> assert anyio.run( |
| 724 | + ... RequiresContextFutureResult.from_typecast( |
| 725 | + ... RequiresContext.from_value(FutureResult.from_failure(1)), |
| 726 | + ... ), |
| 727 | + ... RequiresContextFutureResult.empty, |
| 728 | + ... ) == IOFailure(1) |
| 729 | +
|
| 730 | + """ |
| 731 | + return RequiresContextFutureResult(container) |
| 732 | + |
| 733 | + @classmethod |
| 734 | + def from_context( |
| 735 | + cls, inner_value: 'RequiresContext[_EnvType, _FirstType]', |
| 736 | + ) -> 'RequiresContextFutureResult[_EnvType, _FirstType, Any]': |
| 737 | + """ |
| 738 | + Creates new container from ``RequiresContext`` as a success unit. |
| 739 | +
|
| 740 | + .. code:: python |
| 741 | +
|
| 742 | + >>> import anyio |
| 743 | + >>> from returns.context import RequiresContext |
| 744 | + >>> from returns.io import IOSuccess |
| 745 | +
|
| 746 | + >>> assert anyio.run( |
| 747 | + ... RequiresContextFutureResult.from_context( |
| 748 | + ... RequiresContext.from_value(1), |
| 749 | + ... ), |
| 750 | + ... RequiresContextFutureResult.empty, |
| 751 | + ... ) == IOSuccess(1) |
| 752 | +
|
| 753 | + """ |
| 754 | + return RequiresContextFutureResult( |
| 755 | + lambda deps: FutureResult.from_value(inner_value(deps)), |
| 756 | + ) |
| 757 | + |
| 758 | + @classmethod |
| 759 | + def from_failed_context( |
| 760 | + cls, inner_value: 'RequiresContext[_EnvType, _FirstType]', |
| 761 | + ) -> 'RequiresContextFutureResult[_EnvType, Any, _FirstType]': |
| 762 | + """ |
| 763 | + Creates new container from ``RequiresContext`` as a failure unit. |
| 764 | +
|
| 765 | + .. code:: python |
| 766 | +
|
| 767 | + >>> import anyio |
| 768 | + >>> from returns.context import RequiresContext |
| 769 | + >>> from returns.io import IOFailure |
| 770 | +
|
| 771 | + >>> assert anyio.run( |
| 772 | + ... RequiresContextFutureResult.from_failed_context( |
| 773 | + ... RequiresContext.from_value(1), |
| 774 | + ... ), |
| 775 | + ... RequiresContextFutureResult.empty, |
| 776 | + ... ) == IOFailure(1) |
| 777 | +
|
| 778 | + """ |
| 779 | + return RequiresContextFutureResult( |
| 780 | + lambda deps: FutureResult.from_failure(inner_value(deps)), |
| 781 | + ) |
| 782 | + |
| 783 | + @classmethod |
| 784 | + def from_result_context( |
| 785 | + cls, |
| 786 | + inner_value: 'RequiresContextResult[_EnvType, _ValueType, _ErrorType]', |
| 787 | + ) -> 'RequiresContextFutureResult[_EnvType, _ValueType, _ErrorType]': |
| 788 | + """ |
| 789 | + Creates new container from ``RequiresContextResult`` as a unit value. |
| 790 | +
|
| 791 | + .. code:: python |
| 792 | +
|
| 793 | + >>> import anyio |
| 794 | + >>> from returns.context import RequiresContextResult |
| 795 | + >>> from returns.io import IOSuccess, IOFailure |
| 796 | +
|
| 797 | + >>> assert anyio.run( |
| 798 | + ... RequiresContextFutureResult.from_result_context( |
| 799 | + ... RequiresContextResult.from_value(1), |
| 800 | + ... ), |
| 801 | + ... RequiresContextFutureResult.empty, |
| 802 | + ... ) == IOSuccess(1) |
| 803 | +
|
| 804 | + >>> assert anyio.run( |
| 805 | + ... RequiresContextFutureResult.from_result_context( |
| 806 | + ... RequiresContextResult.from_failure(1), |
| 807 | + ... ), |
| 808 | + ... RequiresContextFutureResult.empty, |
| 809 | + ... ) == IOFailure(1) |
| 810 | +
|
| 811 | + """ |
| 812 | + return RequiresContextFutureResult( |
| 813 | + lambda deps: FutureResult.from_result(inner_value(deps)), |
| 814 | + ) |
| 815 | + |
635 | 816 | @classmethod
|
636 | 817 | def from_value(
|
637 | 818 | cls, inner_value: _FirstType,
|
@@ -676,6 +857,44 @@ def from_failure(
|
676 | 857 | lambda _: FutureResult.from_failure(inner_value),
|
677 | 858 | )
|
678 | 859 |
|
| 860 | + @classmethod |
| 861 | + def from_iterable( |
| 862 | + cls, |
| 863 | + containers: |
| 864 | + Iterable[ |
| 865 | + 'RequiresContextFutureResult[_EnvType, _ValueType, _ErrorType]', |
| 866 | + ], |
| 867 | + ) -> 'ReaderFutureResult[_EnvType, Sequence[_ValueType], _ErrorType]': |
| 868 | + """ |
| 869 | + Transforms an iterable of ``RequiresContextFutureResult`` containers. |
| 870 | +
|
| 871 | + Returns a single container with multiple elements inside. |
| 872 | +
|
| 873 | + .. code:: python |
| 874 | +
|
| 875 | + >>> import anyio |
| 876 | + >>> from returns.context import RequiresContextFutureResult |
| 877 | + >>> from returns.io import IOSuccess, IOFailure |
| 878 | +
|
| 879 | + >>> assert anyio.run( |
| 880 | + ... RequiresContextFutureResult.from_iterable([ |
| 881 | + ... RequiresContextFutureResult.from_value(1), |
| 882 | + ... RequiresContextFutureResult.from_value(2), |
| 883 | + ... ]), |
| 884 | + ... RequiresContextFutureResult.empty, |
| 885 | + ... ) == IOSuccess((1, 2)) |
| 886 | +
|
| 887 | + >>> assert anyio.run( |
| 888 | + ... RequiresContextFutureResult.from_iterable([ |
| 889 | + ... RequiresContextFutureResult.from_value(1), |
| 890 | + ... RequiresContextFutureResult.from_failure('a'), |
| 891 | + ... ]), |
| 892 | + ... RequiresContextFutureResult.empty, |
| 893 | + ... ) == IOFailure('a') |
| 894 | +
|
| 895 | + """ |
| 896 | + return iterable(cls, containers) |
| 897 | + |
679 | 898 |
|
680 | 899 | @final
|
681 | 900 | class ContextFutureResult(Immutable, Generic[_EnvType], metaclass=ABCMeta):
|
|
0 commit comments