Request: Truthiness of Result, Correct way to filter over Iterable[Result]
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 20/100
Piste de recherche
Commencez par les exemples documentés de Result/Option et les points d’entrée returns.iterables et Fold.collect_all mentionnés dans l’issue. Comparez les recommandations canoniques de filtrage aux idées proposées concernant truthiness, la propriété ok, Filter et l’en-tête de la documentation ; le travail est terminé lorsqu’une recommandation arrêtée ou une modification d’API/documentation délimitée est clairement définie.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Not sure if this is a question, feature request, recommendation, documenting request, or whatnot.
Love what you folks are doing with this library. However, I've been going through the docs and examples and I'm not sure how to handle Result/Option at the boundary with less-functional-style Python. Take the example of filtering over some failure-prone operation.
from returns.result import Result, Success, Failure
def half(x: int) -> Result:
if x & 1:
return Failure('not an even number')
return Success(x // 2)
results = [half(i) for i in range(5)]
results
out:
[<Success: 0>,
<Failure: not an even number>,
<Success: 1>,
<Failure: not an even number>,
<Success: 2>]
Alright, now I want to obtain only the successful results. My very first instinct is to reach for something like [el for el in results if el] / filter(lambda x: x, results), however Failure and Nothing are both Truthy. Alright, I can see how that interface makes sense, but then I want to reach for [el for el in results if el.ok], alas that doesn't exist.
Looking around the repo for inspiration, I find pipeline.is_successful. Alright, that basically casts an UnwrapFailedError to a bool. I suppose I could do that, but seems rather clunky and not that performant. Alternatively, I could do bool(result.unwrap_or(False)), but...ew. Checking isinstance(result, Failure) fails cause Failure is a function, and _Failure is private and thus discouraged.
Ok there's Fold surely there's from returns.iterables import Filter....mmm nope. A list-like container with a .filter method would also suffice, but I'm not seeing anything built-in. Maybe I'm supposed to use something like Fold.collect(results, ??)? But I can't get the accumulator right. Fold.collect(results, Some(())) just gives me a single Failure. I suspect I'd need some sort of iterable.List which acts like list() but supports the Container interface.
Pipelining is all well and good, but in some places I want to start getting my feet wet with Maybes and Results, and it's a very shallow interface between lots of traditional imperative Python.
First, a question: What is the current canonical/recommended way to filter over a sequence of Results/Maybes? (see edit below)
My recommendations:
- I think the most pythonic and intuitive approach would be to define
__bool__()for Nothing and Failure to return False. Nothing is very obviously Falsy, but Failure is a little less clear-cut, though my gut is it's Falsy. This gives the nice advantage offilterworks identically over Result and Maybe. Law of least surprisal. Etc. - If you aren't keen on that, definitely add some
.okproperty (preferred) or.ok()method. I can't think of any reason not to. - It would also be cool to have
iterables.Filteranditerables.ImmutableListor eveniterables.Listif you want to be a filthy heathen, just to be able to.map()over lists. Paircontainer is actually pretty cool and I can think of a few uses. It would be nice if that were initerablesrather than just in the test suite.
I'd be willing to take a stab at writing up and PR-ing any of the above if you are interested.
Edit: Ok I'm a bit of a goofus, apparently I wanted Fold.collect_all(results, Success(())). This still seems a little roundabout. I still think a convenience method Fold.filter(results) makes sense here, whether it's just sugar for Fold.collect_all(foo: List[Bar[Spam]], Bar(())), or checking for truthiness, or whatever.
Might also be good to have a bolded header for Filtering an iterable of containers just below
Collecting an iterable of containers into a single container for, erm, the more oblivious developers out there ;)
- Langage dominant
- Python
- Étoiles
- 4.4k
- Forks
- 156
- Merge moyen
- 3 h 44 min
- PR mergées (30 j)
- 22
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de dry-python/returns
-
Difficulté 4/5 3-5 jours Accessibilité débutants 48/100
dry-python/returns#2394 · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
Composing 0-argument functionsOuverte
Difficulté 4/5 3-5 jours Accessibilité débutants 52/100
dry-python/returns#2365 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
dry-python/returns#2355 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
dry-python/returns#2295 · 4 commentaires · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
dry-python/returns#2253 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de dry-python/returns
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
NousResearch/hermes-agent#136483 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
Les mainteneurs répondent en général sous 1 jour
-
[BUG] LazyStackedTensorDictStore zeroes the last byte of a new key set on the last elementPeut-être pris @peterdsharpe l’a pris aujourd’hui. Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
pytorch/tensordict#2307 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
GrokModel.generate/a_generate pass an OpenAI-style list-of-dicts to xai_sdk.chat.user(), so every call crashes with a protobuf TypeError before any network I/OPeut-être pris @Christian-Sidak l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
confident-ai/deepeval#3436 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour