python / python/mypy

In a function call, if a variable accessed in an iteratable doesn't have an attribute, mypy picks the first overload available, creating spurious further list-item errors.

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

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

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

説明

Bug Report

If a variable accessed in an iteratable doesn't have an attribute ([attr-defined] error), and that iterable is then passed to a function, mypy seems to pick the first overload available to continue typechecking against that function, creating spurious further list-item errors, even when more information is available to deduce the correct type. The code below should make more clear what I mean.

To Reproduce
mypy-play gist

from typing import Any, Iterable, overload

d = 42 #example value without attributes

@overload
def foo(x: Iterable[str]) -> None: ...
@overload
def foo(x: Iterable[int]) -> None: ...

def foo(x: Any) -> None:
  return None

# Pass the checker, correctly
foo(["ok", "ok"])
foo([1, 1])

# Fail the checker, correctly, with [list-item] errors (although they all assume the str override is the authoritative one)
foo(["ok", 1])
foo([1, "ok"])
foo([None, "ok"])

# Fail the checker, but the int ones generate spurious list-item errors.
foo(["ok", d.x])
foo([2, d.x])
foo([d.x, "ok"])
foo([d.x, 2])

# This doesn't generate an [attr-defined] error, but still produces a spurious list-item error!
foo([d.x, 2]) #type: ignore[attr-defined]

Expected Behavior

If a function is declared to take an Iterable, with multiple types of Iterable specified by overload decorators, then an [attr-defined] error inside the iterable the function is called upon should not prevent mypy from deducing the correct type of that iterable from its other contents. The correct type of the iterable should still be deduced.

Actual Behavior

However, what actually happens is that if an [attr-defined] error occurs inside of the list I am passing to the function, mypy just choses the first overload. In this case, Iterable[str]. Therefore, spurious [list-item] errors are created when the other member of the list is an int. The [list-item] errors in the below output for lines 24, 26, and 29 are spurious:

test.py:18: error: List item 1 has incompatible type "int"; expected "str"  [list-item]
test.py:19: error: List item 0 has incompatible type "int"; expected "str"  [list-item]
test.py:20: error: List item 0 has incompatible type "None"; expected "str"  [list-item]
test.py:23: error: "int" has no attribute "x"  [attr-defined]
test.py:24: error: List item 0 has incompatible type "int"; expected "str"  [list-item]
test.py:24: error: "int" has no attribute "x"  [attr-defined]
test.py:25: error: "int" has no attribute "x"  [attr-defined]
test.py:26: error: "int" has no attribute "x"  [attr-defined]
test.py:26: error: List item 1 has incompatible type "int"; expected "str"  [list-item]
test.py:29: error: List item 1 has incompatible type "int"; expected "str"  [list-item]
test.py:29: note: Error code "list-item" not covered by "type: ignore" comment
Found 10 errors in 1 file (checked 1 source file)

Here is a --pretty listing of the relevant errors:

test.py:24: error: List item 0 has incompatible type "int"; expected "str"  [list-item]
    foo([2, d.x])
         ^
test.py:24: error: "int" has no attribute "x"  [attr-defined]
    foo([2, d.x])
            ^~~
test.py:26: error: "int" has no attribute "x"  [attr-defined]
    foo([d.x, 2])
         ^~~
test.py:26: error: List item 1 has incompatible type "int"; expected "str"  [list-item]
    foo([d.x, 2])
              ^
test.py:29: error: List item 1 has incompatible type "int"; expected "str"  [list-item]
    foo([d.x, 2]) #type: ignore[attr-defined]
test.py:29: note: Error code "list-item" not covered by "type: ignore" comment

29 is especially annoying, because the [attr-defined] ignore does not suppress all warnings that indirectly result from the [attr-defined] error!

You can also switch the order of the overloads in the code to see that mypy now complains that its arguments are str when it needs ints, instead of vice-versa.

Your Environment

  • Mypy version used: 1.9.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: Python 3.11.0 and 3.12

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

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

はじめの一歩

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

調査の方向性

まず、inline の例またはリンクされた mypy-play gist を使い、overload の順序と attr-defined を無視するケースを含めて、mypy 1.9.0 で問題を再現します。overload の選択と iterable の要素の推論を追跡し、その後、既知のリスト項目によって正しい overload が決定され、誤った list-item エラーが発生しない一方で attr-defined エラーは残ることを示す回帰テストのカバレッジを追加します。

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

評価

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

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

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