I noticed that mypy forgets type information when going inside the scope of a lambda. Below is a small example to explain:
from typing import Optional, Callable
def wrapper(x: Callable[[], None]):
x()
def foo(a: int):
print(str(a))
a: Optional[int] = 0
if a is None:
exit()
wrapper(lambda: foo(a))
Mypy complains about this snippet with error: Argument 1 to "foo" has incompatible type "None"; expected "int".
However, we already checked that a is not None before. This is surprisingly strange because if we were to call foo(a) directly (without the wrapper), it does work.
I'm using python 3.6.10 with mypy 0.740.
Is this intended behavior? And if so, how would you go about typing this correctly?
Thanks!