Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#1,361 コメント 18 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
python
領域
tooling

調査の方向性

示されている pattern_div.py の例と pyproject.toml の mypy 設定を使ってレポートを再現します。次に、returns.contrib.mypy.returns_plugin のエントリーポイントと、存在する場合は既存のプラグインテストを調査します。Success/Failure の網羅的なマッチングで missing-return エラーが発生しなくなり、その例を対象とするリグレッションテストが追加されていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug

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

  • 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
主要言語
Python
スター
4.4k
フォーク
154
平均マージ
3時間 5分
マージ済み PR(30日)
22

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

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

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dry-python/returns のほかの issue

dry-python/returns の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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