Pyright type checker won't let me pattern match just `Success` and `Failure` for a function returning `Result`
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
Riproduci il report con divtest.py usando uv run pyright ., concentrandoti sulla funzione div e sui suoi casi di match Success/Failure. Confronta questo comportamento con l’esempio documentato di pattern matching e determina se l’issue riguarda returns o Pyright; il lavoro è concluso quando il match esaustivo viene accettato o viene preparato un report Pyright confermato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug report
What's wrong
from returns.result import Failure, Success, safe
@safe
def div(first_number: int, second_number: int) -> int:
return first_number // second_number
def test_div() -> str:
match div(1, 0):
# Matches if the result stored inside `Success` is `10`
case Success(10):
return 'Result is "10"'
# Matches any `Success` instance and binds its value to the `value` variable
case Success(value):
return f'Result is "{value}"'
# Matches if the result stored inside `Failure` is `ZeroDivisionError`
case Failure(ZeroDivisionError()):
return '"ZeroDivisionError" was raised'
# Matches any `Failure` instance
case Failure(_):
return 'The division was a failure'
Given this code, taken directly from the documentation, but wrapped in a function with str return type instead of prints, I get the following error from pyright:
➜ divtest git:(main) ✗ uv run pyright .
/Users/Shared/Development/divtest/divtest.py
/Users/Shared/Development/divtest/divtest.py:9:19 - error: Function with declared return type "str" must return value on all code paths
"None" is not assignable to "str" (reportReturnType)
1 error, 0 warnings, 0 informations
I can fix this error by adding a catch-all branch _, but this kinda defeats the purpose of using a result and ruins type safety.
How is that should be
The type checker should know that Result and Success are the only possible return types for this function. The type checker shouldn't be asking for the _ branch.
System information
➜ divtest git:(main) ✗ uv tree
Resolved 5 packages in 1ms
divtest v0.1.0
├── pyright v1.1.407
│ ├── nodeenv v1.9.1
│ └── typing-extensions v4.15.0
└── returns v0.26.0
└── typing-extensions v4.15.0
➜ divtest git:(main) ✗ uv run python --version
Python 3.12.11
Comment
Please advise whether this is a returns issue or should I report it to pyright. I'm also open to creating a plugin for pyright if needed or otherwise investing some effort into resolving this. Also, I tried to join the telegram community and despite solving the numeric challenge I got kicked out a minute after entering.
Thanks
- 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#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
-
essnmx good first issue
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
-
[Feature] 奇物选择添加优先级 Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
syfoud/Simulated_Scepter#174 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Giskard-AI/giskard-oss#2840 · 1 commento ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success Apertaarea: repo bug perceived difficulty: 2
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
yeti-platform/yeti#1380 ·