-
-
Notifications
You must be signed in to change notification settings - Fork 120
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
Issue 1114 #1131
Open
CucumisSativus
wants to merge
10
commits into
dry-python:master
Choose a base branch
from
CucumisSativus:issue-1114
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Issue 1114 #1131
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6ef0537
Filter for maybe
CucumisSativus 586af8b
add changes to changelog
CucumisSativus 6b96d21
Update docs
CucumisSativus e281d41
Update docs2
CucumisSativus bed806e
hkt for filter
CucumisSativus 4954a4a
Bind filter to maybe n
CucumisSativus 45b1fa6
satisfy almighty mypy
CucumisSativus 3878184
typo and nested function typing
CucumisSativus 60950f6
issue found!
CucumisSativus cca6894
slots added
CucumisSativus 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 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 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,56 @@ | ||
from abc import abstractmethod | ||
from typing import Callable, NoReturn, TypeVar | ||
|
||
from returns.interfaces.specific.maybe import MaybeLikeN | ||
from returns.primitives.hkt import Kind1 | ||
|
||
_FirstType = TypeVar('_FirstType') | ||
_SecondType = TypeVar('_SecondType') | ||
_ThirdType = TypeVar('_ThirdType') | ||
|
||
_FilterableType = TypeVar('_FilterableType', bound='FilterableN') | ||
|
||
|
||
class FilterableN(MaybeLikeN[_FirstType, _SecondType, _ThirdType]): | ||
""" | ||
Represents container that can apply filter over inner value. | ||
|
||
There are no aliases or ``FilterableN` for ``Filterable`` interface. | ||
Because it always uses one type. | ||
|
||
Not all types can be ``Filterable`` because we require | ||
a possibility to access internal value and to model a case, | ||
where the predicate is false | ||
|
||
.. code:: python | ||
|
||
>>> from returns.maybe import Nothing, Some | ||
>>> from returns.pointfree import filter_ | ||
|
||
>>> def is_even(argument: int) -> bool: | ||
... return argument % 2 == 0 | ||
|
||
>>> assert filter_(is_even)(Some(5)) == Nothing | ||
>>> assert filter_(is_even)(Some(6)) == Some(6) | ||
>>> assert filter_(is_even)(Nothing) == Nothing | ||
|
||
""" | ||
|
||
__slots__ = () | ||
|
||
@abstractmethod | ||
def filter( | ||
self: _FilterableType, | ||
predicate: Callable[[_FirstType], bool], | ||
) -> Kind1[_FilterableType, _FirstType]: | ||
"""Applies 'predicate' to the result of a previous computation.""" | ||
|
||
|
||
#: Type alias for kinds with one type argument. | ||
Filterable1 = FilterableN[_FirstType, NoReturn, NoReturn] | ||
|
||
#: Type alias for kinds with two type arguments. | ||
Filterable2 = FilterableN[_FirstType, _SecondType, NoReturn] | ||
|
||
#: Type alias for kinds with three type arguments. | ||
Filterable3 = FilterableN[_FirstType, _SecondType, _ThirdType] |
This file contains 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 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 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,41 @@ | ||
from typing import Callable, TypeVar | ||
|
||
from returns.interfaces.filterable import FilterableN | ||
from returns.primitives.hkt import Kinded, KindN, kinded | ||
|
||
_FirstType = TypeVar('_FirstType') | ||
_SecondType = TypeVar('_SecondType') | ||
_ThirdType = TypeVar('_ThirdType') | ||
_FilterableKind = TypeVar('_FilterableKind', bound=FilterableN) | ||
|
||
|
||
def filter_( | ||
predicate: Callable[[_FirstType], bool], | ||
) -> Kinded[Callable[ | ||
[KindN[_FilterableKind, _FirstType, _SecondType, _ThirdType]], | ||
KindN[_FilterableKind, _FirstType, _SecondType, _ThirdType], | ||
]]: | ||
""" | ||
Applies predicate over container. | ||
|
||
This is how it should be used: | ||
|
||
.. code:: python | ||
|
||
>>> from returns.maybe import Some, Nothing | ||
|
||
>>> def example(value): | ||
... return value % 2 == 0 | ||
|
||
>>> assert filter_(example)(Some(5)) == Nothing | ||
>>> assert filter_(example)(Some(6)) == Some(6) | ||
|
||
""" | ||
|
||
@kinded | ||
def factory( | ||
container: KindN[_FilterableKind, _FirstType, _SecondType, _ThirdType], | ||
) -> KindN[_FilterableKind, _FirstType, _SecondType, _ThirdType]: | ||
return container.filter(predicate) | ||
|
||
return factory |
This file contains 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,11 @@ | ||
from returns.maybe import Nothing, Some | ||
|
||
|
||
def test_maybe_filter(): | ||
"""Ensures that .filter works correctly.""" | ||
def factory(argument: int) -> bool: | ||
return argument % 2 == 0 | ||
|
||
assert Some(5).filter(factory) == Nothing | ||
assert Some(6).filter(factory) == Some(6) | ||
assert Nothing.filter(factory) == Nothing |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future note: we can also make an
overload
forTypeGuard
case.See python/typeshed#6140
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate a bit what can we do with type guard here? We are not filtering for example a list with different types. Filter executed on
_FilterableType
always returns_FilterableType
. Did I miss something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, this was a self note 🙂
This is even not supported by mypy yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like mypy supports
TypeGuard
now 🎉 :