Skip to content

Lambda contextual typing doesn't work with union of callables #219

Closed as duplicate

Description

@hurryabit

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

Activity

  1. yangdanny97 commented on May 16, 2025

    @yangdanny97
    Collaborator

    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.

  2. added theissue type on May 16, 2025
  3. samwgoldman commented on Jul 16, 2025

    @samwgoldman
    Member

    related to #164

  4. stroxler commented on Aug 28, 2025

    @stroxler
    Contributor

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

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

  5. added this to the V1 Release milestone on Jan 29, 2026
  6. changed the title [-]Lambdas don't play well with unions of callables[/-] [+]False positive in lambda annotated w/ Callable[/+] on Feb 27, 2026
  7. rchen152 commented on Feb 27, 2026

    @rchen152
    Contributor

    Wait, isn't that a legitimate error without the union? Adding an int and a str.

  8. yangdanny97 commented on Feb 28, 2026

    @yangdanny97
    Collaborator

    oh lol, i didn't see the quotes around the 1

  9. changed the title [-]False positive in lambda annotated w/ Callable[/-] [+]Lambda contextual typing doesn't work with union of callables[/+] on Feb 28, 2026
  10. self-assigned this
    on Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

contextual-typingissues related to contextual typinggoogleissues from google

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions