This rule raises an issue when NotImplemented is used in a boolean context, such as in if statements, logical operations, or explicit boolean conversion.

Why is this an issue?

Starting with Python 3.14, using NotImplemented in boolean contexts raises a TypeError. This change was introduced to prevent confusion and bugs that could arise from the implicit boolean evaluation of NotImplemented.

Previously, NotImplemented would evaluate to True in boolean contexts, which could lead to unexpected behavior in code that was meant to check whether an operation was actually implemented. The NotImplemented singleton is specifically designed to be returned by binary special methods (like __eq__, __lt__, etc.) to indicate that the operation is not supported for the given operand types, and should be compared explicitly using identity checks.

This breaking change was preceded by a DeprecationWarning since Python 3.9, giving developers time to update their code. The change ensures that NotImplemented is used correctly and prevents subtle bugs in comparison operations and other contexts where boolean evaluation might occur unintentionally.

What is the potential impact?

Code using NotImplemented in boolean contexts will raise a TypeError at runtime in Python 3.14 and later versions. This can cause application crashes and unexpected behavior. The impact is particularly significant for libraries implementing custom comparison operators or arithmetic operations that return NotImplemented and then check its value incorrectly.

How to fix?

Replace boolean evaluation of NotImplemented with explicit identity checks using is or is not. This clearly expresses the intent to check whether the operation was implemented.

Non-compliant code example

def __eq__(self, other):
    result = NotImplemented
    if result:  # Noncompliant
        return True
    return False

Compliant code example

def __eq__(self, other):
    result = NotImplemented
    if result is not NotImplemented:
        return True
    return False

Documentation

Standards