mypy
ad561646 - Ensure we always infer a valid fallback type for lambda callables (#13576)

Commit
3 years ago
Ensure we always infer a valid fallback type for lambda callables (#13576) Fixes #9234 This diff fixes a bug in `infer_lambda_type_using_context` where it blindly trusted and reused whatever fallback the context callable was using. This causes mypy to crash in the case where the context was a dynamic constructor. This is because... 1. The constructor has a fallback of `builtins.type` 2. The Callable object `infer_lambda_type_using_context` returns uses this fallback as-is. 3. The join of the LHS and RHS of the ternary ends up being a `def (Any) -> Any` with a fallback of `builtins.type`. See: https://github.com/python/mypy/blob/7ffaf230a3984faaf848fe314cf275b854a0cdb0/mypy/join.py#L578 4. Later, we call `CallableType.is_type_obj()` and `CallableType.type_object()`. The former ends up succeeding due to the fallback, but the latter fails an assert because the return type is Any, not an Instance: https://github.com/python/mypy/blob/7ffaf230a3984faaf848fe314cf275b854a0cdb0/mypy/types.py#L1771 I opted to fix this by modifying `infer_lambda_type_using_context` so it overrides the fallback to always be `builtins.function` -- I don't think it makes sense for it to be anything else.
Author
Parents
Loading