CompareSingletonPrimitivesByIsRule

Enforces the use of is and is not in comparisons to singleton primitives (None, True, False) rather than == and !=. The == operator checks equality, when in this scenario, we want to check identity. See Flake8 rules E711 (https://www.flake8rules.com/rules/E711.html) and E712 (https://www.flake8rules.com/rules/E712.html).

Message

Comparisons to singleton primitives should not be done with == or !=, as they check equality rather than identiy. Use is or is not instead.

Has Autofix: Yes

VALID Code Examples

# 1:

if x: pass

# 2:

if not x: pass

# 3:

x is True

# 4:

x is False

# 5:

x is None

# 6:

x is not None

# 7:

x is True is not y

# 8:

y is None is not x

# 9:

None is y

# 10:

True is x

# 11:

False is x

# 12:

x == 2

# 13:

2 != x

INVALID Code Examples

# 1:

x != True

Autofix:

---
+++
@@ -1 +1 @@
-x != True
+x is not True

# 2:

x != False

Autofix:

---
+++
@@ -1 +1 @@
-x != False
+x is not False

# 3:

x == False

Autofix:

---
+++
@@ -1 +1 @@
-x == False
+x is False

# 4:

x == None

Autofix:

---
+++
@@ -1 +1 @@
-x == None
+x is None

# 5:

x != None

Autofix:

---
+++
@@ -1 +1 @@
-x != None
+x is not None

# 6:

False == x

Autofix:

---
+++
@@ -1 +1 @@
-False == x
+False is x

# 7:

x is True == y

Autofix:

---
+++
@@ -1 +1 @@
-x is True == y
+x is True is y