Skip to content

--warn-unreachable doesn't flag unnecessary comparison to None #20060

@A5rocks

Description

@A5rocks

Here is a full example:

from random import randrange



def f(x: int) -> int | None:

    return None if x == 0 else 2 * x



def g(x: int) -> int:

    return 2 * x



if __name__ == '__main__':

    if f(randrange(10)) is None:

        print('f: got 0')



    if g(randrange(10)) is None:  # <-- unreachable but not flagged

        print('g: got 0')

mypy --strict --warn-unreachable does not raise any warnings, but it should. So this is a FALSE NEGATIVE bug. Or a new feature, whatever, but in any case this situation should be flagged.

Originally posted by @nickdrozd in #18386

This might be intentional, but I think it's counterintuitive and I can't really see a benefit. Making an issue to make sure I get around to investigating this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions