Skip to content

Spurious error when adding two list #15087

Description

@mishamsk

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

classA:
passclassB(A):
passdeffoo(a: list[A]) ->None:
passl1: list[A] = [A(), B()]
l2: list[B] = [B()]
l3=l1+l2foo(l2+l1) # OK, no errorfoo(l3) # also OKfoo(
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions