dry-python / dry-python/returns

mypy plugin allows false positive error when exhaustively pattern matching on Result

Đang mở
#1,361 18 bình luận 2 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Python
Star
4.4k
Fork
155
Merge trung bình
2 giờ 27 phút
Pull request đã merge (30 ngày)
22

Mô tả

# Bug report

## What's wrong

> Again, this is a **really** cool project ❤️

Consider the following code:

```py
from enum import Enum, auto
import math
from typing import TypeAlias

from returns.result import Failure, Result, Success

class MathError(Enum):
DivisionByZero = auto()
NonPositiveLogarithm = auto()

MathResult: TypeAlias = Result[float, MathError]

def div(x: float, y: float) -> MathResult:
if y == 0.0:
return Failure(MathError.DivisionByZero)

return Success(x / y)

def ln(x: float) -> MathResult:
if x <= 0.0:
return Failure(MathError.NonPositiveLogarithm)

return Success(math.log(x))

def op_(x: float, y: float) -> MathResult:
z = div(x, y)
match z:
case Success(ratio):
return ln(ratio)
case Failure(_):
return z
```

`mypy` configuration in `pyproject.toml`

```toml
[tool.mypy]
ignore_missing_imports = true
strict = true
plugins = [
"returns.contrib.mypy.returns_plugin",
]
```

Running:

```
$ mypy pattern_div.py
pattern_div.py:32: error: Missing return statement
```

Related to #1090

## How is that should be

As far as I can tell (new to `returns`), I'm matching exhaustively here, so I would not expect a `mypy` error.

`mypy` seems to understand exhaustive matching in general. `mypy` does not throw a type checking error in the following snippet.

```py
def test(err: MathError) -> int:
match err:
case MathError.DivisionByZero:
return 0
case MathError.NonPositiveLogarithm:
return 1
```

## System information

- `python` version: 3.10.2
- `returns` version: 0.19.0
- `mypy` version: 0.942

```
$ pip list
Package Version
----------------- -------
black 22.3.0
click 8.1.2
distlib 0.3.4
filelock 3.6.0
isort 5.10.1
mypy 0.942
mypy-extensions 0.4.3
packaging 21.3
pathspec 0.9.0
pep517 0.12.0
pip 22.0.4
pip-tools 6.6.0
platformdirs 2.5.1
pluggy 1.0.0
py 1.11.0
pyparsing 3.0.8
returns 0.19.0
setuptools 62.1.0
six 1.16.0
toml 0.10.2
tomli 2.0.1
tox 3.25.0
typing_extensions 4.1.1
virtualenv 20.14.1
wheel 0.37.1
```

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

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

Đánh giá

Issue này chưa được đánh giá.

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.