python / python/mypy

Can't assign result of list addition to list variable covariantly

Open
#16,773 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

I cannot assign a list[int] to a list[int | None] if I make it using addition. However, it works fine if I don't use addition. See the tiny example below.

In this example, of course, I can avoid the problem using the list literal [1, 2], but in the application code where I'm facing this issue I actually need to add two comprehensions and store the result as a variable. It shows the same unexpected behavior.

To Reproduce

a: list[int | None] = [1]         # allowed
b: list[int | None] = [1] + [2]   # not allowed

https://mypy-play.net/?mypy=latest&python=3.12&gist=800cfe12c8689a108440e71e641ad240

Expected Behavior

I expect mypy to behave in one of two ways:

  • Both lines are allowed 👈 this would be very convenient
  • Both lines are not allowed 👈 this would be correct
    • How can the first line be allowed if list is invariant?
    • In this case I would need a way to type-hint a list comprehension

Note:

I expect both lines to be treated the same way. This expectation is reinforced because reveal_type([1]) reports the same type as reveal_type([1] + [2]). If that was not the case the different treatment would be easier to understand.

Actual Behavior

main.py:2: error: Incompatible types in assignment (expression has type "list[int]", variable has type "list[int | None]")  [assignment]
main.py:2: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:2: note: Consider using "Sequence" instead, which is covarian

Your Environment

  • Mypy version used: 1.8.0 and 1.5.1 (latest and version in use on my project)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12

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

Start with the minimal reproduction in main.py and run it against the linked mypy-play example using the reported Python and mypy versions. Trace how the list literal and list-addition expressions are inferred, then make their assignment behavior consistent while preserving list invariance; add or update a regression test for both forms.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.