Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Issue 1114 #1131

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

Open
CucumisSativus wants to merge 10 commits into dry-python:master
base: master
Choose a base branch
Loading
from CucumisSativus:issue-1114
Open

Conversation

@CucumisSativus
Copy link

@CucumisSativus CucumisSativus commented Nov 16, 2021

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

Refs #1114

A simple implementation of filter for maybe. I am happy to implement something similar for Result, but I'm not entirely sure about the preferred approach.
In Scala it looks more or less like this

 if (p(value)) this else Failure(new NoSuchElementException("Predicate does not hold for " + value))

Not sure if something like this is ok for Python as well.

My second idea was to have a signature like

def filter(predicate, on_failure):
(...)

so user can specify exactly what happens in case of not matching predicate
🙏 Please, if you or your company finds dry-python valuable, help us sustain the project by sponsoring it transparently on https://github.com/sponsors/dry-python. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.

Copy link
Member

We also need to make an interface type for filter. This way we will be able to use with HKT.
See interfaces/ folder.

Copy link
Author

@sobolevn I think i managed to add them, but lets say i'm not 100% sure about those. Looking forward for some feedback :)

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! Just one question 🙂

"""

@abstractmethod
def filter(
Copy link
Member

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 for TypeGuard case.
See python/typeshed#6140

Copy link
Author

@CucumisSativus CucumisSativus Nov 20, 2021

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?

Copy link
Member

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.

Copy link
Contributor

@ariebovenberg ariebovenberg Sep 12, 2022

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 🎉 :

returns/maybe.py Outdated
"""Raises exception for successful container."""
raise UnwrapFailedError(self)

def filter(self, function):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to create a common filter_ function for all possible types? Result, IOResult, ...?

Copy link
Contributor

@ariebovenberg ariebovenberg Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter is special in the sense that it 'empties' the container, which is possible in Maybe, but not in Result (i.e. what would the failure value of Success(4).filter(lambda x: False) be?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see! Than, it looks like we can use MaybeLikeN to limit the input type of filter_?

Copy link
Author

@CucumisSativus CucumisSativus Nov 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sobolevn I used it, please tell me if its something you were looking for. I am not entirely sure if I did it right :)

Copy link
Author

@sobolevn when i'm running poetry run pytest typesafety -p no:cov -o addopts="" --mypy-ini-file=setup.cfg locally I get 830 passed, 1 warning. Any ideas why does it fail on github?

Copy link
Member

I will take a look a bit later this week.

Thanks a lot for the PR!

CucumisSativus reacted with heart emoji

self: _FilterableType,
predicate: Callable[[_FirstType], bool],
) -> Kind1[_FilterableType, _FirstType]:
"""Applies 'predicate' to the result fo a previous computation."""
Copy link
Contributor

@ariebovenberg ariebovenberg Nov 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: fo->of

>>> from returns.maybe import Nothing, Some
>>> from returns.pointfree import filter_
>>> def example(argument: int) -> bool:
Copy link
Contributor

@ariebovenberg ariebovenberg Nov 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would you think of naming the example function? Naming the function is_even makes the example clearer in my mind

Copy link
Author

@sobolevn i think I found it

Copy link

codecov bot commented Dec 12, 2021
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (b782c2e) to head (cca6894).
Report is 624 commits behind head on master.

Additional details and impacted files
@@ Coverage Diff @@
## master #1131 +/- ##
=========================================
 Coverage 100.00% 100.00% 
=========================================
 Files 79 81 +2 
 Lines 2415 2450 +35 
 Branches 208 210 +2 
=========================================
+ Hits 2415 2450 +35 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sobolevn sobolevn sobolevn left review comments

+2 more reviewers

@ariebovenberg ariebovenberg ariebovenberg left review comments

@donbarbos donbarbos donbarbos approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /