wemake-python-styleguide
wemake-python-styleguide copied to clipboard
Detect the same `if` conditions
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.
Isn't this a case for mypy? It should warn that the 3rd statement is unreachable (if you've enabled that option).
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:
...
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.
This is a different issue, I guess 🙂 We can detect similar conditions that go as the next statement.