-
-
Notifications
You must be signed in to change notification settings - Fork 131
add Partition #1905 #1908
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
Merged
Merged
add Partition #1905 #1908
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
bad0590
add annotation
romanmatveevsky 8e4aedb
changelog
romanmatveevsky 268e69e
Update CHANGELOG.md
sobolevn 4a881e3
Merge branch 'dry-python:master' into master
RomanMIzulin fb9c208
wip
966e8f4
upd CHANGELOG
c27144f
pr upds
6c7e210
upd
6c3de2f
fix type test... maybe
5f15d05
fixes
77f7a33
what will typesafety tell us?
b7a8f56
missing file
6a145ea
Update typesafety/test_result/test_partition.yml
RomanMIzulin 6a5aa90
Update typesafety/test_result/test_partition.yml
RomanMIzulin d8369b8
mr issues
romanmatveevsky f1839c3
fixes
romanmatveevsky afe493f
fixes
romanmatveevsky 7a2a8fc
broke brains because of typesafety
romanmatveevsky 7b37a0f
seems like that
romanmatveevsky dd94a17
idk what wrong with Nothing type
romanmatveevsky 85b98e0
change types and fix test
romanmatveevsky dd74a4e
fix ci
romanmatveevsky 9b2e02b
Update returns/methods/partition.py
RomanMIzulin 777cd5b
Update tests/test_result/test_result_methods.py
RomanMIzulin 8f05776
Update tests/test_result/test_result_methods.py
RomanMIzulin dcebdf5
new days new fixes
romanmatveevsky 0b64a3f
Merge branch 'master' into partition
RomanMIzulin edf92e1
Apply suggestions from code review
sobolevn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
from typing import Iterable, List, TypeVar | ||
|
||
from returns.interfaces.unwrappable import Unwrappable | ||
from returns.primitives.exceptions import UnwrapFailedError | ||
|
||
_ValueType = TypeVar('_ValueType', covariant=True) | ||
_ErrorType = TypeVar('_ErrorType', covariant=True) | ||
|
||
|
||
def partition( | ||
containers: Iterable[ | ||
Unwrappable[_ValueType, _ErrorType], | ||
], | ||
) -> tuple[List[_ValueType], List[_ErrorType]]: | ||
""" | ||
Partition a list of unwrappables into successful and failed 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: list[_ValueType] = [] | ||
failures: list[_ErrorType] = [] | ||
for container in containers: | ||
try: | ||
successes.append(container.unwrap()) | ||
except UnwrapFailedError: | ||
failures.append(container.failure()) | ||
return successes, failures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
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 | ||
|
||
|
||
@pytest.mark.parametrize(('containers', 'expected'), [ | ||
( | ||
(Success(1), Success(2), Failure(None), Success(3)), | ||
([1, 2, 3], [None]), | ||
), | ||
( | ||
( | ||
IOResult.from_value(1), | ||
IOResult.from_failure(2), | ||
IOResult.from_value(3), | ||
IOResult.from_failure(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.""" | ||
assert partition(containers) == expected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
- case: partition_no_params | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
disable_cache: false | ||
main: | | ||
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[builtins.int], builtins.list[builtins.str]]" | ||
|
||
- case: partition_io_results | ||
disable_cache: false | ||
main: | | ||
from typing import Tuple | ||
from returns.result import Success, Failure | ||
from returns.methods import partition | ||
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[returns.io.IO[builtins.int]], builtins.list[returns.io.IO[builtins.str]]]" | ||
|
||
- case: partition_maybe | ||
disable_cache: false | ||
main: | | ||
from typing import List, Tuple | ||
from returns.maybe import Maybe | ||
from returns.methods import partition | ||
|
||
x: List[Maybe[int]] | ||
|
||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtins.int], builtins.list[None]]" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.