Skip to content

Inference of lambda passed as an argument to an overloaded function doesn't work #3597

@hamdanal

Description

@hamdanal

Summary

In this example, inference of the lambda parameters and return type work with the non-overloaded function but fails with the overloaded one even though the overload is correctly resolved (as evidenced by the outer reveal_types)

from collections.abc import Callable
from typing import overload, reveal_type

@overload
def f(a: str, b: Callable[[int], str], /) -> str: ...
@overload
def f(a: bytes, b: Callable[[int], bytes], /) -> bytes: ...
def f(a, b, /): return a

def g[T: (str, bytes)](a: T, b: Callable[[int], T], /) -> T: return a

reveal_type(  # Revealed type: `str`
    f("", reveal_type(lambda x: str(x) * x))  # Revealed type: `(x) -> Unknown`
)
reveal_type(  # Revealed type: `bytes`
    f(b"", reveal_type(lambda x: x.to_bytes(1)))  # Revealed type: `(x) -> Unknown`
)
reveal_type(  # Revealed type: `str`
    g("", reveal_type(lambda x: str(x) * x))  # Revealed type: `(x: int) -> str`
)
reveal_type(  # Revealed type: `bytes`
    g(b"", reveal_type(lambda x: x.to_bytes(1)))  # Revealed type: `(x: int) -> bytes`
)

Playground

Version

ty 0.0.40

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions