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

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

Closed
glyph opened this issue Oct 17, 2023 · 2 comments
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@glyph
Copy link

glyph commented Oct 17, 2023

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
@glyph glyph added the bug mypy got something wrong label Oct 17, 2023
@erictraut
Copy link

erictraut commented Oct 17, 2023

This is another join-vs-union issue. See #5128, #12009, #12056, #15808, #16055, etc.

@AlexWaygood
Copy link
Member

This is another join-vs-union issue. See #5128, #12009, #12056, #15808, #16055, etc.

Yeah, I think we have enough of these open. I'm going to close this as "duplicate of all of the above" :-)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2023
@AlexWaygood AlexWaygood added the topic-join-v-union Using join vs. using unions label Oct 17, 2023
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-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

3 participants