This rule raises an issue when == or != compares a value with a sentinel created by the built-in sentinel() (Python 3.15, PEP 661) or by typing_extensions.Sentinel.

Why is this an issue?

A sentinel is a unique object used to represent a special state, such as a missing or unset value. Its meaning comes from its identity: the object itself, not a value it contains.

Using an equality or inequality comparison asks the language to compare values. This is less clear than an identity comparison and can invoke custom equality behavior from the other operand.

Use a reference-identity comparison when checking for a sentinel, and a negated identity comparison when checking that a value is not the sentinel. These comparisons clearly show that the code checks for the unique marker object.

What is the potential impact?

Value-equality comparisons can make sentinel checks less clear and may behave unexpectedly when the other operand defines custom equality behavior. Comparisons that test whether both references designate the same object express the intended test directly and reliably.

How to fix it

Replace == with is and != with is not when comparing with a value created by the built-in sentinel() or by typing_extensions.Sentinel.

Code examples

Noncompliant code example

MISSING = sentinel("MISSING")
UNSET = sentinel("UNSET")

if value == MISSING:  # Noncompliant
    handle_missing()

if value != UNSET:  # Noncompliant
    process(value)

Compliant solution

MISSING = sentinel("MISSING")
UNSET = sentinel("UNSET")

if value is MISSING:
    handle_missing()

if value is not UNSET:
    process(value)

Resources

Documentation