Feature Request: Convenience constructor for Result
まだ誰も着手していません。
評価
調査の方向性
まず、既存の Result コンストラクターと、issue で言及されている Maybe.from_optional の実装を読んでください。callable ベースのコンストラクターで例外を表現しつつ、一般的な失敗型への対応を維持する方法を調査し、その後、関連するテストを特定してください。API 設計が確定し、その動作と型付けがテストでカバーされれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Hello! Apologies for filing as a "bug" but didn't see another issue type I could file as.
I was wondering if there was any appetite for an additional constructor on Result that would implement something akin to Scala's Try constructor (e.g. https://alvinalexander.com/scala/try-success-failure-example-reading-file/)
As a very primitive example, I often use a utility function that looks something like this:
def res_lift(lambda_function: Callable[[], T]) -> Result[T, Exception]:
try:
return Result.from_value(lambda_function())
except Exception as e:
return Result.from_failure(e)
So that I can do something like res_lift(lambda: something_that_might_error()) and easily get a ResultE out. This would kind of be like Maybe's .from_optional method (https://returns.readthedocs.io/en/latest/pages/maybe.html#maybe-container), except it would be for ResultE.
I'm not totally sure it's feasible to implement this in a way that's general for all Result Failure types (I recognize that Exception isn't the only Failure type that people use), but if there's interest, I'd be happy to try and put together a PR for this or collaborate however would be useful!
- 主要言語
- Python
- スター
- 4.4k
- フォーク
- 154
- 平均マージ
- 3時間 5分
- マージ済み PR(30日)
- 22
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dry-python/returns のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
dry-python/returns#2394 · リアクション 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
dry-python/returns#2365 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
dry-python/returns#2355 · コメント 1 件 ·
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
dry-python/returns#2295 · コメント 4 件 · リアクション 1 件 ·
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
dry-python/returns#2253 · コメント 2 件 ·
dry-python/returns の issue をすべて見る
似ている issue
-
bug confirmed issue
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
open-webui/open-webui#30750 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
good first issue
難易度 1/5 1時間未満 初心者へのやさしさ 90/100