Skip to content

Assert on Literal field causes spurious "Non-overlapping equality check" #9409

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

Closed
intgr opened this issue Sep 3, 2020 · 2 comments
Closed
Labels
bug mypy got something wrong topic-literal-types topic-overlap Overlapping equality check

Comments

@intgr
Copy link
Contributor

intgr commented Sep 3, 2020

🐛 Bug Report

Mypy is overzealous in constraining the possible values of a Literal field, assuming that it cannot change after being asserted.

I suppose this can also happen with other types, like Union fields, but in the case of Literal it's particularly awkward.

To Reproduce

from dataclasses import dataclass
from typing import Literal


@dataclass
class Data:
    state: Literal["on", "off"]

    def set_state(self, state: Literal["on", "off"]):
        self.state = state


def test_func():
    obj = Data("on")
    assert obj.state == "on"

    obj.set_state("off")
    #   ^^^^^^^^^^^^^^^^ mutation happens here
    assert obj.state == "off"
    #      ^^^^^^^^^
    # mypy: Non-overlapping equality check (left operand type: "Literal['on']", right operand type: "Literal['off']")
  • Mypy version used: mypy 0.780
  • Python version used: 3.8.6
@intgr intgr added the bug mypy got something wrong label Sep 3, 2020
@intgr intgr changed the title Assert on literal field causes spurious "Non-overlapping equality check" Assert on Literal field causes spurious "Non-overlapping equality check" Sep 3, 2020
@intgr
Copy link
Contributor Author

intgr commented Sep 3, 2020

Hrm, this is more-or-less duplicate of #9005, although here I'm using Literal instead of Enum.

@JelleZijlstra JelleZijlstra added the topic-overlap Overlapping equality check label Mar 19, 2022
@hauntsaninja
Copy link
Collaborator

Closing as a duplicate of #9005

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 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-literal-types topic-overlap Overlapping equality check
Projects
None yet
Development

No branches or pull requests

4 participants