Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Improve motivating example for Maybe

未关闭
#760 2 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
32/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
documentation

调研方向

首先找到包含当前 Maybe 动机示例的文档部分;issue 中没有指定文件或测试。将该示例与所述的 Python 替代方案进行比较,并明确预期的使用场景。当文档能够呈现可信的动机,而不把含义不明确的 None 返回值当作常规错误处理时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

bug documentation

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
派生
154
平均合并
3 小时 5 分钟
30 天内合并 PR
22

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dry-python/returns 的其他 Issue

查看 dry-python/returns 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。