what is None?
None is used when there is no value. In Mathematics we use 0 for no quantity. In some languages or domains we use NULL, in forms we use N/A when the options do not apply. In Python we can use None, it is the simplest object.
I used assertIs and assertIsNot in test_assertion_error_w_none in the assertion_error project, where I saw that
I want to use assert methods to compare None with the other Python basic objects to see what it is and what it is not.
preview
I have these tests by the end of the chapter
questions about None
start the project
I name this project
noneI open a terminal
I change directory to the
nonefolder in thepumping_pythonfolderthe terminal shows
cd: no such file or directory: noneI use uv to make a directory for the project and initialize it
the terminal shows
Initialized project `none` at `.../pumping_python/none`I change directory to
noneI make a directory for the tests
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pyI make an empty file for the tests in the
testsfoldertouch tests/test_none.pyNew-Item tests/test_none.pyI open
test_none.pyfrom thetestsfolderI add the first failing test to
test_none.py1import unittest 2 3 4class TestNone(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertEqual(False, True)I go back to the terminal
I use echo to add pytest to
requirements.txtecho "pytest" > requirements.txtI add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
I use Out-File to add pytest to
requirements.txt"pytest" | Out-File requirements.txt -Encoding UTF8I add pytest-watcher to the requirements file
"pytest-watcher" >> requirements.txt
I use uv to install pytest-watcher with the requirements file
the terminal shows that it installed pytest-watcher and its dependencies.
I add a git commit message
I use pytest-watcher to run the tests automatically
the terminal is my friend, and shows AssertionError
======================== FAILURES ======================== _________________ TestNone.test_failure __________________ self = <tests.test_none.TestNone testMethod=test_failure> def test_failure(self): > self.assertEqual(False, True) E AssertionError: False != True tests/test_none.py:7: AssertionError ================ short test summary info ================= FAILED tests/test_none.py::TestNone::test_failure - AssertionError: False != True =================== 1 failed in X.YZs ====================because True is NOT equal to False.
if the terminal does not show the same error, then check if
your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyyou ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
and try
uv run pytest-watcher . --now --patterns "**/*.py"againI add AssertionError to the list of Exceptions seen, in
test_none.pyI change True to False in the assertion
the test passes.
test_what_is_none
RED: make it fail
I remove test_failure
I add test_what_is_none with a call to the assertIsNot method
the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: None
GREEN: make it pass
I change assertIsNot to assertIs
6 def test_what_is_none(self):
7 # self.assertIsNot(None, None)
8 self.assertIs(None, None)
9
10
11# Exceptions seen
the test passes.
another way to test if something is NOT None
I can also use another assert method from the unittest.TestCase class to test if something is NOT the same object as None - the assertIsNotNone method.
RED: make it fail
I add an assertion with the assertIsNotNone method
6 def test_what_is_none(self):
7 # self.assertIsNot(None, None)
8 self.assertIs(None, None)
9 self.assertIsNotNone(None)
10
11
12# Exceptions seen
the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly None
because assertIsNotNone raises AssertionError if the object given in parentheses is None.
assertIsNot(something, None)andassertIsNotNone(something)are the same.
another way to test if something is None
I can use the assertIsNone method from the unittest.TestCase class to test if something is the same object as None.
GREEN: make it pass
I change assertIsNotNone to the assertIsNone method
6 def test_what_is_none(self):
7 # self.assertIsNot(None, None)
8 self.assertIs(None, None)
9 # self.assertIsNotNone(None)
10 self.assertIsNone(None)
11
12
13# Exceptions seen
the test passes because None is None and assertIsNone raises AssertionError if the object given in parentheses is NOT None.
assertIs(something, None)andassertIsNone(something)are the same.
REFACTOR: make it better
I remove the commented lines
I add comments
I open a new terminal then change directories to
none
I know two new assert methods
test_is_none_a_boolean
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is a boolean
the terminal is my friend, and shows AssertionError
AssertionError: False is not None
GREEN: make it pass
I change assertIsNone to the assertIsNotNone method
10 def test_is_none_a_boolean(self):
11 # self.assertIsNone(False)
12 self.assertIsNotNone(False)
13
14
15# NOTES
the test passes.
REFACTOR: make it better
I add a comment
another comment from the assertion_error project.
I add an assertion for the other boolean
the terminal is my friend, and shows AssertionError
AssertionError: True is not NoneI change assertIsNone to assertIsNotNone
the test passes.
I add a comment
I add a call to the assertNotIsInstance method to test if False is an instance of the bool class
the terminal is my friend, and shows AssertionError
AssertionError: False is an instance of <class 'bool'>I change assertNotIsInstance to the assertIsInstance method
the test passes.
I add a failing line for the other boolean with assertNotIsInstance
the terminal is my friend, and shows AssertionError
AssertionError: True is an instance of <class 'bool'>I make the statement True with assertIsInstance
the test passes.
I add assertIsInstance to test if None is an instance (a copy) of the bool class
the terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'bool'>because None is NOT a boolean.
I make the line True with assertNotIsInstance
the test passes.
I remove the commented lines from test_is_none_a_boolean
I change the last 2 comments I added (
True is NOT NoneandFalse is NOT None), since this is about None
The names of the assert methods can be confusing,
there is
assertIsNotNonewhereNotcomes afterIsthere is
assertNotIsInstancewhereIscomes afterNot
Would assertIsNotInstance be better than assertNotIsInstance, since assertNotIsNone does not sound better than assertIsNotNone?
Not in this case, because the names of the assert methods match the assert statements they replace
assertIsNotNone(x)forassert x is not NoneassertNotIsInstance(x, y)forassert not isinstance(x, y)
test_is_none_an_integer
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is an integer (a whole number without decimals)
the terminal is my friend, and shows AssertionError
AssertionError: -1 is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
17 def test_is_none_an_integer(self):
18 # self.assertIsNone(-1)
19 self.assertIsNotNone(-1)
20
21
22# NOTES
the test passes.
REFACTOR: make it better
I add a new failing line with assertIsNone
the terminal is my friend, and shows AssertionError
AssertionError: 0 is not NoneI make the statement True with assertIsNotNone
the test passes.
I add an assertion for
1the terminal is my friend, and shows AssertionError
AssertionError: 1 is not NoneI change the line to make it True
the test passes.
I add a new failing line with assertNotIsInstance
I make the line True with assertIsInstance
the test passes.
I add a failing assertion with assertNotIsInstance
the terminal is my friend, and shows AssertionError
AssertionError: 0 is an instance of <class 'int'>because
0is an integer.I change the statement to make it True
the test passes.
I add a failing assertion with assertNotIsInstance
the terminal is my friend, and shows AssertionError
AssertionError: 1 is an instance of <class 'int'>I make the failing line True with assertIsInstance
the test passes.
I add one more failing line to test if None is an integer with assertIsInstance
the terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'int'>because None is NOT an integer.
I make the line True with assertNotIsInstance
the test passes.
I remove the commented lines from test_is_none_an_integer
I add a new comment
test_is_none_a_float
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is a float (binary floating point decimal number)
24 self.assertNotIsInstance(None, int)
25
26 def test_is_none_a_float(self):
27 self.assertIsNone(-0.1)
28
29
30# NOTES
the terminal is my friend, and shows AssertionError
AssertionError: -0.1 is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
26 def test_is_none_a_float(self):
27 # self.assertIsNone(-0.1)
28 self.assertIsNotNone(-0.1)
29
30
31# NOTES
the test passes.
REFACTOR: make it better
I add a call to assertIsNone for
0.0the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not NoneI make the statement True with assertIsNotNone
the test passes.
I add a call to assertIsNone for
0.1the terminal is my friend, and shows AssertionError
AssertionError: 0.1 is not NoneI make the statement True with assertIsNotNone
the test passes. Time for instance tests.
I add a failing line with assertNotIsInstance for
-0.1float is the object for binary floating point numbers. the terminal is my friend, and shows AssertionError
AssertionError: -0.1 is an instance of <class 'float'>because
-0.1is a float.I use
-0.1for all the binary floating point numbers that are smaller than0.0.
I make the statement True with assertIsInstance
the test passes.
I add the next instance test with assertNotIsInstance for
0.0the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is an instance of <class 'float'>because
0.0is a float.I make the statement True with assertIsInstance
the test passes.
I add a failing line with assertNotIsInstance for
0.1the terminal is my friend, and shows AssertionError
AssertionError: 0.1 is an instance of <class 'float'>because
0.1is a float.I use
0.1for all the binary floating point numbers that are bigger than0.0.
I make the statement True with assertIsInstance
the test passes.
I add one more failing line with the assertIsInstance method
the terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'float'>because None is NOT a float.
I make the statement True with the assertNotIsInstance method
the test passes.
I remove the commented lines from test_is_none_a_float
I add a new comment
test_is_none_a_string
RED: make it fail
GREEN: make it pass
I make the statement True with assertIsNotNone
35 def test_is_none_a_string(self):
36 # self.assertIsNone('')
37 self.assertIsNotNone('')
38
39
40# NOTES
the test passes.
REFACTOR: make it better
I add a call to assertIsNone for a string that is not empty
the terminal is my friend, and shows AssertionError
AssertionError: 'characters' is not NoneI change the assert method to make the statement True
the test passes.
I add a failing line with assertNotIsInstance for the empty string (
"")I change the assert method
the test passes.
I add an assertion for
'characters'the terminal is my friend, and shows AssertionError
AssertionError: 'characters' is an instance of <class 'str'>I change the assert method
the test passes.
I add a failing assertion with assertIsInstance
the terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'str'>because None is NOT a string.
I change the assert method
I remove the commented lines from test_is_none_a_string
I add a comment
test_is_none_a_tuple
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is a tuple (anything in parentheses (
( )) separated by commas)the terminal is my friend, and shows AssertionError
AssertionError: () is not None
GREEN: make it pass
I make the statement True
42 def test_is_none_a_tuple(self):
43 # self.assertIsNone(())
44 self.assertIsNotNone(())
45
46
47# NOTES
the test passes.
REFACTOR: make it better
I add a failing assertion for
(0, 1, 2, 'n')the terminal is my friend, and shows AssertionError
AssertionError: (0, 1, 2, 'n') is not NoneI make the statement True
the test passes.
I add a failing line with assertNotIsInstance for the empty tuple
the terminal is my friend, and shows AssertionError
AssertionError: () is an instance of <class 'tuple'>I make the statement True
the test passes.
I add a failing assertion for
(0, 1, 2, 'n')the terminal is my friend, and shows AssertionError
AssertionError: (0, 1, 2, 'n') is an instance of <class 'tuple'>because in Python anything in parentheses (
( )) separated by commas is a tuple.I change the assert method to make the statement True
the test passes.
I add one more instance test
the terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'tuple'>because None is NOT a tuple.
I change the statement to make it True
the test passes.
I remove the commented lines from test_is_none_a_tuple
I add a comment
it looks like None is None and not anything else.
test_is_none_a_list
RED: make it fail
I go back to the terminal where the tests are running
I add a new test to see if None is a list (anything in square brackets (
[ ]))the terminal is my friend, and shows AssertionError
AssertionError: [] is not None
GREEN: make it pass
I change the assertion to make the statement True
49 def test_is_none_a_list(self):
50 # self.assertIsNone([])
51 self.assertIsNotNone([])
52
53
54# NOTES
the test passes.
REFACTOR: make it better
I add a failing assertion for
[0, 1, 2, 'n']the terminal is my friend, and shows AssertionError
AssertionError: [0, 1, 2, 'n'] is not NoneI make the statement True
the test passes.
I add a failing instance test for the empty list
the terminal is my friend, and shows AssertionError
AssertionError: [] is an instance of <class 'list'>because in Python anything in square brackets (
[ ]) is a list.I make the statement True
the test passes.
I add a failing assertion with assertNotIsInstance
the terminal is my friend, and shows AssertionError
AssertionError: [0, 1, 2, 'n'] is an instance of <class 'list'>because in Python anything in square brackets (
[ ]) is a list.I change the assert method
the test passes.
I add one more failing line with the assertIsInstance method
the terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'list'>because None is NOT a list.
I make the statement True
the test passes.
I remove the commented lines from test_is_none_a_list
I add a new comment
test_is_none_a_set
RED: make it fail
I go back to the terminal where the tests are running
I want to test if None is a set (anything in curly braces
{ }, not key-value pairs)the terminal is my friend, and shows AssertionError
AssertionError: set() is not None
GREEN: make it pass
I make the statement True
56 def test_is_none_a_set(self):
57 # self.assertIsNone(set())
58 self.assertIsNotNone(set())
59
60
61# NOTES
the test passes.
REFACTOR: make it better
I add an assertion for
{0, 1, 2, 'n'}the terminal is my friend, and shows AssertionError
AssertionError: {0, 1, 2, 'n'} is not NoneI make the statement True
the test passes.
I add an instance test
the terminal is my friend, and shows AssertionError
AssertionError: {0, 1, 2, 'n'} is an instance of <class 'set'>I make the statement True
the test passes.
I add another instance test
the terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'set'>because None is NOT a set.
I make the statement True
the test passes.
I remove the commented lines from test_is_none_a_set
I add a comment
test_is_none_a_dictionary
Is None a dictionary?
RED: make it fail
I go back to the terminal where the tests are running
One last test, this one for if None is a dictionary (key-value pairs in curly braces ‘{ }’ separated by commas)
the terminal is my friend, and shows AssertionError
AssertionError: {} is not Nonewait a minute! Python uses
{}for sets. It also uses them for dictionaries with a difference.
GREEN: make it pass
I make the statement True
62 def test_is_none_a_dictionary(self):
63 # self.assertIsNone(dict())
64 self.assertIsNotNone(dict())
65
66
67# NOTES
the test passes.
REFACTOR: make it better
I add an assertion for
{'key': 'value'}the terminal is my friend, and shows AssertionError
AssertionError: {'key': 'value'} is not NoneI make the statement True
the test passes.
I add a failing instance test for
{}dict is the object for dictionaries
the terminal is my friend, and shows AssertionError
AssertionError: {} is an instance of <class 'dict'>{}is the empty dictionaryset()is the empty set
I change the assert method
the test passes.
I add an instance test for
{'key': 'value'}the terminal is my friend, and shows AssertionError
AssertionError: {'key': 'value'} is an instance of <class 'dict'>I make the statement True
the test passes.
I add the last failing instance test with assertIsInstance
the terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'dict'>because None is NOT a dictionary.
I make the statement True with assertNotIsInstance
the test passes.
I remove the commented lines from test_is_none_a_dictionary
I add the last comment
sets vs dictionaries
{'key': 'value'} is a dictionary with : separating the key on the left from the value on the right.
I can add more key-value pairs separating them with commas, for example
{
'key': 'value',
'another_key': 'another value',
'one_more_key': 'one more value',
'magic_key': 'magic value',
'keyN': [0, 1, 2, 'n'],
}
sets do NOT have key-value pairs.
{
0, 1, 2, 'n',
'value',
'another value',
'one more value',
'magic value',
}
close the project
I close
test_none.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
nonethe terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I used assertIsInstance and assertNotIsInstance to show that None is not an instance of the other basic types.
I used two new assert methods for None:
assertIsNotNone which raises AssertionError if the thing in parentheses is None.
assertIsNone which raises AssertionError if the thing in parentheses is NOT None.
How many questions can you answer after going through this chapter?
code from the chapter
what is next?
Would you like to use assertIsNotNone and assertIsNone to test these projects?
Would you like to use the assertIsNotNone and assertIsNone methods with the assertion_error project?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.