python / python/mypy

Errors for union of callables could be better

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

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

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

説明

Given this definition (playground):

f: Callable[[int, int], None] | Callable[[int, str], None]

this code results in the following errors:

f(5, 42)  # E: Argument 2 has incompatible type "int"; expected "str"  [arg-type]
f(5, '42')  # E: Argument 2 has incompatible type "str"; expected "int"  [arg-type]

Looking at the errors (which are probably emitted in a for-loop) one would miss the big picture, which is that it's a union of two incompatible types.

Perhaps we could "meet" the callables, thus resulting in:

f(5, 42)  # E: Argument 2 has incompatible type "int"; expected <nothing>  [arg-type]
f(5, '42')  # E: Argument 2 has incompatible type "str"; expected <nothing>  [arg-type]

Notably we handle defaults well, i.e. in this code:

class FInt:
    def __call__(self, a: int, b: int=42) -> None:
        return

class FStr:
    def __call__(self, a: int, b: str='42') -> None:
        return

f: FInt | FStr

we get

f(5, 42)  # E: Argument 2 to "__call__" of "FStr" has incompatible type "int"; expected "str"  [arg-type]
f(5, '42')  # E: Argument 2 to "__call__" of "FInt" has incompatible type "str"; expected "int"  [arg-type]

but no error on

f(5)

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

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

はじめの一歩

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

調査の方向性

リンクされた2つの mypy playground の例から始め、デフォルト引数のケースを含む、呼び出し可能な型のユニオンに対する診断を比較します。報告されたエラーを生成する Callable の引数チェック経路を追跡します。互換性のない Callable シグネチャは満たせないことを診断が伝えつつ、有効なデフォルト引数の動作が維持されれば完了です。

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

評価

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

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

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