Skip to content

bogus complaint when checking Callable arguments #18689

Open
@MarcelloPerathoner

Description

@MarcelloPerathoner

Bug Report

mypy complains about correct code. it confuses the arguments to a Callable parameter with the return value of the function.

To Reproduce

https://gist.github.com/mypy-play/59b8705d6737e1f8f4855d7b2270e7e9

Expected Behavior

Code checks OK.

Actual Behavior

Bogus error:
Argument 1 to "test" has incompatible type "Callable[[str], bool]"; expected "Callable[[float | str], bool]" [arg-type]

Your Environment

mypy 1.15.0 (compiled: yes)
Python 3.13.2

Activity

sterliakov

sterliakov commented on Feb 17, 2025

@sterliakov
Collaborator

This only happens when unpacking the call result directly. (playground)

from typing import TypeVar, Callable

T = TypeVar("T")

def predicate(a: str) -> bool:
    return True

def test(f: Callable[[T], bool]) -> tuple[float | T, bool]:
    return 1.0, True
    

reveal_type(test(predicate))  # N: Revealed type is "tuple[Union[builtins.float, builtins.str], builtins.bool]"

res = test(predicate)
reveal_type(res)  # N: Revealed type is "tuple[Union[builtins.float, builtins.str], builtins.bool]"
a1, b1 = res
reveal_type(a1)  # N: Revealed type is "Union[builtins.float, builtins.str]"
reveal_type(b1)  # N: Revealed type is "builtins.bool"

a, b = test(predicate)  # E: Argument 1 to "test" has incompatible type "Callable[[str], bool]"; expected "Callable[[float | str], bool]"  [arg-type]
reveal_type(a)  # N: Revealed type is "Union[builtins.float, builtins.str]"
reveal_type(b)  # N: Revealed type is "builtins.bool"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      bogus complaint when checking Callable arguments · Issue #18689 · python/mypy