sourcery icon indicating copy to clipboard operation
sourcery copied to clipboard

Unecessary None checking

Open marcoaaguiar opened this issue 4 years ago • 3 comments

Issue description or question

I noticed a unnecessary None checking that could be simplified:

from typing import List, Optional


def func(items: Optional[List]):
    if items is not None and isinstance(items, list):
        return items.pop()
    return None

Here checking for None is unnecessary, since NoneType != list.

Sourcery Version

0.8.2

Code editor or IDE name and version

Discovered on NeoVim 0.5.0

OS name and version

Ubuntu 20.04 via WSL

marcoaaguiar avatar Nov 11 '20 02:11 marcoaaguiar

I guess in this case I would probably change it to:

if items:
    return items.pop()
return None

but I don't think I could add that to Sourcery since the semantics aren't quite the same.

Hellebore avatar Nov 11 '20 08:11 Hellebore

Maybe my example was not the best :)

I was focusing in the fact that the items is not of type list (I'm using list in the example but it could be any other type/class) when it is None, and vice-versa (tautology). It is more evident if the conditional is switched around:

def func(items: Optional[List]):
    if isinstance(items, list) and items is not None
        # some code
    #other code

marcoaaguiar avatar Nov 11 '20 11:11 marcoaaguiar

Yes I see what you mean - have added it to our pipeline.

Hellebore avatar Nov 11 '20 12:11 Hellebore