Skip to content

False negative when passing invalid lambda to Union of higher-order functions #7012

@LeeeeT

Description

@LeeeeT

When doing something with a Union, your code should be valid for every choice of the Union:

type U = int | str

def f(u: U) -> None:
    u.lower()  # type checking error

Having first-order functions as the Union choices and passing a wrong argument to them also gives an error:

from collections.abc import Callable

type Takes[T] = Callable[[T], object]

type U = Takes[int] | Takes[str]

def f(u: U) -> None:
    u(42)  # type checking error

But passing a lambda of a wrong type to a Union of higher-order functions does not produce an error:

from collections.abc import Callable

type Takes[T] = Callable[[T], object]

type U = Takes[Takes[int]] | Takes[Takes[str]]

def f(u: U) -> None:
    u(lambda v: v.lower())  # no error :(

Mypy shows an error for the above code (substituting PEP 695 type aliases with the old ones, since they're not yet supported by mypy), and I expect pyright to do the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions