dry-python / dry-python/returns

can't compose @future_safe coroutines that specify picky exceptions

Abierto
#2,080 3 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Python
Estrellas
4.4k
Forks
155
Merge medio
2 h 27 min
PR fusionados (30 d)
22

Descripción

Given coroutines `coro1`, `coro2`:

```python
@future_safe(exceptions=(ConnectionError,))
async def coro1(c: int | None) -> int:
if c is None:
raise ConnectionError("not connected")
await asyncio.sleep(0) # emulate I/O
return c

@future_safe(exceptions=(ZeroDivisionError,))
async def coro2(n: int) -> float:
await asyncio.sleep(0) # emulate I/O
return 1 / n
```

I want to combine them via bind: `coro1(c).bind(coro2)`. It results in type error:

```shell
$ uvx --with 'returns[compatible-mypy]==0.25.0' mypy mre.py; ./mre.py
mre.py:30: error: Argument 1 to "bind" of "FutureResult" has incompatible type "Callable[[int], FutureResult[float, ZeroDivisionError]]"; expected "Callable[[int], KindN[FutureResult[Any, Any], float, ConnectionError, Any]]" [arg-type]
```

where `mre.py`:

```python
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "returns >=0.25.0",
# ]
# ///
from typing import assert_never
from returns.result import Success, Failure
from returns.io import IOSuccess, IOFailure
from returns.future import future_safe

@future_safe(exceptions=(ConnectionError,))
async def coro1(c: int | None) -> int:
if c is None:
raise ConnectionError("not connected")
await asyncio.sleep(0) # emulate I/O
return c

@future_safe(exceptions=(ZeroDivisionError,))
async def coro2(n: int) -> float:
await asyncio.sleep(0) # emulate I/O
return 1 / n

async def run() -> None:
for c in [2, 0, None]:
match await coro1(c).bind(coro2):
case IOSuccess(Success(r)):
assert r == 1 / 2, r
case IOFailure(Failure(ZeroDivisionError(args=(msg,)))):
assert msg == "division by zero", msg
case IOFailure(Failure(ConnectionError(args=(msg,)))):
assert msg == "not connected", msg
case _ as unreachable:
assert_never(unreachable) # type: ignore[arg-type]

if __name__ == "__main__":
import asyncio

asyncio.run(run())
```

If picky exceptions are removed:
```python
@future_safe
async def coro1(c: int | None) -> int: ...

@future_safe
async def coro2(n: int) -> float: ...
```
I get the desired behavior that coroutines are composed without type errors but these declarations catch too much.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.