python / python/mypy

mypy throws `assignment` error for a list created from an empty list

Open
#18,735 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-type-variables topic-union-types
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy fails to proceed sorting of the empty list, considering it's a list[object], see the reproduce script and the output

To Reproduce

a: list[str] | list[int] = []
a = sorted(a)

Expected Behavior

There shouldn't be errors

Actual Behavior

> mypy .
/tmp/test.py:2: error: Incompatible types in assignment (expression has type "list[SupportsDunderLT[Any] | SupportsDunderGT[Any]]", variable has type "list[str] | list[int]")  [assignment]

Your Environment

  • Mypy version used: mypy 1.14.0 (compiled: no)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13.2

In the scope of https://github.com/ClickHouse/ClickHouse/pull/76679

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use /tmp/test.py as the reproducer and run mypy . with mypy 1.14.0 and Python 3.13.2. Investigate the assignment diagnostic for sorted(a) when a is list[str] | list[int]. Done when this example type-checks without an error and a regression check covers it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.