wemake-python-styleguide icon indicating copy to clipboard operation
wemake-python-styleguide copied to clipboard

Detect the same `if` conditions

Open sobolevn opened this issue 4 years ago • 4 comments

Today I saw this code:

if isinstance(x, int):
    ...
elif isinstance(x, str):
    ...
elif isinstance(x, int): ...

The thing is that isinstance(x, int) is duplicated. And the second branch won't ever be called.

We need to detecct this kind of problems and report them.

sobolevn avatar Nov 01 '21 16:11 sobolevn

Isn't this a case for mypy? It should warn that the 3rd statement is unreachable (if you've enabled that option).

Dreamsorcerer avatar Nov 19 '21 00:11 Dreamsorcerer

Yes, mypy can catch some cases. But, not this one for example:

def some(x: int) -> None:
    if x == 1:
        ...
    elif x == 2:
        ...
    elif x == 1:
        ...

sobolevn avatar Nov 19 '21 08:11 sobolevn

Looking at the title, I thought about something like this:

if a:
 ...
if a:
 ...

Both blocks follow next to each other and have the same condition and so can be merged.

orsinium avatar Nov 30 '21 15:11 orsinium

This is a different issue, I guess 🙂 We can detect similar conditions that go as the next statement.

sobolevn avatar Nov 30 '21 15:11 sobolevn