An operator method raises NotImplementedError where returning NotImplemented lets the other operand try

tx0/python:operator-raises-not-implementedrecommendedwarningbug

What it finds

def __eq__(self, other): raise NotImplementedError: Python asks the other operand when a comparison or arithmetic method returns NotImplemented, and only raises TypeError if neither side can handle the operation. Raising NotImplementedError instead skips the other operand, and makes == with an unrelated object fail where it should be False. Abstract methods, and other methods with decorators, are left alone.

From the rule's source, operator_raises_not_implemented.rs.

Example from the rule's tests
class Money:    def __eq__(self, other):        raise NotImplementedErrortx0 reports: __eq__ raises NotImplementedError; returning NotImplemented lets the other operand try     def __add__(self, other):        if not isinstance(other, Money):            raise NotImplementedError("money only")tx0 reports: __add__ raises NotImplementedError        return self     def convert(self):        raise NotImplementedError     def __lt__(self, other):        return NotImplemented
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:operator-raises-not-implemented" = "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:operator-raises-not-implemented] reason="..."

tx0 explain tx0/python:operator-raises-not-implemented shows the same in the terminal. Found a false positive? Report it.