Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Partition #1905 #1908

Merged
merged 28 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bad0590
add annotation
romanmatveevsky Jul 30, 2024
8e4aedb
changelog
romanmatveevsky Jul 30, 2024
268e69e
Update CHANGELOG.md
sobolevn Jul 30, 2024
4a881e3
Merge branch 'dry-python:master' into master
RomanMIzulin Aug 8, 2024
fb9c208
wip
Aug 8, 2024
966e8f4
upd CHANGELOG
Aug 8, 2024
c27144f
pr upds
Aug 8, 2024
6c7e210
upd
Aug 8, 2024
6c3de2f
fix type test... maybe
Aug 8, 2024
5f15d05
fixes
Aug 9, 2024
77f7a33
what will typesafety tell us?
Aug 9, 2024
b7a8f56
missing file
Aug 9, 2024
6a145ea
Update typesafety/test_result/test_partition.yml
RomanMIzulin Aug 12, 2024
6a5aa90
Update typesafety/test_result/test_partition.yml
RomanMIzulin Aug 12, 2024
d8369b8
mr issues
romanmatveevsky Aug 12, 2024
f1839c3
fixes
romanmatveevsky Aug 12, 2024
afe493f
fixes
romanmatveevsky Aug 12, 2024
7a2a8fc
broke brains because of typesafety
romanmatveevsky Aug 12, 2024
7b37a0f
seems like that
romanmatveevsky Aug 12, 2024
dd94a17
idk what wrong with Nothing type
romanmatveevsky Aug 13, 2024
85b98e0
change types and fix test
romanmatveevsky Aug 14, 2024
dd74a4e
fix ci
romanmatveevsky Aug 14, 2024
9b2e02b
Update returns/methods/partition.py
RomanMIzulin Aug 14, 2024
777cd5b
Update tests/test_result/test_result_methods.py
RomanMIzulin Aug 14, 2024
8f05776
Update tests/test_result/test_result_methods.py
RomanMIzulin Aug 14, 2024
dcebdf5
new days new fixes
romanmatveevsky Aug 15, 2024
0b64a3f
Merge branch 'master' into partition
RomanMIzulin Aug 15, 2024
edf92e1
Apply suggestions from code review
sobolevn Aug 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions docs/pages/result.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ argument that lead to that exception.

This decorator works only with functions that has just one argument.

partition
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
~~~~~~~

:func:`partition <returns.result.partition>` is used to convert
list of ``Result`` instances to a tuple of two lists: successes and failures.
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
.. 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
---

Expand Down
25 changes: 25 additions & 0 deletions returns/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Any,
Callable,
Generator,
Iterable,
Iterator,
List,
Optional,
Expand Down Expand Up @@ -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)]
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
>>> 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
15 changes: 15 additions & 0 deletions tests/test_result/test_result_partition.py
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions typesafety/test_result/test_partition.yml
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- case: partition_no_params
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
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]]"

Loading