Skip to content

Inlining a list argument into call of function that accepts a protocol produces "incompatible type" error #21563

@maybeetree

Description

@maybeetree

Bug Report

When passing a list of ints as an argument to a function that accepts a generic protocol, for some reason it is necessary to first bind the list to a variable. Inlining the list as a literal expression in the function call produces a [list-item] error from mypy. What's interesting is that this error references a generic type T, which is not declared anywhere in my program.

To Reproduce

from typing import TypeVar, Protocol, Self

K = TypeVar('K', covariant=True)
class Concattable(Protocol[K]):
    def __add__(self, other: Self) -> Self: ...

J = TypeVar('J')
def duplicate(seq: Concattable[J]) -> Concattable[J]:
    return seq + seq

# No issue
mylist = [1,2,3]
assert duplicate(mylist) == [1,2,3,1,2,3]

# Issue
assert duplicate([1,2,3]) == [1,2,3,1,2,3]

Expected Behavior

I expect that both function calls do not produce any mypy errors.

Actual Behavior

While the first call is fine, the second one produces an error for each element of the list.
It also makes mention of a generic type T, which is not declared anywhere in my program.

construct-new-sequence.py:16: error: List item 0 has incompatible type "int"; expected "T"  [list-item]
construct-new-sequence.py:16: error: List item 1 has incompatible type "int"; expected "T"  [list-item]
construct-new-sequence.py:16: error: List item 2 has incompatible type "int"; expected "T"  [list-item]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

$ pip --version
pip 26.1.1 from /venv/lib/python3.12/site-packages/pip (python 3.12)
$ python --version
Python 3.12.13
$ mypy --version
mypy 2.1.0 (compiled: yes)
$ pip freeze | grep mypy
mypy==2.1.0
mypy_extensions==1.1.0
  • There is no mypy.ini or other mypy configuration files.
  • I invoke mypy simply from the command line by running mypy --strict myfile.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions