Skip to content

comparison-overlap error not triggered for booleans #11364

Open

Activity

  1. JukkaL commented on Oct 20, 2021

    @JukkaL
    Collaborator

    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.

  2. KotlinIsland commented on Mar 2, 2022

    @KotlinIsland
    Contributor
    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.

  3. KotlinIsland commented on Mar 16, 2022

    @KotlinIsland
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-overlapOverlapping equality check

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions