Skip to content

Flag None check on value that cannot be None #18386

Open
@nickdrozd

Description

@nickdrozd

Say I have function that maybe returns None and I check the result:

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

if f() is None: ...

I rewrite the function so that it never returns None:

def f() -> int: ...

if f() is None: ...

Now the is None check makes no sense -- it will always fail. This is a mistake introduced in refactoring and it should be flagged. (And similar for is not None checks)

In Rust I don't think this kind of error is possible:

fn f() -> Option<u8>;

if f().is_none() {}  // okay

fn g() -> u8;

if g().is_none() {} // no good

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