python / python/mypy

Changing just the return type annotation causes incompatible argument error

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

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Bug Report

When a return type annotation changes from Iterable of Optional typevars into Iterable of Tuples of Optional typevars mypy starts to signal an error in function call arguments (invalid callback argument type).

To Reproduce

from typing import Callable, Iterable, Optional, Tuple, TypeVar

SourceType = TypeVar("SourceType")
TransformedType = TypeVar("TransformedType")
ProcessedType = TypeVar("ProcessedType")


def simple_transform_process(
    source: Iterable[SourceType],
    transform_callback: Callable[[SourceType], Optional[TransformedType]],
    process_callback: Callable[[TransformedType], ProcessedType],
) -> Iterable[Optional[ProcessedType]]:
    for item in source:
        transformed = transform_callback(item)
        if transformed is None:
            yield None
        else:
            yield process_callback(transformed)


def transform_process(
    source: Iterable[SourceType],
    transform_callback: Callable[[SourceType], Optional[TransformedType]],
    process_callback: Callable[[TransformedType], ProcessedType],
) -> Iterable[Tuple[Optional[TransformedType], Optional[ProcessedType]]]:
    for item in source:
        transformed = transform_callback(item)
        if transformed is None:
            yield None, None
        else:
            yield transformed, process_callback(transformed)


def transform(x: str) -> Optional[int]:
    if x:
        return int(x)
    else:
        return None


def process(x: int) -> float:
    return x / 2


def test_simple_transform_process() -> None:
    assert list(simple_transform_process(["", "2", "3"], transform, process)) == [
        None,
        1,
        1.5,
    ]


def test_transform_process() -> None:
    assert list(transform_process(["", "2", "3"], transform, process)) == [
        (None, None),
        (2, 1),
        (3, 1.5),
    ]

(Write your steps here:)

  1. Run mypy on the above code mypy test_mypy_problem.py

Expected Behavior

The code should have passed the type checker for both simple_transform_process call and transform_process call as both of those have the same exact signatures of input parameters.

Actual Behavior

mypy reports the following error on transform_process call:

test_mypy_problem.py:54: error: Argument 3 to "transform_process" has incompatible type "Callable[[int], float]"; expected "Callable[[Optional[int]], Optional[float]]"

The expected type of argument should be Callable[[int], float], just like for simple_transform_process and the code should pass the type checker.

Your Environment

  • Mypy version used: 0.790, 0.800, mypy 0.820+dev.dc4f0af163ed3748311115fd896494262c0dc644
  • Mypy command-line flags: mypy test_mypy_problem.py
  • Mypy configuration options from mypy.ini (and other config files): no config file used
  • Python version used: Python 3.8.5
  • Operating system and version: Ubuntu 20.04.2 LTS

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

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

はじめの一歩

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

調査の方向性

test_mypy_problem.py の再現コードから始め、報告されている Python と mypy のバージョンを使って mypy test_mypy_problem.py を実行します。ジェネリック推論が transform_process コールバックを simple_transform_process と比較してどのように処理するかを追跡し、その後、両方の呼び出しが正常に型チェックを通過することを示すリグレッションテストを追加します。

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

評価

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

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

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