too-many-return-statements / R0911ΒΆ
Message emitted:
Too many return statements (%s/%s)
Description:
Used when a function or method has too many return statement, making it hard to follow.
Problematic code:
def to_string(x): # [too-many-return-statements]
# max of 6 by default, can be configured
if x == 1:
return "This is one."
if x == 2:
return "This is two."
if x == 3:
return "This is three."
if x == 4:
return "This is four."
if x == 5:
return "This is five."
if x == 6:
return "This is six."
if x == 7:
return "This is seven."
Correct code:
NUMBERS_TO_STRINGS = {
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
}
def to_string(x):
return f"This is {NUMBERS_TO_STRINGS.get(x)}."
Created by the design checker.