From bad0590b2fab052a175d49223679b2990f55212d Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Tue, 30 Jul 2024 21:40:15 +0400 Subject: [PATCH 01/26] add annotation --- returns/pipeline.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/returns/pipeline.py b/returns/pipeline.py index 9f41a230..648c9ecb 100644 --- a/returns/pipeline.py +++ b/returns/pipeline.py @@ -1,4 +1,4 @@ -from typing import ClassVar +from typing import Any from returns._internal.pipeline.flow import flow as flow from returns._internal.pipeline.managed import managed as managed @@ -8,7 +8,7 @@ # TODO: add overloads for specific types, so it can narrow them with `TypeIs` -def is_successful(container: Unwrappable) -> bool: +def is_successful(container: Unwrappable[Any, Any]) -> bool: """ Determines if a container was successful or not. From 8e4aedb98cccf33cdca86a618594fe6d42dcd74d Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Tue, 30 Jul 2024 21:43:22 +0400 Subject: [PATCH 02/26] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a51d20..3f5201fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ See [0Ver](https://0ver.org/). - Improve inference of `ResultLike` objects when exception catching decorator is applied with explicit exception types - Add picky exceptions to `impure_safe` decorator like `safe` has. Issue #1543 +- Unwrappable annotated for suppressing reportUnknownVariableType when importing for Pyright checks ### Misc From 268e69e6b76fdb987642322e677b11e850dfba4b Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 30 Jul 2024 20:51:00 +0300 Subject: [PATCH 03/26] Update CHANGELOG.md --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f5201fc..f4a51d20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,6 @@ See [0Ver](https://0ver.org/). - Improve inference of `ResultLike` objects when exception catching decorator is applied with explicit exception types - Add picky exceptions to `impure_safe` decorator like `safe` has. Issue #1543 -- Unwrappable annotated for suppressing reportUnknownVariableType when importing for Pyright checks ### Misc From fb9c20856f1a8c06871539dac70e9d7b15cc019b Mon Sep 17 00:00:00 2001 From: roman matveev Date: Thu, 8 Aug 2024 10:02:14 +0400 Subject: [PATCH 04/26] wip --- docs/pages/result.rst | 12 +++++++++++ returns/result.py | 25 ++++++++++++++++++++++ tests/test_result/test_result_partition.py | 15 +++++++++++++ typesafety/test_result/test_partition.yml | 13 +++++++++++ 4 files changed, 65 insertions(+) create mode 100644 tests/test_result/test_result_partition.py create mode 100644 typesafety/test_result/test_partition.yml diff --git a/docs/pages/result.rst b/docs/pages/result.rst index 4f568db9..3e798686 100644 --- a/docs/pages/result.rst +++ b/docs/pages/result.rst @@ -138,6 +138,18 @@ argument that lead to that exception. This decorator works only with functions that has just one argument. +partition +~~~~~~~ + +:func:`partition ` is used to convert +list of ``Result`` instances to a tuple of two lists: successes and failures. + .. code:: python + + >>> from returns.result import Failure, Success, partition + >>> results = [Success(1), Failure(2), Success(3)] + >>> partition(results) + ([Success(1), Success(3)], [Failure(2)]) + FAQ --- diff --git a/returns/result.py b/returns/result.py index 972f8b7a..f1a362dd 100644 --- a/returns/result.py +++ b/returns/result.py @@ -6,6 +6,7 @@ Any, Callable, Generator, + Iterable, Iterator, List, Optional, @@ -603,3 +604,27 @@ def decorator(arg: _FirstType) -> Result[_NewValueType, _FirstType]: return Failure(arg) return decorator + + +def partition(resulsts: Iterable[Result[_ValueType, _ErrorType]]) -> tuple[ + List[Success], List[Failure], +]: + """ + Partition a list of results into successes and failures. Preserves order. + + .. code:: python + + >>> from returns.result import Failure, Success, partition + >>> results = [Success(1), Failure(2), Success(3)] + >>> partition(results) + ([Success(1), Success(3)], [Failure(2)]) + + """ + successes = [] + failures = [] + for res in resulsts: + if isinstance(res, Success): + successes.append(res) + else: + failures.append(res) + return successes, failures diff --git a/tests/test_result/test_result_partition.py b/tests/test_result/test_result_partition.py new file mode 100644 index 00000000..9165604a --- /dev/null +++ b/tests/test_result/test_result_partition.py @@ -0,0 +1,15 @@ + +import pytest + +from returns.result import Failure, Success, partition + + +@pytest.mark.parametrize(('containers', 'expected'), [ + ( + [Success(1), (Success(2), Failure(None))], + ([Success(1), Success(2)], [Failure(None)]), + ), +]) +def test_partition(containers, expected): + """Test partition function.""" + assert partition(containers) == expected diff --git a/typesafety/test_result/test_partition.yml b/typesafety/test_result/test_partition.yml new file mode 100644 index 00000000..c91a7556 --- /dev/null +++ b/typesafety/test_result/test_partition.yml @@ -0,0 +1,13 @@ +- case: partition_no_params + disable_cache: false + main: | + from returns.result import Success, Failure, partition + + def some(arg: int) -> tuple[list[Success],list[Failure]]: + suc = Sucess(arg) + failure = Failure('test') + + return partition([suc, failure]) + + reveal_type(some(1)) # N: Revealed type is "tuple[returns.result.Success[builtin.int],returns.result.Failure[builtin.str]]" + From 966e8f48b1c1b848518d57d2a263ca41204c0687 Mon Sep 17 00:00:00 2001 From: roman matveev Date: Thu, 8 Aug 2024 10:04:28 +0400 Subject: [PATCH 05/26] upd CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a51d20..583a4b2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ See [0Ver](https://0ver.org/). - Improve inference of `ResultLike` objects when exception catching decorator is applied with explicit exception types - Add picky exceptions to `impure_safe` decorator like `safe` has. Issue #1543 +- Add partition function to result module. Issue #1905 ### Misc From c27144f53d2c52b7a92da6a584e8b1913c3987ed Mon Sep 17 00:00:00 2001 From: roman matveev Date: Thu, 8 Aug 2024 14:51:29 +0400 Subject: [PATCH 06/26] pr upds --- returns/result.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/returns/result.py b/returns/result.py index f1a362dd..3024605f 100644 --- a/returns/result.py +++ b/returns/result.py @@ -606,7 +606,10 @@ def decorator(arg: _FirstType) -> Result[_NewValueType, _FirstType]: return decorator -def partition(resulsts: Iterable[Result[_ValueType, _ErrorType]]) -> tuple[ +_AdditionalType = TypeVar('_AdditionalType') + + +def partition(results: Iterable[result.ResultBasedN[_ValueType, _ErrorType,_AdditionalType]]) -> tuple[ List[Success], List[Failure], ]: """ @@ -622,7 +625,7 @@ def partition(resulsts: Iterable[Result[_ValueType, _ErrorType]]) -> tuple[ """ successes = [] failures = [] - for res in resulsts: + for res in results: if isinstance(res, Success): successes.append(res) else: From 6c7e21042e89c38c1838972c83d0af4916ee27f2 Mon Sep 17 00:00:00 2001 From: roman matveev Date: Thu, 8 Aug 2024 16:14:16 +0400 Subject: [PATCH 07/26] upd --- returns/result.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/returns/result.py b/returns/result.py index 3024605f..7b09b4e4 100644 --- a/returns/result.py +++ b/returns/result.py @@ -609,11 +609,11 @@ def decorator(arg: _FirstType) -> Result[_NewValueType, _FirstType]: _AdditionalType = TypeVar('_AdditionalType') -def partition(results: Iterable[result.ResultBasedN[_ValueType, _ErrorType,_AdditionalType]]) -> tuple[ - List[Success], List[Failure], +def partition(containers: Iterable[result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType]]) -> tuple[ + List[_ValueType], List[_ErrorType], ]: """ - Partition a list of results into successes and failures. Preserves order. + Partition a list of results into successful and failed unwrapped values. Preserves order. .. code:: python @@ -625,9 +625,9 @@ def partition(results: Iterable[result.ResultBasedN[_ValueType, _ErrorType,_Addi """ successes = [] failures = [] - for res in results: + for res in containers: if isinstance(res, Success): - successes.append(res) + successes.append(res.unwrap()) else: - failures.append(res) + failures.append(res.unwrap()) return successes, failures From 6c3de2f6f74f5c6ade371799f2aa78b9846e5793 Mon Sep 17 00:00:00 2001 From: roman matveev Date: Thu, 8 Aug 2024 16:15:58 +0400 Subject: [PATCH 08/26] fix type test... maybe --- typesafety/test_result/test_partition.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/typesafety/test_result/test_partition.yml b/typesafety/test_result/test_partition.yml index c91a7556..c9fb4a03 100644 --- a/typesafety/test_result/test_partition.yml +++ b/typesafety/test_result/test_partition.yml @@ -3,11 +3,11 @@ main: | from returns.result import Success, Failure, partition - def some(arg: int) -> tuple[list[Success],list[Failure]]: - suc = Sucess(arg) + def some(arg: int) -> tuple[list[int],list[str]]: + suc = Success(arg) failure = Failure('test') return partition([suc, failure]) - reveal_type(some(1)) # N: Revealed type is "tuple[returns.result.Success[builtin.int],returns.result.Failure[builtin.str]]" + reveal_type(some(1)) # N: Revealed type is "tuple[list[builtin.int],list[builtin.str]]" From 5f15d056921fb108d5b84e5ce435e852f575d0d5 Mon Sep 17 00:00:00 2001 From: roman matveev Date: Fri, 9 Aug 2024 11:39:17 +0400 Subject: [PATCH 09/26] fixes --- returns/result.py | 16 ++++++++++------ tests/test_result/test_result_partition.py | 4 ++-- typesafety/test_result/test_partition.yml | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/returns/result.py b/returns/result.py index 7b09b4e4..f49c26e8 100644 --- a/returns/result.py +++ b/returns/result.py @@ -609,18 +609,22 @@ def decorator(arg: _FirstType) -> Result[_NewValueType, _FirstType]: _AdditionalType = TypeVar('_AdditionalType') -def partition(containers: Iterable[result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType]]) -> tuple[ - List[_ValueType], List[_ErrorType], -]: +def partition( + containers: Iterable[ + result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType] + ], +) -> tuple[List[_ValueType], List[_ErrorType]]: """ - Partition a list of results into successful and failed unwrapped values. Preserves order. + Partition a list of results into successful and failed unwrapped values. + + Preserves order. .. code:: python >>> from returns.result import Failure, Success, partition >>> results = [Success(1), Failure(2), Success(3)] >>> partition(results) - ([Success(1), Success(3)], [Failure(2)]) + ([1, 3], [2]) """ successes = [] @@ -629,5 +633,5 @@ def partition(containers: Iterable[result.ResultBasedN[_ValueType, _ErrorType, _ if isinstance(res, Success): successes.append(res.unwrap()) else: - failures.append(res.unwrap()) + failures.append(res.failure()) return successes, failures diff --git a/tests/test_result/test_result_partition.py b/tests/test_result/test_result_partition.py index 9165604a..3e974012 100644 --- a/tests/test_result/test_result_partition.py +++ b/tests/test_result/test_result_partition.py @@ -6,8 +6,8 @@ @pytest.mark.parametrize(('containers', 'expected'), [ ( - [Success(1), (Success(2), Failure(None))], - ([Success(1), Success(2)], [Failure(None)]), + (Success(1), Success(2), Failure(None)), + ([1, 2], [None]), ), ]) def test_partition(containers, expected): diff --git a/typesafety/test_result/test_partition.yml b/typesafety/test_result/test_partition.yml index c9fb4a03..bb72fa1a 100644 --- a/typesafety/test_result/test_partition.yml +++ b/typesafety/test_result/test_partition.yml @@ -9,5 +9,5 @@ return partition([suc, failure]) - reveal_type(some(1)) # N: Revealed type is "tuple[list[builtin.int],list[builtin.str]]" + reveal_type(some(1)) # N: Revealed type is "Tuple[builtins.list[builtin.int],builtins.list[builtin.str]]" From 77f7a331d481ecdfc30cc98090c941cf23975b8e Mon Sep 17 00:00:00 2001 From: roman matveev Date: Fri, 9 Aug 2024 18:11:13 +0400 Subject: [PATCH 10/26] what will typesafety tell us? --- docs/pages/result.rst | 7 +++-- returns/methods/__init__.py | 1 + returns/result.py | 32 ---------------------- tests/test_result/test_result_partition.py | 13 ++++++++- typesafety/test_result/test_partition.yml | 18 ++++++++---- 5 files changed, 29 insertions(+), 42 deletions(-) diff --git a/docs/pages/result.rst b/docs/pages/result.rst index 3e798686..74ef35a9 100644 --- a/docs/pages/result.rst +++ b/docs/pages/result.rst @@ -145,10 +145,11 @@ partition list of ``Result`` instances to a tuple of two lists: successes and failures. .. code:: python - >>> from returns.result import Failure, Success, partition - >>> results = [Success(1), Failure(2), Success(3)] + >>> from returns.result import Failure, Success + >>> from returns.result.methods import partition + >>> results = [Success(1), Failure(2), Success(3), Failure(4)] >>> partition(results) - ([Success(1), Success(3)], [Failure(2)]) + ([1, 3], [2, 4]) FAQ --- diff --git a/returns/methods/__init__.py b/returns/methods/__init__.py index 5e9a0a92..0613f1f5 100644 --- a/returns/methods/__init__.py +++ b/returns/methods/__init__.py @@ -1,4 +1,5 @@ from returns.methods.cond import cond as cond +from returns.methods.partition import partition as partition from returns.methods.unwrap_or_failure import ( unwrap_or_failure as unwrap_or_failure, ) diff --git a/returns/result.py b/returns/result.py index f49c26e8..972f8b7a 100644 --- a/returns/result.py +++ b/returns/result.py @@ -6,7 +6,6 @@ Any, Callable, Generator, - Iterable, Iterator, List, Optional, @@ -604,34 +603,3 @@ def decorator(arg: _FirstType) -> Result[_NewValueType, _FirstType]: return Failure(arg) return decorator - - -_AdditionalType = TypeVar('_AdditionalType') - - -def partition( - containers: Iterable[ - result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType] - ], -) -> tuple[List[_ValueType], List[_ErrorType]]: - """ - Partition a list of results into successful and failed unwrapped values. - - Preserves order. - - .. code:: python - - >>> from returns.result import Failure, Success, partition - >>> results = [Success(1), Failure(2), Success(3)] - >>> partition(results) - ([1, 3], [2]) - - """ - successes = [] - failures = [] - for res in containers: - if isinstance(res, Success): - successes.append(res.unwrap()) - else: - failures.append(res.failure()) - return successes, failures diff --git a/tests/test_result/test_result_partition.py b/tests/test_result/test_result_partition.py index 3e974012..78724497 100644 --- a/tests/test_result/test_result_partition.py +++ b/tests/test_result/test_result_partition.py @@ -1,7 +1,10 @@ import pytest -from returns.result import Failure, Success, partition +from returns.future import IOResult +from returns.io import IO +from returns.methods import partition +from returns.result import Failure, Success @pytest.mark.parametrize(('containers', 'expected'), [ @@ -9,6 +12,14 @@ (Success(1), Success(2), Failure(None)), ([1, 2], [None]), ), + ( + ( + IOResult.from_value(1), + IOResult.from_value(2), + IOResult.from_failure(None), + ), + ([IO(1), IO(2)], [IO(None)]), + ), ]) def test_partition(containers, expected): """Test partition function.""" diff --git a/typesafety/test_result/test_partition.yml b/typesafety/test_result/test_partition.yml index bb72fa1a..47fcc2ca 100644 --- a/typesafety/test_result/test_partition.yml +++ b/typesafety/test_result/test_partition.yml @@ -1,13 +1,19 @@ - case: partition_no_params disable_cache: false main: | - from returns.result import Success, Failure, partition + from returns.result import Success, Failure + from returns.result.methods import partition - def some(arg: int) -> tuple[list[int],list[str]]: - suc = Success(arg) - failure = Failure('test') + x: List[Result[int, str]] + reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtin.int], builtins.list[builtin.str]]" - return partition([suc, failure]) +- case: partition_io_results + disable_cache: false + main: | + from returns.result import Success, Failure + from returns.result.methods import partition + from returns.io import IO, IOResult - reveal_type(some(1)) # N: Revealed type is "Tuple[builtins.list[builtin.int],builtins.list[builtin.str]]" + x: List[IOResult[int, str], IOResult[int,str]] + reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]],builtins.list[IO[builtin.int]]" From b7a8f56d72d38667535786d20760c0bf2b6c6664 Mon Sep 17 00:00:00 2001 From: roman matveev Date: Fri, 9 Aug 2024 18:13:30 +0400 Subject: [PATCH 11/26] missing file --- returns/methods/partition.py | 38 ++++++++++++++++++++++ tests/test_result/test_result_partition.py | 3 +- 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 returns/methods/partition.py diff --git a/returns/methods/partition.py b/returns/methods/partition.py new file mode 100644 index 00000000..0180325c --- /dev/null +++ b/returns/methods/partition.py @@ -0,0 +1,38 @@ + +from typing import Iterable, List, TypeVar + +from returns.interfaces.specific import result +from returns.primitives.exceptions import UnwrapFailedError + +_ValueType = TypeVar('_ValueType', covariant=True) +_ErrorType = TypeVar('_ErrorType', covariant=True) +_AdditionalType = TypeVar('_AdditionalType') + + +def partition( + containers: Iterable[ + result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType] + ], +) -> tuple[List[_ValueType], List[_ErrorType]]: + """ + Partition a list of results into successful and failed unwrapped values. + + Preserves order. + + .. code:: python + + >>> from returns.result import Failure, Success + >>> from returns.methods import partition + >>> results = [Success(1), Failure(2), Success(3), Failure(4)] + >>> partition(results) + ([1, 3], [2,4]) + + """ + successes = [] + failures = [] + for container in containers: + try: + successes.append(container.unwrap()) + except UnwrapFailedError: + failures.append(container.failure()) + return successes, failures diff --git a/tests/test_result/test_result_partition.py b/tests/test_result/test_result_partition.py index 78724497..312f0ed5 100644 --- a/tests/test_result/test_result_partition.py +++ b/tests/test_result/test_result_partition.py @@ -1,8 +1,7 @@ import pytest -from returns.future import IOResult -from returns.io import IO +from returns.io import IO, IOResult from returns.methods import partition from returns.result import Failure, Success From 6a145ea5f9b464b94ae4f58c0b8a6d1a5374519f Mon Sep 17 00:00:00 2001 From: RomanMIzulin Date: Mon, 12 Aug 2024 10:56:35 +0400 Subject: [PATCH 12/26] Update typesafety/test_result/test_partition.yml Co-authored-by: sobolevn --- typesafety/test_result/test_partition.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typesafety/test_result/test_partition.yml b/typesafety/test_result/test_partition.yml index 47fcc2ca..4214be2f 100644 --- a/typesafety/test_result/test_partition.yml +++ b/typesafety/test_result/test_partition.yml @@ -14,6 +14,6 @@ from returns.result.methods import partition from returns.io import IO, IOResult - x: List[IOResult[int, str], IOResult[int,str]] + x: Tuple[IOResult[int, str], IOResult[int, str]] reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]],builtins.list[IO[builtin.int]]" From 6a5aa9084623578b516bb7cf339f95e097a87a59 Mon Sep 17 00:00:00 2001 From: RomanMIzulin Date: Mon, 12 Aug 2024 10:57:02 +0400 Subject: [PATCH 13/26] Update typesafety/test_result/test_partition.yml Co-authored-by: sobolevn --- typesafety/test_result/test_partition.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typesafety/test_result/test_partition.yml b/typesafety/test_result/test_partition.yml index 4214be2f..63923faf 100644 --- a/typesafety/test_result/test_partition.yml +++ b/typesafety/test_result/test_partition.yml @@ -15,5 +15,5 @@ from returns.io import IO, IOResult x: Tuple[IOResult[int, str], IOResult[int, str]] - reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]],builtins.list[IO[builtin.int]]" + reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]], builtins.list[IO[builtin.int]]" From d8369b8253e36adade51e45e2577a0a25df83377 Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Mon, 12 Aug 2024 11:04:04 +0400 Subject: [PATCH 14/26] mr issues --- .../{test_result_partition.py => test_result_methods.py} | 0 typesafety/test_result/{test_partition.yml => test_methods.yml} | 1 + 2 files changed, 1 insertion(+) rename tests/test_result/{test_result_partition.py => test_result_methods.py} (100%) rename typesafety/test_result/{test_partition.yml => test_methods.yml} (95%) diff --git a/tests/test_result/test_result_partition.py b/tests/test_result/test_result_methods.py similarity index 100% rename from tests/test_result/test_result_partition.py rename to tests/test_result/test_result_methods.py diff --git a/typesafety/test_result/test_partition.yml b/typesafety/test_result/test_methods.yml similarity index 95% rename from typesafety/test_result/test_partition.yml rename to typesafety/test_result/test_methods.yml index 63923faf..50b17096 100644 --- a/typesafety/test_result/test_partition.yml +++ b/typesafety/test_result/test_methods.yml @@ -10,6 +10,7 @@ - case: partition_io_results disable_cache: false main: | + from typing import Tuple from returns.result import Success, Failure from returns.result.methods import partition from returns.io import IO, IOResult From f1839c310490da8bf4b848cdfb892261361290c9 Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Mon, 12 Aug 2024 11:21:27 +0400 Subject: [PATCH 15/26] fixes --- docs/pages/result.rst | 2 +- returns/methods/partition.py | 2 +- .../test_methods.yml => test_methods/test_partition.yml} | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename typesafety/{test_result/test_methods.yml => test_methods/test_partition.yml} (86%) diff --git a/docs/pages/result.rst b/docs/pages/result.rst index 74ef35a9..751b81d3 100644 --- a/docs/pages/result.rst +++ b/docs/pages/result.rst @@ -146,7 +146,7 @@ list of ``Result`` instances to a tuple of two lists: successes and failures. .. code:: python >>> from returns.result import Failure, Success - >>> from returns.result.methods import partition + >>> from returns.methods import partition >>> results = [Success(1), Failure(2), Success(3), Failure(4)] >>> partition(results) ([1, 3], [2, 4]) diff --git a/returns/methods/partition.py b/returns/methods/partition.py index 0180325c..a8d3ad93 100644 --- a/returns/methods/partition.py +++ b/returns/methods/partition.py @@ -25,7 +25,7 @@ def partition( >>> from returns.methods import partition >>> results = [Success(1), Failure(2), Success(3), Failure(4)] >>> partition(results) - ([1, 3], [2,4]) + ([1, 3], [2, 4]) """ successes = [] diff --git a/typesafety/test_result/test_methods.yml b/typesafety/test_methods/test_partition.yml similarity index 86% rename from typesafety/test_result/test_methods.yml rename to typesafety/test_methods/test_partition.yml index 50b17096..e6681c6a 100644 --- a/typesafety/test_result/test_methods.yml +++ b/typesafety/test_methods/test_partition.yml @@ -2,7 +2,7 @@ disable_cache: false main: | from returns.result import Success, Failure - from returns.result.methods import partition + from returns.methods import partition x: List[Result[int, str]] reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtin.int], builtins.list[builtin.str]]" @@ -12,7 +12,7 @@ main: | from typing import Tuple from returns.result import Success, Failure - from returns.result.methods import partition + from returns.methods import partition from returns.io import IO, IOResult x: Tuple[IOResult[int, str], IOResult[int, str]] From afe493f438e3de2e63209532f49784689f76a40a Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Mon, 12 Aug 2024 11:45:00 +0400 Subject: [PATCH 16/26] fixes --- returns/methods/partition.py | 9 ++++++--- typesafety/test_methods/test_partition.yml | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/returns/methods/partition.py b/returns/methods/partition.py index a8d3ad93..09ab5de1 100644 --- a/returns/methods/partition.py +++ b/returns/methods/partition.py @@ -1,7 +1,7 @@ -from typing import Iterable, List, TypeVar +from typing import Iterable, List, TypeVar, Union -from returns.interfaces.specific import result +from returns.interfaces.specific import ioresult, result from returns.primitives.exceptions import UnwrapFailedError _ValueType = TypeVar('_ValueType', covariant=True) @@ -11,7 +11,10 @@ def partition( containers: Iterable[ - result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType] + Union[ + result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType], + ioresult.IOResultBasedN[_ValueType, _ErrorType, _AdditionalType], + ] ], ) -> tuple[List[_ValueType], List[_ErrorType]]: """ diff --git a/typesafety/test_methods/test_partition.yml b/typesafety/test_methods/test_partition.yml index e6681c6a..2bd9718a 100644 --- a/typesafety/test_methods/test_partition.yml +++ b/typesafety/test_methods/test_partition.yml @@ -1,11 +1,12 @@ - case: partition_no_params disable_cache: false main: | - from returns.result import Success, Failure + from typing import List + from returns.result import Success, Failure, Result from returns.methods import partition x: List[Result[int, str]] - reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtin.int], builtins.list[builtin.str]]" + reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtins.int], builtins.list[builtins.str]]" - case: partition_io_results disable_cache: false @@ -13,7 +14,7 @@ from typing import Tuple from returns.result import Success, Failure from returns.methods import partition - from returns.io import IO, IOResult + from returns.io import IO, IOResult, IOSuccess x: Tuple[IOResult[int, str], IOResult[int, str]] reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]], builtins.list[IO[builtin.int]]" From 7a2a8fc7a49f9e2558f7f3958e465ab51064e8d8 Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Mon, 12 Aug 2024 13:51:21 +0400 Subject: [PATCH 17/26] broke brains because of typesafety --- returns/methods/partition.py | 9 +++------ typesafety/test_methods/test_partition.yml | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/returns/methods/partition.py b/returns/methods/partition.py index 09ab5de1..080a164e 100644 --- a/returns/methods/partition.py +++ b/returns/methods/partition.py @@ -1,7 +1,7 @@ -from typing import Iterable, List, TypeVar, Union +from typing import Iterable, List, TypeVar -from returns.interfaces.specific import ioresult, result +from returns.interfaces.specific import result from returns.primitives.exceptions import UnwrapFailedError _ValueType = TypeVar('_ValueType', covariant=True) @@ -11,10 +11,7 @@ def partition( containers: Iterable[ - Union[ - result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType], - ioresult.IOResultBasedN[_ValueType, _ErrorType, _AdditionalType], - ] + result.UnwrappableResult[_ValueType, _ErrorType, _AdditionalType, _ValueType, _ErrorType], ], ) -> tuple[List[_ValueType], List[_ErrorType]]: """ diff --git a/typesafety/test_methods/test_partition.yml b/typesafety/test_methods/test_partition.yml index 2bd9718a..b7f2075a 100644 --- a/typesafety/test_methods/test_partition.yml +++ b/typesafety/test_methods/test_partition.yml @@ -17,5 +17,5 @@ from returns.io import IO, IOResult, IOSuccess x: Tuple[IOResult[int, str], IOResult[int, str]] - reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]], builtins.list[IO[builtin.int]]" + reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]], builtins.list[IO[builtin.str]]" From 7b37a0fd171bc6b4e8fb62daf97736cddc002617 Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Mon, 12 Aug 2024 14:23:44 +0400 Subject: [PATCH 18/26] seems like that --- returns/methods/partition.py | 9 ++++----- typesafety/test_methods/test_partition.yml | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/returns/methods/partition.py b/returns/methods/partition.py index 080a164e..20eb64f7 100644 --- a/returns/methods/partition.py +++ b/returns/methods/partition.py @@ -1,17 +1,16 @@ from typing import Iterable, List, TypeVar -from returns.interfaces.specific import result +from returns.interfaces.unwrappable import Unwrappable from returns.primitives.exceptions import UnwrapFailedError _ValueType = TypeVar('_ValueType', covariant=True) _ErrorType = TypeVar('_ErrorType', covariant=True) -_AdditionalType = TypeVar('_AdditionalType') def partition( containers: Iterable[ - result.UnwrappableResult[_ValueType, _ErrorType, _AdditionalType, _ValueType, _ErrorType], + Unwrappable[_ValueType, _ErrorType], ], ) -> tuple[List[_ValueType], List[_ErrorType]]: """ @@ -28,8 +27,8 @@ def partition( ([1, 3], [2, 4]) """ - successes = [] - failures = [] + successes: list[_ValueType] = [] + failures: list[_ErrorType] = [] for container in containers: try: successes.append(container.unwrap()) diff --git a/typesafety/test_methods/test_partition.yml b/typesafety/test_methods/test_partition.yml index b7f2075a..391802eb 100644 --- a/typesafety/test_methods/test_partition.yml +++ b/typesafety/test_methods/test_partition.yml @@ -17,5 +17,5 @@ from returns.io import IO, IOResult, IOSuccess x: Tuple[IOResult[int, str], IOResult[int, str]] - reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]], builtins.list[IO[builtin.str]]" + reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[returns.io.IO[builtins.int]], builtins.list[returns.io.IO[builtins.str]]]" From dd94a17eef3a5f3fb956187756ec5e721c90be89 Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Tue, 13 Aug 2024 12:05:18 +0400 Subject: [PATCH 19/26] idk what wrong with Nothing type --- docs/pages/result.rst | 13 +++++++------ tests/test_result/test_result_methods.py | 5 +++++ typesafety/test_methods/test_partition.yml | 11 +++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/docs/pages/result.rst b/docs/pages/result.rst index 751b81d3..cd153fac 100644 --- a/docs/pages/result.rst +++ b/docs/pages/result.rst @@ -143,13 +143,14 @@ partition :func:`partition ` is used to convert list of ``Result`` instances to a tuple of two lists: successes and failures. - .. code:: python - >>> from returns.result import Failure, Success - >>> from returns.methods import partition - >>> results = [Success(1), Failure(2), Success(3), Failure(4)] - >>> partition(results) - ([1, 3], [2, 4]) +.. code:: python + + >>> from returns.result import Failure, Success + >>> from returns.methods import partition + >>> results = [Success(1), Failure(2), Success(3), Failure(4)] + >>> partition(results) + ([1, 3], [2, 4]) FAQ --- diff --git a/tests/test_result/test_result_methods.py b/tests/test_result/test_result_methods.py index 312f0ed5..f8a5eaa1 100644 --- a/tests/test_result/test_result_methods.py +++ b/tests/test_result/test_result_methods.py @@ -2,6 +2,7 @@ import pytest from returns.io import IO, IOResult +from returns.maybe import Nothing, Some from returns.methods import partition from returns.result import Failure, Success @@ -19,6 +20,10 @@ ), ([IO(1), IO(2)], [IO(None)]), ), + ( + (Some(1), Some(2), Nothing), + ([1, 2], []), + ), ]) def test_partition(containers, expected): """Test partition function.""" diff --git a/typesafety/test_methods/test_partition.yml b/typesafety/test_methods/test_partition.yml index 391802eb..f555e8c8 100644 --- a/typesafety/test_methods/test_partition.yml +++ b/typesafety/test_methods/test_partition.yml @@ -19,3 +19,14 @@ x: Tuple[IOResult[int, str], IOResult[int, str]] reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[returns.io.IO[builtins.int]], builtins.list[returns.io.IO[builtins.str]]]" +- case: partition_maybe + disable_cache: false + main: | + from typing import List, Tuple, Never + from returns.maybe import Maybe, Some, Nothing, Maybe, _Nothing + from returns.methods import partition + + x: Tuple[Some[int], Some[int], Maybe[None]] + + reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtins.int], builtins.list[None]]" + From 85b98e007ccb218d7fbf11f6506d0e7686ac820b Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Wed, 14 Aug 2024 12:37:07 +0400 Subject: [PATCH 20/26] change types and fix test --- tests/test_result/test_result_methods.py | 2 +- typesafety/test_methods/test_partition.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_result/test_result_methods.py b/tests/test_result/test_result_methods.py index f8a5eaa1..c9afb37c 100644 --- a/tests/test_result/test_result_methods.py +++ b/tests/test_result/test_result_methods.py @@ -22,7 +22,7 @@ ), ( (Some(1), Some(2), Nothing), - ([1, 2], []), + ([1, 2], [None]), ), ]) def test_partition(containers, expected): diff --git a/typesafety/test_methods/test_partition.yml b/typesafety/test_methods/test_partition.yml index f555e8c8..3bef6d6b 100644 --- a/typesafety/test_methods/test_partition.yml +++ b/typesafety/test_methods/test_partition.yml @@ -26,7 +26,7 @@ from returns.maybe import Maybe, Some, Nothing, Maybe, _Nothing from returns.methods import partition - x: Tuple[Some[int], Some[int], Maybe[None]] + x: List[Maybe[int]] reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtins.int], builtins.list[None]]" From dd74a4eb30cf7e40ab65e536c15b3730ecb763ca Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Wed, 14 Aug 2024 18:34:56 +0400 Subject: [PATCH 21/26] fix ci --- docs/pages/result.rst | 2 +- typesafety/test_methods/test_partition.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/pages/result.rst b/docs/pages/result.rst index cd153fac..3c5fe271 100644 --- a/docs/pages/result.rst +++ b/docs/pages/result.rst @@ -139,7 +139,7 @@ argument that lead to that exception. This decorator works only with functions that has just one argument. partition -~~~~~~~ +~~~~~~~~~ :func:`partition ` is used to convert list of ``Result`` instances to a tuple of two lists: successes and failures. diff --git a/typesafety/test_methods/test_partition.yml b/typesafety/test_methods/test_partition.yml index 3bef6d6b..2241138e 100644 --- a/typesafety/test_methods/test_partition.yml +++ b/typesafety/test_methods/test_partition.yml @@ -22,8 +22,8 @@ - case: partition_maybe disable_cache: false main: | - from typing import List, Tuple, Never - from returns.maybe import Maybe, Some, Nothing, Maybe, _Nothing + from typing import List, Tuple + from returns.maybe import Maybe from returns.methods import partition x: List[Maybe[int]] From 9b2e02b8f9a105d3ab231a8b45dc73ae07f67987 Mon Sep 17 00:00:00 2001 From: RomanMIzulin Date: Wed, 14 Aug 2024 19:16:54 +0400 Subject: [PATCH 22/26] Update returns/methods/partition.py Co-authored-by: sobolevn --- returns/methods/partition.py | 1 + 1 file changed, 1 insertion(+) diff --git a/returns/methods/partition.py b/returns/methods/partition.py index 20eb64f7..04bd36f8 100644 --- a/returns/methods/partition.py +++ b/returns/methods/partition.py @@ -22,6 +22,7 @@ def partition( >>> from returns.result import Failure, Success >>> from returns.methods import partition + >>> results = [Success(1), Failure(2), Success(3), Failure(4)] >>> partition(results) ([1, 3], [2, 4]) From 777cd5bb2e28474d61120f8b2ae318c52b7774ab Mon Sep 17 00:00:00 2001 From: RomanMIzulin Date: Wed, 14 Aug 2024 19:17:07 +0400 Subject: [PATCH 23/26] Update tests/test_result/test_result_methods.py Co-authored-by: sobolevn --- tests/test_result/test_result_methods.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_result/test_result_methods.py b/tests/test_result/test_result_methods.py index c9afb37c..d9c73dc9 100644 --- a/tests/test_result/test_result_methods.py +++ b/tests/test_result/test_result_methods.py @@ -9,8 +9,8 @@ @pytest.mark.parametrize(('containers', 'expected'), [ ( - (Success(1), Success(2), Failure(None)), - ([1, 2], [None]), + (Success(1), Success(2), Failure(None), Success(3)), + ([1, 2, 3], [None]), ), ( ( From 8f057761bf6eef2bae33e11f2b3b61475f6690f9 Mon Sep 17 00:00:00 2001 From: RomanMIzulin Date: Wed, 14 Aug 2024 19:17:42 +0400 Subject: [PATCH 24/26] Update tests/test_result/test_result_methods.py Co-authored-by: sobolevn --- tests/test_result/test_result_methods.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_result/test_result_methods.py b/tests/test_result/test_result_methods.py index d9c73dc9..01bbb85d 100644 --- a/tests/test_result/test_result_methods.py +++ b/tests/test_result/test_result_methods.py @@ -15,10 +15,11 @@ ( ( IOResult.from_value(1), - IOResult.from_value(2), - IOResult.from_failure(None), + IOResult.from_failure(2), + IOResult.from_value(3), + IOResult.from_failure(4), ), - ([IO(1), IO(2)], [IO(None)]), + ([IO(1), IO(2)], [IO(3), IO(4)]), ), ( (Some(1), Some(2), Nothing), From dcebdf5cee78db7068f3fd95c0aefb0d8ddb6c5f Mon Sep 17 00:00:00 2001 From: Roman Matveev Date: Thu, 15 Aug 2024 10:33:45 +0400 Subject: [PATCH 25/26] new days new fixes --- docs/pages/methods.rst | 14 ++++++++++++++ docs/pages/result.rst | 14 -------------- returns/methods/partition.py | 2 +- .../test_partition.py} | 3 ++- 4 files changed, 17 insertions(+), 16 deletions(-) rename tests/{test_result/test_result_methods.py => test_methods/test_partition.py} (92%) diff --git a/docs/pages/methods.rst b/docs/pages/methods.rst index 421ae224..0e8ffe2e 100644 --- a/docs/pages/methods.rst +++ b/docs/pages/methods.rst @@ -76,6 +76,20 @@ Here's a full example: >>> error_handled = pointfree.bimap(lambda inr: inr + 1, lambda _: 0)(instance) >>> assert isinstance(methods.unwrap_or_failure(error_handled), int) +partition +~~~~~~~~~ + +:func:`partition ` is used to convert +list of Unwrappable instances like Result, IOResult, Maybe +to a tuple of two lists: successes and failures. + +.. code:: python + + >>> from returns.result import Failure, Success + >>> from returns.methods import partition + >>> results = [Success(1), Failure(2), Success(3), Failure(4)] + >>> partition(results) + ([1, 3], [2, 4]) API Reference ------------- diff --git a/docs/pages/result.rst b/docs/pages/result.rst index 3c5fe271..4f568db9 100644 --- a/docs/pages/result.rst +++ b/docs/pages/result.rst @@ -138,20 +138,6 @@ argument that lead to that exception. This decorator works only with functions that has just one argument. -partition -~~~~~~~~~ - -:func:`partition ` is used to convert -list of ``Result`` instances to a tuple of two lists: successes and failures. - -.. code:: python - - >>> from returns.result import Failure, Success - >>> from returns.methods import partition - >>> results = [Success(1), Failure(2), Success(3), Failure(4)] - >>> partition(results) - ([1, 3], [2, 4]) - FAQ --- diff --git a/returns/methods/partition.py b/returns/methods/partition.py index 04bd36f8..f844fda7 100644 --- a/returns/methods/partition.py +++ b/returns/methods/partition.py @@ -14,7 +14,7 @@ def partition( ], ) -> tuple[List[_ValueType], List[_ErrorType]]: """ - Partition a list of results into successful and failed unwrapped values. + Partition a list of unwrappables into successful and failed values. Preserves order. diff --git a/tests/test_result/test_result_methods.py b/tests/test_methods/test_partition.py similarity index 92% rename from tests/test_result/test_result_methods.py rename to tests/test_methods/test_partition.py index 01bbb85d..d68faefb 100644 --- a/tests/test_result/test_result_methods.py +++ b/tests/test_methods/test_partition.py @@ -19,12 +19,13 @@ IOResult.from_value(3), IOResult.from_failure(4), ), - ([IO(1), IO(2)], [IO(3), IO(4)]), + ([IO(1), IO(3)], [IO(2), IO(4)]), ), ( (Some(1), Some(2), Nothing), ([1, 2], [None]), ), + ((), ([], [])), ]) def test_partition(containers, expected): """Test partition function.""" From edf92e13e1c617b70ab447aa2470cec4c2968819 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 15 Aug 2024 10:50:10 +0300 Subject: [PATCH 26/26] Apply suggestions from code review --- docs/pages/methods.rst | 4 +++- typesafety/test_methods/test_partition.yml | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/pages/methods.rst b/docs/pages/methods.rst index 0e8ffe2e..ce7cfa64 100644 --- a/docs/pages/methods.rst +++ b/docs/pages/methods.rst @@ -80,7 +80,9 @@ partition ~~~~~~~~~ :func:`partition ` is used to convert -list of Unwrappable instances like Result, IOResult, Maybe +list of :class:`~returns.interfaces.Unwrappable` +instances like :class:`~returns.result.Result`, +:class:`~returns.io.IOResult`, and :class:`~returns.maybe.Maybe` to a tuple of two lists: successes and failures. .. code:: python diff --git a/typesafety/test_methods/test_partition.yml b/typesafety/test_methods/test_partition.yml index 2241138e..3f4eeba6 100644 --- a/typesafety/test_methods/test_partition.yml +++ b/typesafety/test_methods/test_partition.yml @@ -1,4 +1,4 @@ -- case: partition_no_params +- case: partition_result disable_cache: false main: | from typing import List