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 12 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
13 changes: 13 additions & 0 deletions docs/pages/result.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ 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
>>> from returns.result.methods import partition
>>> results = [Success(1), Failure(2), Success(3), Failure(4)]
>>> partition(results)
([1, 3], [2, 4])

FAQ
---

Expand Down
1 change: 1 addition & 0 deletions returns/methods/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
)
38 changes: 38 additions & 0 deletions returns/methods/partition.py
Original file line number Diff line number Diff line change
@@ -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.
sobolevn marked this conversation as resolved.
Show resolved Hide resolved

Preserves order.

.. code:: python

>>> from returns.result import Failure, Success
>>> from returns.methods import partition
>>> results = [Success(1), Failure(2), Success(3), Failure(4)]
RomanMIzulin marked this conversation as resolved.
Show resolved Hide resolved
>>> 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
25 changes: 25 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,25 @@

import pytest

from returns.io import IO, IOResult
from returns.methods import partition
from returns.result import Failure, Success


@pytest.mark.parametrize(('containers', 'expected'), [
(
(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."""
assert partition(containers) == expected
19 changes: 19 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,19 @@
- case: partition_no_params
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
disable_cache: false
main: |
from returns.result import Success, Failure
from returns.result.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]]"

- case: partition_io_results
disable_cache: false
main: |
from returns.result import Success, Failure
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
from returns.result.methods import partition
from returns.io import IO, IOResult

x: List[IOResult[int, str], IOResult[int,str]]
RomanMIzulin marked this conversation as resolved.
Show resolved Hide resolved
reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[IO[builtin.int]],builtins.list[IO[builtin.int]]"
RomanMIzulin marked this conversation as resolved.
Show resolved Hide resolved

Loading