redline
LowType safetynone-compare

Compares to None with ==

Compares to None with ==. == None should be is None / is not None.

This is how the comment reads inline, under the flagged line.

Why it matters

== can be overridden by a class and gives surprising answers. is None is exact and idiomatic.

What to do

Use is None or is not None.

Examples

Flagged
if user == None:
Better
if user is None:

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

if x == None:
    pass

Left alone

if x is None:
    pass

Research

`==`, `var`, `== None`, and `is` with a literal. Coercion and leaky scope make comparisons true or false for the wrong reason.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "none-compare"
    ],
    "severity": {
      "none-compare": "medium"
    }
  }
}

Read the source or report a false positive.