Skip to content

Seemingly erroneous incompatible type "list[int]"; expected "Iterable[Never]" #16788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JonathonReinhart opened this issue Jan 16, 2024 · 0 comments · May be fixed by #17310 or coderabbit-test/mypy#5
Open
Labels
bug mypy got something wrong

Comments

@JonathonReinhart
Copy link

Bug Report
mypy seems to not like an argument of type Union[T, Iterable[T]] where T = TypeVar("T"), instead considering it to be Iterable[Never].

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=1471ed0391c1e251edc0ecd85a28d684

from typing import List, Iterable, TypeVar, Union

T = TypeVar("T")

def coerce_list(x: Union[T, Iterable[T]]) -> List[T]:
    reveal_type(x)
    raise NotImplementedError()

def test() -> None:
    coerce_list(1)
    coerce_list([1, 2])

Expected Behavior
I expected no errors. I expected [1, 2] to match Iterable[T] with T = int.

Actual Behavior

example.py:6: note: Revealed type is "Union[T`-1, typing.Iterable[T`-1]]"
example.py:11: error: Argument 1 to "coerce_list" has incompatible type "list[int]"; expected "Iterable[Never]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

I'm confused why the revealed type of x looks to be correct (although I don't understand the -1 part), but the expected type of the parameter is Iterable[Never].

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.11.2 (Debian 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
1 participant