dry-python / dry-python/returns

Feature Request: Convenience constructor for Result

オープン
#1,627 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug
主要言語
Python
スター
4.4k
フォーク
155
平均マージ
2時間 27分
マージ済み PR(30日)
22

説明

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](https://www.scala-lang.org/api/2.13.6/scala/util/Try.html) (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!

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。