Improve motivating example for Maybe
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 32/100
- Issue-Typ
- Dokumentation
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- python
- Bereich
- documentation
Rechercherichtung
Beginne damit, den Dokumentationsabschnitt zu finden, der das aktuelle Maybe-Motivationsbeispiel enthält; in der Issue wird weder eine Datei noch ein Test genannt. Vergleiche das Beispiel mit den genannten Python-Alternativen und kläre den vorgesehenen Anwendungsfall. Erledigt ist die Aufgabe, wenn die Dokumentation eine glaubwürdige Motivation darstellt, ohne mehrdeutige None-Rückgaben als gewöhnliche Fehlerbehandlung zu behandeln.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
The current example motivating the use of Maybe is somewhat misleading because it solves a made-up problem:
Alleged original "python" code:
if user is not None:
balance = user.get_balance()
if balance is not None:
credit = balance.credit_amount()
if credit is not None and credit > 0:
discount_program = choose_discount(credit)
Alleged "better" solution using Maybe:
discount_program: Maybe['DiscountProgram'] = Maybe.from_optional(
user,
).bind_optional( # This won't be called if `user is None`
lambda real_user: real_user.get_balance(),
).bind_optional( # This won't be called if `real_user.get_balance()` is None
lambda balance: balance.credit_amount(),
).bind_optional( # And so on!
lambda credit: choose_discount(credit) if credit > 0 else None,
)
Usual python code solving this exact problem:
try:
discount_program = choose_discount(user.get_balance().credit_amount())
except AttributeError:
pass
The example is based on the very bad habit of signaling errors by return values, e.g. returning None.
No sane (python) developer would write a function that returns None in case of an error unless there is good reason for it, it is properly documented and returning None immediately and unambiguously tells the caller what went wrong. When exceptions occur, exceptions should be raised.
For example, credit_amount() returning None conveys no meaning at all. No credit? Credit amount == 0? Credit amount < 0? Raccoons taking over the world?
And even if one had to use flawed 3rd party code like this, there is a shorter and more concise version to handle this without Maybe.
I believe there is a legitimate use case for Maybe, but this is not it.
- Vorherrschende Sprache
- Python
- Sterne
- 4.4k
- Forks
- 154
- Ø Merge
- 3 Std. 5 Min.
- Gemergte PRs (30 T.)
- 22
Beitragsleitfaden
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 ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 52/100
dry-python/returns#2365 ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
dry-python/returns#2355 · 1 Kommentar ·
-
bug
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 45/100
dry-python/returns#2295 · 4 Kommentare · 1 Reaktion ·
-
bug
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 45/100
dry-python/returns#2253 · 2 Kommentare ·
Alle Issues in dry-python/returns
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
browser-use/browser-use#5905 ·
-
type: enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
ynput/ayon-python-api#363 ·
-
bug needs triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
modelscope/FunASR#3728 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
open-compass/opencompass#2655 ·