Interest check: implement __bool__ for Result and Maybe
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 48/100
Línea de trabajo
El issue no especifica archivos ni pruebas concretos. Empieza localizando las implementaciones de Result y Maybe y sus pruebas existentes de narrowing de tipos; después, revisa cómo se distinguen Success y Failure. Done debe incluir el comportamiento acordado de bool para ambos tipos, cobertura para los tipos revelados y la consideración de la compatibilidad con las suposiciones existentes sobre los valores de verdad.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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?
- Lenguaje dominante
- Python
- Estrellas
- 4.4k
- Forks
- 154
- Merge medio
- 3 h 5 min
- PR fusionados (30 d)
- 22
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de dry-python/returns
-
Composing 0-argument functions Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 52/100
dry-python/returns#2365 ·
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 25/100
dry-python/returns#2355 · 1 comentario ·
-
bug
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
dry-python/returns#2295 · 4 comentarios · 1 reacción ·
-
bug
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
dry-python/returns#2253 · 2 comentarios ·
-
Incorrect result type for `returns.curry.partial` with keyword arguments for positional parameters Abiertobug
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
dry-python/returns#2191 · 1 comentario ·
Todos los issues de dry-python/returns
Issues similares
-
bug confirmed issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
open-webui/open-webui#30750 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
-
good first issue
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100