Skip to content

"Incompatible types in assignment" on assignment of ternary operator of instances of classes with __call__ #16824

@ZeeD

Description

@ZeeD

Bug Report

I'm facing a false positive with the ternary assignment on a callable

To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=e0b946c6f8cc1379b98c755cdc773b92

from collections.abc import Callable

class Foo:
    def __call__(self) -> None: ...
class Bar:
    def __call__(self) -> None: ...

def foo() -> None: ...
def bar() -> None: ...

def fun(b: bool) -> None:
    error: Callable[[], None] = Foo() if b else Bar()

    workaround: Callable[[], None]
    if b:
        workaround = Foo()
    else:
        workaround = Bar()

    noerror: Callable[[], None] = foo if b else bar

Expected Behavior
No errors

Actual Behavior

Incompatible types in assignment (expression has type "object", variable has type "Callable[[], None]") [assignment]

Your Environment

mypy 1.8.0 (compiled: yes)
strict = true
Python 3.12.1

(edit: simplified example)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions