Python

Python

By DeepSource

Branches of the if statement have similar implementation PTC-W0051

Anti-pattern
Major

For the highlighted if statements, all the elif / else branches have the same body as if. It is recommended to refactor this snippet.

If the if-chain is performing the same action in every case, it shouldn't be used there at all.

Not preferred:

if b == 0:
    do_something()
elif b == 1:
    do_something()
else:
    do_something()

b = 4 if a > 12 else 4

Preferred:

# If this is was a copy-paste error, review and update the snippet
if b == 0:
    do_something()
elif b == 1:
    do_something_else()
else:
    do_other_thing()

b = -4 if a > 12 else 4

# OR
# Refactor the code to not use the if-chain at all.
do_something()

b = 4