Error for lambda containing Any even when type can be inferred
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Using mypy 0.641 with the --disallow-any-expr flag and this sample using @overload
from typing import Callable, Tuple, TypeVar, overload
T = TypeVar('T')
A0 = TypeVar('A0')
A1 = TypeVar('A1')
@overload
def foo(
vars: Tuple[A0],
op: Callable[[A0], T]
) -> T: ...
@overload
def foo(
vars: Tuple[A0, A1],
op: Callable[[A0, A1], T]
) -> T: ...
def foo(vars, op):
pass
def foo_not_overloaded(
vars: Tuple[A0, A1],
op: Callable[[A0, A1], T]
) -> T:
pass
a = 5
b = 3
foo((a, b), lambda a, b: a - b)
produces
typist2.py:36: error: Expression type contains "Any" (has type "Callable[[Any, Any], Any]")
typist2.py:36: error: Expression has type "Any"
However mypy seems to be able to infer the type of the lambda because if I modify the last line to read
foo((a, b), lambda a, b: a - b.bad)
mypy correctly identifies that "int" has no attribute "bad", sprinkling some prints on the mypy source also reveals it figures out it's a (int, int) -> int at some point.
foo_not_overloaded checks without trouble and so does using a plain def instead of the lambda.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供された再現コードを mypy 0.641 で実行するところから始め、foo のオーバーロードされた呼び出しを foo_not_overloaded および単純な def のケースと比較してください。オーバーロードの経路を通じて lambda の推論された型を追跡し、その後、妥当な lambda では disallow-any-expr エラーが発生しなくなり、a - b.bad のバリアントでは引き続き無効な属性が報告されることを確認してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100