mypy plugin allows false positive error when exhaustively pattern matching on Result
まだ誰も着手していません。
評価
調査の方向性
示されている pattern_div.py の例と pyproject.toml の mypy 設定を使ってレポートを再現します。次に、returns.contrib.mypy.returns_plugin のエントリーポイントと、存在する場合は既存のプラグインテストを調査します。Success/Failure の網羅的なマッチングで missing-return エラーが発生しなくなり、その例を対象とするリグレッションテストが追加されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug report
What's wrong
Again, this is a really cool project ❤️
Consider the following code:
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
[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.
def test(err: MathError) -> int:
match err:
case MathError.DivisionByZero:
return 0
case MathError.NonPositiveLogarithm:
return 1
System information
pythonversion: 3.10.2returnsversion: 0.19.0mypyversion: 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
- 主要言語
- 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