Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bogus complaint when checking Callable arguments #18689

Open
MarcelloPerathoner opened this issue Feb 16, 2025 · 1 comment
Open

bogus complaint when checking Callable arguments #18689

MarcelloPerathoner opened this issue Feb 16, 2025 · 1 comment
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference topic-type-variables

Comments

@MarcelloPerathoner
Copy link

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

@MarcelloPerathoner MarcelloPerathoner added the bug mypy got something wrong label Feb 16, 2025
@sterliakov
Copy link
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"

@sterliakov sterliakov added the topic-type-context Type context / bidirectional inference label Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference topic-type-variables
Projects
None yet
Development

No branches or pull requests

3 participants