Pattern: Inconsistent return statement
Issue: -
Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None
, and an explicit return statement should be present at the end of the function (if reachable).
Example of incorrect code:
def mix_implicit_explicit_returns(arg):
if arg < 10:
return True
elif arg < 20:
return
Example of correct code:
def mix_implicit_explicit_returns(arg):
if arg < 10:
return True
elif arg < 20:
return None