__init__ hands back a value, and creating the object raises TypeError

tx0/python:return-in-initdefaultwarningbug

What it finds

def __init__(self): ... return True: Python requires __init__ to return None, and creating an instance fails with a TypeError when it returns anything else.

Only values that are certainly not None are reported: literals other than None, strings, displays and comprehensions, and lambdas. A value that may be None, such as the result of super().__init__(), is left alone.

From the rule's source, return_in_init.rs.

Example from the rule's tests
class Point:    def __init__(self, x, y):        if x is None:            return Falsetx0 reports: __init__ must return None        if y is None:            return x, ytx0 reports: __init__ must return None        if x == y:            return "same"tx0 reports: __init__ must return None        if x < 0:            return        if y < 0:            return None        self.x = x         def helper():            return 1         self.f = lambda: 2        return super().__init__()  def __init__(self):    return 1  class Other:    def setup(self):        return 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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/python:return-in-init" = "off"

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:return-in-init] reason="..."

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