python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.5k stars 2.83k forks source link

lambda input type is `Any` when function type is a union, and no error when lambda does not satisfy any of the types in the union #13036

Open DetachHead opened 2 years ago

DetachHead commented 2 years ago
from typing import Callable
def f(a: Callable[[int], None] | Callable[[int, str], None]) -> None: ...

f(lambda x: reveal_type(x))  # Any

playground

erictraut commented 1 year ago

The title of this bug is misleading. The real bug here is that mypy doesn't report a type violation for f(lambda x: x). This lambda doesn't satisfy either Callable[[int], None] or Callable[[int, str], None], so it should be flagged as a type violation.

DetachHead commented 1 year ago

it still happens when the lambda does satisfy one of the types in the union:

from typing import Callable
def f(a: Callable[[int], int] | Callable[[int, str], None]) -> None: ...

f(lambda x: reveal_type(x))  # Any

i guess that was a mistake in my original post but that means there are two different issues here