pyrefly
pyrefly copied to clipboard
Lambdas don't play well with unions of callables
Describe the Bug
from typing import Callable
f: Callable[[int], int] | Callable[[str], str] = lambda x: x + "1"
should pass but fails with
ERROR 3:60-67: `+` is not supported between `int` and `Literal['1']`
Argument `Literal['1']` is not assignable to parameter with type `int` in function `int.__add__` [[bad-argument-type](https://pyrefly.org/en/docs/error-kinds/#bad-argument-type)]
Sandbox Link
https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCAhgDYlEBGJApgFC3ABchp5V1A2h6jALoA0mFHygAfFmUo0uAZxggBUOQqgBeKOQgUAJkSgAPZvqgBqKACIAjOaA
(Only applicable for extension issues) IDE Information
No response
The way we currently do contextual typing doesn't handle unions well in some cases, it's on our roadmap to improve the behavior here.
related to #164
Issue #164 is now closed, but the fix was specific to TypedDict
This is probably related to (perhaps identical to) #793