Skip to content

Conditional expression produces different result than if (w/ callable())Β #8265

@paravoid

Description

@paravoid

I saw a bunch of different bugs and chatter for both conditional expressions and but I'm unsure if this behavior is reported on any of them. Apologies if this is noise.

This is with mypy 0.761 (with no arguments) on Python 3.7.3, inline:

foo: Union[int, Callable[[Any], int]]
bar: Callable[[Any], int]

foo = lambda _: 0

# case A
# no errors:
if callable(foo):
    bar = foo
else:
    bar = lambda _: foo

# case B
# error: Incompatible return value type (got "Union[int, Callable[[int], int]]", expected "int")
bar = foo if callable(foo) else lambda _: foo

So it seems the behavior differs on whether the if is inline, which I found really surprising.

[Separately, setting foo to 0 also fails -- but on both of those cases, which I suppose would be a limitation, rather than a bug]

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions