dry-python / dry-python/returns

Pyright type checker won't let me pattern match just `Success` and `Failure` for a function returning `Result`

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

説明

# Bug report

## What's wrong

```python
from returns.result import Failure, Success, safe

@safe
def div(first_number: int, second_number: int) -> int:
return first_number // second_number

def test_div() -> str:
match div(1, 0):
# Matches if the result stored inside `Success` is `10`
case Success(10):
return 'Result is "10"'

# Matches any `Success` instance and binds its value to the `value` variable
case Success(value):
return f'Result is "{value}"'

# Matches if the result stored inside `Failure` is `ZeroDivisionError`
case Failure(ZeroDivisionError()):
return '"ZeroDivisionError" was raised'

# Matches any `Failure` instance
case Failure(_):
return 'The division was a failure'

```

Given this code, taken directly from the documentation, but wrapped in a function with str return type instead of prints, I get the following error from pyright:

```
➜ divtest git:(main) ✗ uv run pyright .
/Users/Shared/Development/divtest/divtest.py
/Users/Shared/Development/divtest/divtest.py:9:19 - error: Function with declared return type "str" must return value on all code paths
  "None" is not assignable to "str" (reportReturnType)
1 error, 0 warnings, 0 informations
```

I can fix this error by adding a catch-all branch `_`, but this kinda defeats the purpose of using a result and ruins type safety.

## How is that should be

The type checker should know that Result and Success are the only possible return types for this function. The type checker shouldn't be asking for the `_` branch.

## System information

```
➜ divtest git:(main) ✗ uv tree
Resolved 5 packages in 1ms
divtest v0.1.0
├── pyright v1.1.407
│ ├── nodeenv v1.9.1
│ └── typing-extensions v4.15.0
└── returns v0.26.0
└── typing-extensions v4.15.0
➜ divtest git:(main) ✗ uv run python --version
Python 3.12.11
```

## Comment

Please advise whether this is a `returns` issue or should I report it to pyright. I'm also open to creating a plugin for pyright if needed or otherwise investing some effort into resolving this. Also, I tried to join the telegram community and despite solving the numeric challenge I got kicked out a minute after entering.

Thanks

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

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

評価

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

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

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