advanced patterns with Type Hinting and Mypy in Python 3.10 when Using Callable Types
I'm working on a personal project and I'm attempting to set up I'm working on a project and hit a roadblock. I'm working with unexpected behavior when using type hinting for a function that receives a callable argument. I defined a function that should accept a callback, but when I run `mypy`, it raises an behavior that I wasn't anticipating. The function signature looks like this: ```python from typing import Callable, Any def process_data(callback: Callable[[int], None]) -> None: for i in range(5): callback(i) ``` When I try to pass a simple function like this: ```python def my_callback(x: int) -> None: print(f'Processing: {x}') ``` and call `process_data(my_callback)`, everything works fine. However, if I try to use a lambda function instead: ```python process_data(lambda x: print(f'Processing: {x}')) ``` I get the following `mypy` behavior: ``` mypy: Argument 1 to "process_data" has incompatible type "Callable[[Any], None]"; expected "Callable[[int], None]" ``` I was under the impression that `Any` should be compatible with `int`. I've tried changing the lambda to explicitly indicate the parameter type like so: ```python process_data(lambda x: print(f'Processing: {x}')) # still raises an behavior ``` I also considered using `cast`, but it feels cumbersome for such a simple use case. Is there a more straightforward way to allow such callbacks without running into type checking issues with `mypy`? How can I make my code more type-safe while still allowing flexibility in the callable types? Any insights would be appreciated. My development environment is macOS. I'm coming from a different tech stack and learning Python. Could someone point me to the right documentation? Thanks for taking the time to read this!