Generic Point Free Function Returns Unexpected Type
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
Riproduci l’output di mypy per test_generic2, concentrandoti sull’entry point bind_result e sulle firme generiche Callable mostrate nel report. Confronta il tipo inferito con il risultato atteso di tipo IOResult[T4, Exception] e conferma che il problema è risolto quando l’esempio supera il controllo dei tipi senza l’errore IOResult annidato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug report
What's wrong
Trying to compose a generic function that converts between different container types fails to type check correctly when using generics. Hopefully I am just using this wrong.
Code:
from typing import Callable, TypeVar
from returns.io import IOResultE
from returns.pointfree import bind_result
from returns.result import ResultE
from typing_extensions import reveal_type
T1 = TypeVar("T1")
T2 = TypeVar("T2")
T3 = TypeVar("T3")
T4 = TypeVar("T4")
# this works correctly
def test(
i: int,
f1: Callable[[int], str],
f2: Callable[[str], IOResultE[int]],
f3: Callable[[int], ResultE[str]],
) -> IOResultE[str]:
f1_res = f1(i)
f2_res = f2(f1_res)
f3_res = bind_result(f3)(f2_res)
reveal_type(f3_res)
return f3_res
# this passes typecheck with returns.io.IOResult[T3`-3, builtins.Exception]
def test_generic(
i: T1,
f1: Callable[[T1], IOResultE[T2]],
f2: Callable[[T2], ResultE[T3]],
) -> IOResultE[T3]:
f1_res = f1(i)
f2_res = bind_result(f2)(f1_res)
reveal_type(f2_res)
return f2_res
# this fails typecheck with returns.io.IOResult[returns.io.IOResult[Any, Any], builtins.Exception]
def test_generic2(
i: T1,
f1: Callable[[T1], T2],
f2: Callable[[T2], IOResultE[T3]],
f3: Callable[[T3], ResultE[T4]],
) -> IOResultE[T4]:
f1_res = f1(i)
f2_res = f2(f1_res)
f3_res = bind_result(f3)(f2_res)
reveal_type(f3_res)
return f3_res
Running mypy:
$ python3 -m mypy test.py
test.py:23: note: Revealed type is "returns.io.IOResult[builtins.str, builtins.Exception]"
test.py:34: note: Revealed type is "returns.io.IOResult[T3`-3, builtins.Exception]"
test.py:47: note: Revealed type is "returns.io.IOResult[returns.io.IOResult[Any, Any], builtins.Exception]"
test.py:48: error: Incompatible return value type (got "IOResult[IOResult[Any, Any], Exception]", expected "IOResult[T4, Exception]")
Found 1 error in 1 file (checked 1 source file)
How is that should be
I would have expected the return type for test_generic2 to be returns.io.IOResult[T4`-3, builtins.Exception]
System information
-
pythonversion: 3.9.12 -
returnsversion: 0.19.0 -
mypyversion: 0.95.0 -
hypothesisversion (if any): -
pytestversion (if any):
- 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 48/100
dry-python/returns#2394 · 1 reazione ·
-
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 ·
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