Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created March 16, 2025 18:23
Show Gist options
  • Save mypy-play/7887dda55773a9af7eaf3028f5e7a9f4 to your computer and use it in GitHub Desktop.
Save mypy-play/7887dda55773a9af7eaf3028f5e7a9f4 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
# remove the typing hints and mypy fails
from typing import Callable, Union
def fun1(x: int) -> Callable[[int], int]:
return lambda y: x + y
x = fun1(1)
reveal_type(x)
def funf(f: Callable[[int], int]) -> int:
return f(1)
y = funf(lambda x: x)
reveal_type(y)
def funz(x):
return x
z: int = funz(3)
reveal_type(z)
def funk(x):
return lambda y: y
# a = funk(1)
a: Callable[..., int] = funk(1)(1)
reveal_type(a)
# (πœ†π‘“ . 𝑓 1) : ((𝐼 β†’ 𝐼 ) & (𝐹 β†’ 𝐹 )) β†’ 𝐼
def funsubs(f) -> Union[Callable[[int], int], Callable[[float], float]]:
return f(1)
b = funsubs(lambda x: x)
# reveals union or float
reveal_type(b(1))
@dibrinsofor
Copy link

mypy contexts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment