Interest check: implement __bool__ for Result and Maybe
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
Direzione di ricerca
L'issue non indica file o test specifici. Inizia individuando le implementazioni di Result e Maybe e i relativi test esistenti di narrowing dei tipi, quindi esamina come vengono distinti Success e Failure. Done deve includere il comportamento concordato di bool per entrambi i tipi, la copertura per i tipi rivelati e la considerazione della compatibilità con le assunzioni esistenti sulla veridicità.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Long before I found Returns, I implemented my own Result monad from scratch. It was a lot of fun, and right now it coexists with Returns as the library it's a part of is being incrementally deprecated. At some point, I decided it would be nice if I could do things like this:
result = some_function() # Result[S, F]
if result:
reveal_type(result) # Success[S]
else:
reveal_type(result) # Failure[F]
It's worked great. I have my own analogue to is_successful, but with __bool__ I don't even have to import it. Also, it actually works better than my functions: my analogue is a pair of TypeGuard functions (TypeIs didn't exist yet), so even after narrowing the container type in the first if, the else would have no type information about the container. Cf., __bool__(self) -> Literal[False] and __bool__(self) -> Literal[True] make type narrowing work perfectly, and it feels very pythonic, imo.
As I update more and more code to use Returns, instead, I find this to be the one thing about my library that I actually miss. There are a lot of cases where I could reach for match for something as ergonomic, but most of the time I'm only interested in Success vs. Failure; match is overkill if I don't need to peak at the contained value.
The one drawback I anticipate to adding this feature would be the possibility of breaking any code that's relying on the default object.__bool__ behavior to differentiate a Failure from eg. a None... but hopefully, anybody using this library would have their Optionals wrapped in a Maybe already?
- Lingua principale
- Python
- Stelle
- 4.4k
- Fork
- 154
- Merge medio
- 3h 5m
- PR unite (30g)
- 22
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di dry-python/returns
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 52/100
dry-python/returns#2365 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
dry-python/returns#2355 · 1 commento ·
-
bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
dry-python/returns#2295 · 4 commenti · 1 reazione ·
-
bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
dry-python/returns#2253 · 2 commenti ·
-
Incorrect result type for `returns.curry.partial` with keyword arguments for positional parameters Apertabug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
dry-python/returns#2191 · 1 commento ·
Tutte le issue di dry-python/returns
Issue simili
-
bug confirmed issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
open-webui/open-webui#30750 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
good first issue
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100