Improve motivating example for Maybe
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 32/100
- Issue 類型
- 文件
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- python
研究方向
首先找到包含目前 Maybe 動機範例的文件部分;issue 中沒有指定檔案或測試。將該範例與所述的 Python 替代方案進行比較,並釐清預期的使用情境。當文件能夠呈現可信的動機,而不把含義不明確的 None 回傳值當作一般錯誤處理時,即視為完成。
由索引模型根據 Issue 內容生成。
描述
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.
- 主要語言
- Python
- 星號
- 4.4k
- 分支
- 156
- 平均合併
- 3 小時 44 分鐘
- 30 天內合併 PR
- 22
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
dry-python/returns 的其他 Issue
-
難度 4/5 3-5 天 新手友好度 48/100
dry-python/returns#2394 · 1 個 reaction ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 52/100
dry-python/returns#2365 ·
維護者通常 1 天內回覆
-
難度 5/5 一週以上 新手友好度 25/100
dry-python/returns#2355 · 1 則留言 ·
維護者通常 1 天內回覆
-
bug
難度 4/5 3-5 天 新手友好度 45/100
dry-python/returns#2295 · 4 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
-
bug
難度 4/5 3-5 天 新手友好度 45/100
dry-python/returns#2253 · 2 則留言 ·
維護者通常 1 天內回覆
查看 dry-python/returns 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
enhancement good first issue Stellar Wave trivial
難度 2/5 1-3 小時 新手友好度 75/100
StellarCanary/ProtocolCanary-Fixtures#258 ·
維護者通常 1 天內回覆
-
enhancement
難度 2/5 1-3 小時 新手友好度 70/100
IBM/ai-atlas-nexus#295 ·
維護者通常 6 天內回覆
-
github_actions
難度 2/5 1-3 小時 新手友好度 65/100
Hochfrequenz/aibap.mcp#578 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 75/100
mishraprafful/multihull#150 ·
維護者通常 1 天內回覆