UseAssertIsNotNoneRule
Discourages use of assertTrue(x is not None)
and assertFalse(x is not None)
as it is deprecated (https://docs.python.org/3.8/library/unittest.html#deprecated-aliases).
Use assertIsNotNone(x)
and assertIsNone(x)
) instead.
Message
“assertTrue” and “assertFalse” are deprecated. Use “assertIsNotNone” and “assertIsNone” instead. See https://docs.python.org/3.8/library/unittest.html#deprecated-aliases
Has Autofix: Yes
VALID Code Examples
# 1:
self.assertIsNotNone(x)
# 2:
self.assertIsNone(x)
# 3:
self.assertIsNone(None)
# 4:
self.assertIsNotNone(f(x))
# 5:
self.assertIsNone(f(x))
# 6:
self.assertIsNone(object.key)
# 7:
self.assertIsNotNone(object.key)
INVALID Code Examples
# 1:
self.assertTrue(a is not None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertTrue(a is not None)
+self.assertIsNotNone(a)
# 2:
self.assertTrue(not x is None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertTrue(not x is None)
+self.assertIsNotNone(x)
# 3:
self.assertTrue(f() is not None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertTrue(f() is not None)
+self.assertIsNotNone(f())
# 4:
self.assertTrue(not x is not None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertTrue(not x is not None)
+self.assertIsNone(x)
# 5:
self.assertTrue(f(x) is not None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertTrue(f(x) is not None)
+self.assertIsNotNone(f(x))
# 6:
self.assertTrue(x is None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertTrue(x is None)
+self.assertIsNone(x)
# 7:
self.assertFalse(x is not None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertFalse(x is not None)
+self.assertIsNone(x)
# 8:
self.assertFalse(not x is None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertFalse(not x is None)
+self.assertIsNone(x)
# 9:
self.assertFalse(f() is not None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertFalse(f() is not None)
+self.assertIsNone(f())
# 10:
self.assertFalse(not x is not None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertFalse(not x is not None)
+self.assertIsNotNone(x)
# 11:
self.assertFalse(f(x) is not None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertFalse(f(x) is not None)
+self.assertIsNone(f(x))
# 12:
self.assertFalse(x is None)
Autofix:
---
+++
@@ -1 +1 @@
-self.assertFalse(x is None)
+self.assertIsNotNone(x)