You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make infer_lambda_type_using_context always set lambda fallback
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 `infer_lambda_type_using_context` returns a CallableType
with this fallback.
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 is supposed to be a guard
for the former, but what happens instead is that the former succeeds
due to the fallback and 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.
0 commit comments