pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Lambdas don't play well with unions of callables

Open hurryabit opened this issue 6 months ago • 1 comments

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

hurryabit avatar May 16 '25 15:05 hurryabit

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.

yangdanny97 avatar May 16 '25 16:05 yangdanny97

related to #164

samwgoldman avatar Jul 16 '25 16:07 samwgoldman

Issue #164 is now closed, but the fix was specific to TypedDict

This is probably related to (perhaps identical to) #793

stroxler avatar Aug 28 '25 16:08 stroxler