Skip to content

comparison-overlap error not triggered for booleans #11364

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

Open
DetachHead opened this issue Oct 20, 2021 · 3 comments
Open

comparison-overlap error not triggered for booleans #11364

DetachHead opened this issue Oct 20, 2021 · 3 comments
Labels
bug mypy got something wrong topic-overlap Overlapping equality check

Comments

@DetachHead
Copy link
Contributor

from typing import Literal

foo: Literal["foo"] = "foo"

# error: Non-overlapping equality check (left operand type: "Literal['foo']", right operand type: "Literal['bar']")  [comparison-overlap]
foo == "bar"


bar: Literal[True]

# no error
bar == False

https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-context%2Cshow-column-numbers%2Cshow-error-codes%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-any-explicit%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=4d291b48d0f5858f4b1ec54a1a67e5f6

@DetachHead DetachHead added the bug mypy got something wrong label Oct 20, 2021
@JukkaL
Copy link
Collaborator

JukkaL commented Oct 20, 2021

I think that this is by design. Bools are often used for things like debug flags that can typically be false, but it's still okay to write if DEBUG: print('something'). The idea is that the developer will temporarily modify the value of the flag during a debugging session if they want verbose logging.

if DEBUG == True: seems similar enough to if DEBUG: that they should probably use the same rules.

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Mar 2, 2022

from typing import Literal

foo: Literal["foo"]

# error: Non-overlapping equality check (left operand type: "Literal['foo']", right operand type: "Literal['bar']")  [comparison-overlap]
foo == "bar"
foo != "bar"

# no error
foo != "foo"
foo == "foo"


bar: Literal[True]

# no error
bar == True
bar == False
bar != True
bar != False
if bar: print(1)

# error: Statement is unreachable  [unreachable]
if not bar: print(1)

(Gist)
There are a lot of simple situations that mypy doesn't seem to pick up on, all of these lines of code are suspect given the type annotations.

Bools are often used for things like debug flags

@JukkaL Do you mean debug flags that are used with always-false/always-true(always-X) for the purpose of compilation optimization?

This feature(fix?) would heavily clash with always-X as any variable modified by those flags would give false errors in every usage:

# mypy: always-false=FOO
from typing import TYPE_CHECKING

Foo = True

if TYPE_CHECKING:  # error: condition always true
    ...
if FOO:  # error: condition always false
    ...

If this truly is intended then it is quite non-obvious, and not documented.

I propose that the current rules regarding ignoring bools only be applied to bools that are modified with always-X, and that they should be disabled for normal bools.

I think that as it stands it's an inconsistent feature, as debug options can also be integers or strings, and the benefits of catching true comparison-overlap errors far outweighs this hidden design in my opinion.

@KotlinIsland
Copy link
Contributor

Researching this further, I have discovered that mypy already special cases artificially literal bools:

# mypy: always-true=foo
from typing import Literal

foo: bool

if foo: print(1)  # no error

bar: Literal[True]

if not bar: print(1)  # error: Statement is unreachable

https://mypy-play.net/?mypy=latest&python=3.10&flags=warn-unreachable&gist=155eb9beccbfde903e2b3a5d61f0752e

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-overlap Overlapping equality check
Projects
None yet
Development

No branches or pull requests

4 participants