Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Improve motivating example for Maybe

Đang mở
#760 2 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
32/100
Loại issue
Tài liệu
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
documentation

Hướng nghiên cứu

Bắt đầu bằng cách xác định phần tài liệu chứa ví dụ minh họa động lực hiện tại cho Maybe; issue không nêu tên file hoặc test nào. So sánh ví dụ với các phương án thay thế bằng Python đã nêu và làm rõ trường hợp sử dụng dự kiến. Công việc được hoàn tất khi tài liệu đưa ra một động lực thuyết phục mà không coi các giá trị trả về None mơ hồ là cách xử lý lỗi thông thường.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
4.4k
Fork
154
Merge trung bình
3 giờ 5 phút
Pull request đã merge (30 ngày)
22

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dry-python/returns

Tất cả issue của dry-python/returns

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.