Pyright type checker won't let me pattern match just `Success` and `Failure` for a function returning `Result`
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 45/100
Rechercherichtung
Reproduziere den Bericht mit divtest.py unter Verwendung von uv run pyright ., wobei der Fokus auf der Funktion div und ihren Success/Failure-Match-Fällen liegt. Vergleiche dieses Verhalten mit dem dokumentierten Pattern-Matching-Beispiel und bestimme, ob das Issue in returns oder Pyright gehört; abgeschlossen ist die Aufgabe, wenn der exhaustive Match akzeptiert wird oder ein bestätigter Pyright-Bericht vorbereitet ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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
- Vorherrschende Sprache
- Python
- Sterne
- 4.4k
- Forks
- 155
- Ø Merge
- 2 Std. 13 Min.
- Gemergte PRs (30 T.)
- 20
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus dry-python/returns
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 48/100
dry-python/returns#2394 · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 52/100
dry-python/returns#2365 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
dry-python/returns#2355 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 45/100
dry-python/returns#2253 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Incorrect result type for `returns.curry.partial` with keyword arguments for positional parametersOffenbug
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 45/100
dry-python/returns#2191 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in dry-python/returns
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
BasedHardware/omi#20271 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 92/100
openai/openai-cookbook#3153 ·
Maintainer antworten meist innerhalb von 1 Tag
-
cvss-severity:high devguard l3montree-cybersecurity/devguard/devguard pkg:golang/github.com/l3montree-dev/devguard risk:low state:open
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
l3montree-dev/devguard#3146 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
bug confirmed issue
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
open-webui/open-webui#31849 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag