python / python/mypy

False positive for function that takes a `Callable` as a parameter and returns a `Tuple` of `Union`

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

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

The following code

from typing import (
    Callable,
    TypeVar,
    Union,
    Tuple,
)


T = TypeVar("T")


def test(pred: Callable[[T], bool]) -> Tuple[Union[T, int]]:
    return (1,)


def main(pred: Callable[[T], bool]) -> None:
    (val,) = test(pred)  # the error appears here


def f(x: int) -> bool:
    return x > 1


main(f)

triggers the mypy error

Argument 1 to "test" has incompatible type "Callable[[T], bool]"; expected "Callable[[Union[T, int]], bool]"

at the line that I marked with a comment. I believe this is a bug as this correction makes no sense to me (of couse, I could be wrong).
I'm aware that this code is meaningless, but it's the simplest sample I was able to produce that causes the error.
From the tests that I've done it seems to depend both on the type variable being used and on the function test returning a Tuple[Union[...]]. So if I replace T with a regular type of if I return either Union[T, int] or Tuple[int] instead of Tuple[Union[T, int]] the error goes away. It also disappears by not upacking the the result of test(pred).

To Reproduce

Take the code above and check it with mypy. That's it.

Expected Behavior

No error triggered.

Actual Behavior

Mypy detects the error reported above.

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: --ignore-missing-imports --follow-imports=silent --show-column-numbers
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.2
  • Operating system and version: Linux x64

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

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

はじめの一歩

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

調査の方向性

まず、提供された Python スニペットを mypy 0.812 で実行し、アンパック呼び出しでのエラーを確認します。Callable、Tuple、Union の型変数の推論を追跡します。完了とは、報告された false-positive エラーなしで再現コードの型チェックが通り、回帰テストのカバレッジによってこの動作が保護されることを意味します。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
42/100

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

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