Use AssertIsNotNone when checking against None

assertIsNone and assertIsNotNone provide more helpful failure messages than assertTrue or assertFalse.

When performing checks against None it's better to use the correct tool for the job: assertIsNone and assertIsNotNone are provided explicitly for this task.

The difference is assertion methods such as assertTrue will report the message:

AssertionError: False is not True

While assertIsNone will provided a much more helpful message:

AssertionError: 'foo' is not None

Good quality failure messages allows the failure mode to be understood rapidly and therefore the problem to be fixed more quickly.

If our GitHub code review bot spots this issue in your pull request it gives this advice:

code-review-doctorbotsuggested changes just now
tests.py
1
+
class TestFeature(unittest.TestCase):
2
+
    def test_feature(self):
3
+
        self.assertTrue(value is not None)
Suggested changes
-
        self.assertTrue(value is not None)
+
        self.assertIsNotNone(value)
Commit suggestion

assertIsNone and assertIsNotNone provide more helpful failure messages than assertTrue or assertFalse.

Read more
Update tests.py
We're your Python code review copilot. Get code improvements right in your pull request with our GitHub code review bot.

Configuring this check

Code Review Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code use-assert-is-not-none in your pyproject.toml file.

Read more about configuring Code Review Doctor.