Skip to content

when descendents of a common base implement a shared protocol, a ternary returning either cannot be assigned to the protocol #16281

Closed as not planned
@glyph

Description

@glyph

Bug Report

When descendents of a common base implement a shared protocol, a ternary returning either cannot be assigned to the protocol.

To Reproduce

from typing import Protocol


class O:
    def y(self) -> None:
        ...


class A(O):
    def x(self) -> None:
        ...


class B(O):
    def x(self) -> None:
        ...


class P(Protocol):
    def x(self) -> None:
        ...


def choice() -> bool:
    return True


p: P = B() if choice() else A()

ab: A | B = B() if choice() else A()

p2: P = ab

https://mypy-play.net/?mypy=latest&python=3.11&gist=8e6bdde0ef585f354461226647d2b66f

Expected Behavior

This should not produce errors.

Actual Behavior

main.py:28: error: Incompatible types in assignment (expression has type "O", variable has type "P")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.6.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): non
  • Python version used: 3.11

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