python / python/mypy

Relation constraints between generic callables in a generic dataclass are ignored when constructed in a function call

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

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

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

説明

Bug Report

Apologies for the lengthy issue title, but I'm not really sure what to call this. Here is the problematic behavior: Suppose I have two related Generic Callables, Callable[[T1], T2] and Callable[[T2], T3]. These callables are packaged together in a dataclass that is parameterized on T1, T2, and T3. For instance:

T1 = TypeVar("T1")
T2 = TypeVar("T2")
T3 = TypeVar("T3")

@dataclass
class FunctionRelation(Generic[T1, T2, T3]):
    func1: Callable[[T1], T2]
    func2: Callable[[T2], T3]

I believe it should be an error to construct an instance of the dataclass such that the return-type of the first callable is not the argument type of the second callable. As far as I can tell, this does work correctly in a simple case.

def func_str_int(x: str) -> int:
    return int(x)

def func_int_str(x: int) -> str:
    return str(x)

def func_str_str(x: str) -> str:
    return x

FunctionRelation(func1=func_str_int, func2=func_int_str)  # OK
FunctionRelation(func1=func_str_int, func2=func_str_str)  # Expected error - Cannot infer T2

However, Mypy incorrectly accepts the bad relation if the dataclass is simply initialized in a function call:

relations: list[FunctionRelation[Any, Any, Any]] = []

def append_to_list(relation: FunctionRelation[T1, T2, T3]) -> None:
    relations.append(relation)

append_to_list(FunctionRelation(func1=func_str_int, func2=func_int_str))  # OK
append_to_list(FunctionRelation(func1=func_str_int, func2=func_str_str))  # Incorrectly accepted by Mypy!
relation = FunctionRelation(func1=func_str_int, func2=func_str_str)  # Expected error
append_to_list(relation)

FWIW, although Mypy accepts the above example, append_to_list(FunctionRelation(func1=func_str_int, func2=func_str_str)) it is (correctly) identified as an error by Pyright, though Pyright does warn against invalid TypeVar use in the function signature. If the signature is changed from FunctionRelation[T1, T2, T3] to FunctionRelation[Any, Any, Any], then neither Mypy nor Pyright complain, which I believe to be incorrect behavior from both type checkers.

It is very possible my understanding of the expected behavior of Mypy is incorrect here, but there does seem to be an inconsistency in how the types are being treated when a function signature can change the type validity of an object passed to that function, even before the function is called. If this is expected, I would sincerely appreciate help understanding why.

Expected Behavior
If I construct a dataclass D with the following spec

@dataclass
class D(Generic[T1, T2, T3]):
    func1: Callable[[T1], T2]
    func2: Callable[[T2], T3]

and I have a function F that accepts a D (regardless of how D is parameterized), then I expect that
IF a statement which constructs a D fails type checking,
THEN a statement that constructs a D and passes it to F should ALSO fail type checking.

Actual Behavior
It is possible to construct a D that fails type checking, but to pass type checking by constructing that same D at the same time as passing it to F.

Your Environment

  • Mypy version used: (mypy 0.960+dev.6eac2403f68e33e317c9f17b434d322291e60543 (compiled: no))
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): strict = true
  • Python version used: 3.10.4
  • Operating system and version: Arch Linux (5.17.3.arch1-1)

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

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

はじめの一歩

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

調査の方向性

まず、strict=true を指定した最小限のジェネリックな dataclass と Callable の例を再現し、直接構築する場合と append_to_list に渡して構築する場合を比較します。dataclass コンストラクターと関数引数に対するジェネリック型の推論を追跡し、その後、どちらの形式も互換性のない Callable の関係を拒否することを示す回帰テストを追加します。

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

評価

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

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

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