Skip to content

lambda input type is Any when function type is a union, and no error when lambda does not satisfy any of the types in the union #13036

Open

Description

@DetachHead
from typing import Callable
def f(a: Callable[[int], None] | Callable[[int, str], None]) -> None: ...
    
f(lambda x: reveal_type(x))  # Any

playground

Activity

  1. erictraut commented on Aug 13, 2023

    @erictraut

    The title of this bug is misleading. The real bug here is that mypy doesn't report a type violation for f(lambda x: x). This lambda doesn't satisfy either Callable[[int], None] or Callable[[int, str], None], so it should be flagged as a type violation.

  2. DetachHead commented on Aug 13, 2023

    @DetachHead
    ContributorAuthor

    it still happens when the lambda does satisfy one of the types in the union:

    from typing import Callable
    def f(a: Callable[[int], int] | Callable[[int, str], None]) -> None: ...
    
    f(lambda x: reveal_type(x))  # Any

    i guess that was a mistake in my original post but that means there are two different issues here

  3. changed the title [-]lambda input type is `Any` when function type is a union[/-] [+]lambda input type is `Any` when function type is a union, and no error when lambda does not satisfy any of the types in the union[/+] on Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotations

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions