A function returns a value on some paths and nothing on others

tx0/python:inconsistent-returnstrictwarningsmell

What it finds

A function that returns a value on some paths and ends with a bare return, or by falling off its end, on others: callers get None there, which the code does not say it means. Writing return None where it is meant makes that a decision.

The end of a function is only taken as reachable when its last statement can complete: an if without else, a loop that is not while True, a call, an assignment, and the like. Functions whose last statement is a raise, a return, a try, a with or a match are left alone, and so are generators.

From the rule's source, inconsistent_return.rs.

Example from the rule's tests
def find(items, key):tx0 reports: find returns a value on some paths and None by falling off its end on others    for item in items:        if item == key:            return item  def first(items):tx0 reports: first returns a value on some paths and None by a bare return on others    if not items:        return    return items[0]  def checked(x):    if x:        return 1    raise ValueError(x)  def explicit(x):    if x:        return 1    return None  def branches(x):    if x:        return 1    else:        return 2  def exits(x):    if x:        return 1    sys.exit(1)
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:inconsistent-return" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:inconsistent-return] reason="..."

tx0 explain tx0/python:inconsistent-return shows the same in the terminal. Found a false positive? Report it.