python / python/mypy

Spurious error when adding two list

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

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

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

説明

Bug Report

Mypy reports Unsupported operand types for + when concatenating two lists, when the second operand is a subtype of the first.

This was previously mentioned in this comment in #5492

Below is a simple concise example

To Reproduce

class A:
    pass


class B(A):
    pass


def foo(a: list[A]) -> None:
    pass


l1: list[A] = [A(), B()]
l2: list[B] = [B()]
l3 = l1 + l2
foo(l2 + l1)  # OK, no error
foo(l3)  # also OK
foo(
    l1 + l2
)  # ERROR: Unsupported operand types for + ("List[A]" and "List[B]")  [operator]mypy(error)

gist link

Expected Behavior
No error on the last line

Actual Behavior
Reports spurious error

Your Environment

  • Mypy version used: mypy 1.2.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): from pyproject.toml
ignore_missing_imports = true
show_column_numbers = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_unused_configs = true
follow_imports = "normal"

plugins = ["pydantic.mypy"]
  • Python version used: 3.10

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

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

はじめの一歩

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

調査の方向性

まず、簡潔な Python の例を mypy 1.2.0 で実行し、l1 + l2 と l2 + l1 の診断を比較します。issue にはソースファイルもテストパスも記載されていないため、この再現例からリスト加算の型チェック経路をたどってください。完了条件は、最終的な l1 + l2 式で unsupported-operand エラーが発生せず、既存の有効なケースが引き続き有効であることです。

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

評価

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

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

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