separate and equal TypeError
I want to move the functions from test_type_error.py in the tests folder to type_error.py in the src folder so that I can keep the tests and solutions separate.
preview
I have these tests by the end of the chapter
1import src.type_error
2
3
4def test_type_error_w_positional_arguments():
5 src.type_error.function_00('a')
6 src.type_error.function_01('a', 'b')
7 src.type_error.function_02('a', 'b', 'c')
8 src.type_error.function_03('a', 'b', 'c', 'd')
9 src.type_error.function_04('a')
10 src.type_error.function_05('a', 'b')
11 src.type_error.function_06('a', 'b', 'c')
12 src.type_error.function_07('a', 'b', 'c', 'd')
13 src.type_error.function_08('last', 'one')
14
15
16def test_type_error_w_keyword_arguments():
17 src.type_error.function_00(the_input=0)
18 src.type_error.function_01(
19 first='first',
20 second={'key': 'value'},
21 )
22 src.type_error.function_02(
23 third=(0, 1, 2, 'n'),
24 second=[0, 1, 2, 'n'],
25 first={0, 1, 2, 'n'},
26 )
27 src.type_error.function_03(
28 first=None,
29 second=False,
30 third=True,
31 fourth=4,
32 )
33 src.type_error.function_04(argument='value')
34 src.type_error.function_05(
35 argument_0='value_1',
36 argument_1=(0, 1, 2, 'n'),
37 )
38 src.type_error.function_06(
39 argument_0='value_1',
40 argument_1=(0, 1, 2, 'n'),
41 argument_2=[0, 1, 2, 'n'],
42 )
43 src.type_error.function_07(
44 argument_0=(0, 1, 2, 'n'),
45 argument_1=[0, 1, 2, 'n'],
46 argument_2={0, 1, 2, 'n'},
47 argument_n={'key': 'value'},
48 )
49 src.type_error.function_08(
50 argument='positional',
51 name='keyword',
52 )
53
54
55def test_type_error_w_args_and_kwargs():
56 src.type_error.function_00('argument')
57 src.type_error.function_01(1, 0)
58 src.type_error.function_02(
59 third=True, second=False,
60 first=None,
61 )
62 src.type_error.function_03(
63 second=[0, 1, 2, 'n'],
64 first=(0, 1, 2, 'n'),
65 third={0, 1, 2, 'n'},
66 fourth={'key': 'value'},
67 )
68 src.type_error.function_04('value')
69 src.type_error.function_05(
70 (0, 1, 2, 'n'),
71 [0, 1, 2, 'n'],
72 )
73 src.type_error.function_06(
74 (0, 1, 2, 'n'),
75 [0, 1, 2, 'n'],
76 argument_2={0, 1, 2, 'n'},
77 )
78 src.type_error.function_07(
79 argument_n={'key': 'value'},
80 argument_2={0, 1, 2, 'n'},
81 argument_0=(0, 1, 2, 'n'),
82 argument_1=[0, 1, 2, 'n'],
83 )
84 src.type_error.function_08(
85 'positional',
86 argument='keyword',
87 )
88
89
90# Exceptions seen
91# AssertionError
92# NameError
93# TypeError
94# ModuleNotFoundError
95# AttributeError
open the project
I open a terminal
I change directory to the type_error folder in the
pumping_pythonfoldercd type_errorthe terminal shows I am in the type_error folder
.../pumping_python/type_errorI open
test_type_error.pyI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal shows
test_type_error.py ... [100%] =================== 3 passed in X.YZs ====================
mass migration
RED: make it fail
I change the call to function_00 in test_type_error_w_positional_arguments to be a call to function_00 of type_error.py in the src folder
43def test_type_error_w_positional_arguments():
44 # function_00('a')
45 src.type_error.function_00('a')
46 function_01('a', 'b')
47 function_02('a', 'b', 'c')
48 function_03('a', 'b', 'c', 'd')
49 function_04('a')
50 function_05('a', 'b')
51 function_06('a', 'b', 'c')
52 function_07('a', 'b', 'c', 'd')
53 function_08('last', 'one')
54
55
56def test_type_error_w_keyword_arguments():
the terminal is my friend, and shows NameError
NameError: name 'src' is not defined
because src is not defined in test_type_error.py.
GREEN: make it pass
I add an import statement at the top of
test_type_error.py1import src.type_error 2 3 4def function_00(the_input): 5 return Nonethe terminal is my friend, and shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src'because there is nothing named
srcin this project.I add ModuleNotFoundError to the list of Exceptions seen
133# Exceptions seen 134# AssertionError 135# NameError 136# TypeError 137# ModuleNotFoundErrorI open another terminal then make sure I am in the type_error folder
cd type_errorI use mkdir to make a folder named
srcmkdir srcI go back to the terminal where the tests are running.
I use ctrl/command+s (Windows & Linux/MacOS) on the keyboard in
test_type_error.pyto run the tests again and the terminal shows ModuleNotFoundErrorModuleNotFoundError: No module named 'src.type_error'because there is nothing in the
srcfolder namedtype_error.I go to the second terminal I opened, then use touch to make
type_error.pyin thesrcfoldertouch src/type_error.pyI add the new file to git for tracking
git add src/type_error.pythe terminal goes back to the command line.
I go back to the terminal where the tests are running and it shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_00'because there is nothing in
type_error.pyin thesrcfolder with the namefunction_00.I add AttributeError to the list of Exceptions seen
133# Exceptions seen 134# AssertionError 135# NameError 136# TypeError 137# ModuleNotFoundError 138# AttributeErrorI open
type_error/__init__.pyfrom thesrcfolderI add a definition for
function_00totype_error.py1def function_00(): 2 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_00() takes 0 positional arguments but 1 was givenI add a name to the parentheses
1# def function_00(): 2def function_00(one): 3 return Nonethe test passes because
Python brings in an object for the
type_error.pyfile from thesrcfolder so I can use it intest_type_error.pyassrc.type_errorwhenimport src.type_errorruns.Python calls the
function_00function from the object it imported for thetype_error.pyfile from thesrcfolder (src.type_error) whensrc.type_error.function_00is called.
I think of
src.type_error.function_00like an addresssrc.type_error.function_00 src └── type_error.py └── def function_00(one): └── return Nonefunction_00is something intype_error, in this case it is a function intype_error.type_erroris something insrc, in this case it istype_error.py(a module) in thesrcfolder.srcis something Python can import (a module, Python package or folder).function_00is now an attribute/property oftype_error.pyin thesrcfolder. I can use it from outside the file withsrc.type_error.function_00.
REFACTOR: make it better
I change the call to
function_01in test_type_error_w_positional_arguments to be a call tofunction_01oftype_error.pyin thesrcfolder, intest_type_error.py46def test_type_error_w_positional_arguments(): 47 # function_00('a') 48 src.type_error.function_00('a') 49 # function_01('a', 'b') 50 src.type_error.function_01('a', 'b') 51 function_02('a', 'b', 'c')the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_01'. Did you mean: 'function_00'?I add a function definition for
function_01totype_error.py1# def function_00(): 2def function_00(one): 3 return None 4 5 6def function_01(): 7 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_01() takes 0 positional arguments but 2 were givenI add two names in the parentheses
1# def function_00(): 2def function_00(one): 3 return None 4 5 6# def function_01(): 7def function_01(one, two): 8 return Nonethe test passes because Python follows this path when
src.type_error.function_01is calledsrc.type_error.function_01 src └── type_error.py └── def function_01(one, two): └── return NoneI change the call to
function_02in test_type_error_w_positional_arguments to be a call tofunction_02oftype_error.pyin thesrcfolder, intest_type_error.py50 src.type_error.function_01('a', 'b') 51 # function_02('a', 'b', 'c') 52 src.type_error.function_02('a', 'b', 'c') 53 function_03('a', 'b', 'c', 'd')the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_02'. Did you mean: 'function_00'?I add a function definition for
function_02totype_error.py6# def function_01(): 7def function_01(one, two): 8 return None 9 10 11def function_02(): 12 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_02() takes 0 positional arguments but 3 were givenI add three names to the parentheses
6# def function_01(): 7def function_01(one, two): 8 return None 9 10 11# def function_02(): 12def function_02(one, two, three): 13 return Nonethe test passes because Python follows this path when
src.type_error.function_02is calledsrc.type_error.function_02 src └── type_error.py └── def function_02(one, two, three): └── return NoneI change the call to
function_03in test_type_error_w_positional_arguments to be a call tofunction_03oftype_error.pyin thesrcfolder, intest_type_error.py52 src.type_error.function_02('a', 'b', 'c') 53 # function_03('a', 'b', 'c', 'd') 54 src.type_error.function_03('a', 'b', 'c', 'd') 55 function_04('a')the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_03'. Did you mean: 'function_00'?I add a function definition for
function_03totype_error.py11# def function_02(): 12def function_02(one, two, three): 13 return None 14 15 16def function_03(): 17 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_03() takes 0 positional arguments but 4 were givenI add four names to the parentheses
11# def function_02(): 12def function_02(one, two, three): 13 return None 14 15 16# def function_03(): 17def function_03(one, two, three, four): 18 return Nonethe test passes because Python follows this path when
src.type_error.function_03is calledsrc.type_error.function_03 src └── type_error.py └── def function_03(one, two, three, four): └── return NoneI change the call to
function_04in test_type_error_w_positional_arguments to be a call tofunction_04oftype_error.pyin thesrcfolder, intest_type_error.py54 src.type_error.function_03('a', 'b', 'c', 'd') 55 # function_04('a') 56 src.type_error.function_04('a') 57 function_05('a', 'b')the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_04'. Did you mean: 'function_00'?I add a function definition for
function_04totype_error.py16# def function_03(): 17def function_03(one, two, three, four): 18 return None 19 20 21def function_04(): 22 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_04() takes 0 positional arguments but 1 was givenI add
oneto the parentheses16# def function_03(): 17def function_03(one, two, three, four): 18 return None 19 20 21# def function_04(): 22def function_04(one): 23 return Nonethe test passes because Python follows this path when
src.type_error.function_04is calledsrc.type_error.function_04 src └── type_error.py └── def function_04(one): └── return NoneI change the call to
function_05in test_type_error_w_positional_arguments to be a call tofunction_05oftype_error.pyin thesrcfolder, intest_type_error.py56 src.type_error.function_04('a') 57 # function_05('a', 'b') 58 src.type_error.function_05('a', 'b') 59 function_06('a', 'b', 'c')the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_05'. Did you mean: 'function_00'?I add a function definition for
function_05totype_error.py21# def function_04(): 22def function_04(one): 23 return None 24 25 26def function_05(): 27 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_05() takes 0 positional arguments but 2 were givenI add two names to the parentheses
21# def function_04(): 22def function_04(one): 23 return None 24 25 26# def function_05(): 27def function_05(one, two): 28 return Nonethe test passes because Python follows this path when
src.type_error.function_05is calledsrc.type_error.function_05 src └── type_error.py └── def function_05(one, two): └── return NoneI change the call to
function_06in test_type_error_w_positional_arguments to be a call tofunction_06oftype_error.pyin thesrcfolder, intest_type_error.py58 src.type_error.function_05('a', 'b') 59 # function_06('a', 'b', 'c') 60 src.type_error.function_06('a', 'b', 'c') 61 function_07('a', 'b', 'c', 'd')the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_06'. Did you mean: 'function_00'?I add a function definition for
function_06totype_error.py26# def function_05(): 27def function_05(one, two): 28 return None 29 30 31def function_06(): 32 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_06() takes 0 positional arguments but 3 were givenI add three names to the parentheses
26# def function_05(): 27def function_05(one, two): 28 return None 29 30 31# def function_06(): 32def function_06(one, two, three): 33 return Nonethe test passes because Python follows this path when
src.type_error.function_06is calledsrc.type_error.function_06 src └── type_error.py └── def function_06(one, two, three): └── return NoneI change the call to
function_07in test_type_error_w_positional_arguments to be a call tofunction_07oftype_error.pyin thesrcfolder, intest_type_error.py60 src.type_error.function_06('a', 'b', 'c') 61 # function_07('a', 'b', 'c', 'd') 62 src.type_error.function_07('a', 'b', 'c', 'd') 63 function_08('last', 'one') 64 65 66def test_type_error_w_keyword_arguments():the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_07'. Did you mean: 'function_00'?I add a function definition for
function_07totype_error.py31# def function_06(): 32def function_06(one, two, three): 33 return None 34 35 36def function_07(): 37 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_07() takes 0 positional arguments but 4 were givenI add four names to the parentheses
31# def function_06(): 32def function_06(one, two, three): 33 return None 34 35 36# def function_07(): 37def function_07(one, two, three, four): 38 return Nonethe test passes because Python follows this path when
src.type_error.function_07is calledsrc.type_error.function_07 src └── type_error.py └── def function_07(one, two, three, four): └── return NoneI change the call to
function_08in test_type_error_w_positional_arguments to be a call tofunction_08oftype_error.pyin thesrcfolder, intest_type_error.py62 src.type_error.function_07('a', 'b', 'c', 'd') 63 # function_08('last', 'one') 64 src.type_error.function_08('last', 'one') 65 66 67def test_type_error_w_keyword_arguments():the terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_08'. Did you mean: 'function_00'?I add a function definition for
function_08totype_error.py36# def function_07(): 37def function_07(one, two, three, four): 38 return None 39 40 41def function_08(): 42 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_08() takes 0 positional arguments but 2 were givenI add two names to the parentheses
36# def function_07(): 37def function_07(one, two, three, four): 38 return None 39 40 41# def function_08(): 42def function_08(one, two): 43 return Nonethe test passes because Python follows this path when
src.type_error.function_08is calledsrc.type_error.function_08 src └── type_error.py └── def function_08(one, two): └── return NoneI remove the commented lines from test_type_error_w_positional_arguments in
test_type_error.py46def test_type_error_w_positional_arguments(): 47 src.type_error.function_00('a') 48 src.type_error.function_01('a', 'b') 49 src.type_error.function_02('a', 'b', 'c') 50 src.type_error.function_03('a', 'b', 'c', 'd') 51 src.type_error.function_04('a') 52 src.type_error.function_05('a', 'b') 53 src.type_error.function_06('a', 'b', 'c') 54 src.type_error.function_07('a', 'b', 'c', 'd') 55 src.type_error.function_08('last', 'one') 56 57 58def test_type_error_w_keyword_arguments():
I change the call to
function_00in test_type_error_w_keyword_arguments to be a call tofunction_00oftype_error.pyin thesrcfolder, intest_type_error.py58def test_type_error_w_keyword_arguments(): 59 # function_00(the_input=0) 60 src.type_error.function_00(the_input=0) 61 function_01( 62 first='first', 63 second={'key': 'value'}, 64 )the terminal is my friend, and shows TypeError
TypeError: function_00() got an unexpected keyword argument 'the_input'because the call uses a name (
the_input) that is not in the parentheses of its definition.I add
the_inputto the parentheses of the definition offunction_00intype_error.py1# def function_00(): 2# def function_00(one): 3def function_00(one, the_input): 4 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_00() missing 1 required positional argument: 'the_input' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_00() missing 1 required positional argument: 'one'my change broke a test that was passing.
I remove
onefrom the parentheses1# def function_00(): 2# def function_00(one): 3# def function_00(one, the_input): 4def function_00(the_input): 5 return Nonethe test passes.
I change the call to
function_01in test_type_error_w_keyword_arguments to be a call tofunction_01oftype_error.pyin thesrcfolder, intest_type_error.py58def test_type_error_w_keyword_arguments(): 59 # function_00(the_input=0) 60 src.type_error.function_00(the_input=0) 61 # function_01( 62 src.type_error.function_01( 63 first='first', 64 second={'key': 'value'}, 65 )the terminal is my friend, and shows TypeError
TypeError: function_01() got an unexpected keyword argument 'first'because the call uses a name (
first) that is not in the parentheses of its definition.I add
firstto the parentheses of the definition offunction_01intype_error.py8# def function_01(): 9# def function_01(one, two): 10def function_01(one, two, first): 11 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_01() missing 1 required positional argument: 'first' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_01() got an unexpected keyword argument 'second'another change that broke a test that was passing.
I add
secondto the parentheses8# def function_01(): 9# def function_01(one, two): 10# def function_01(one, two, first): 11def function_01(one, two, first, second): 12 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_01() missing 2 required positional arguments: 'first' and 'second' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_01() missing 2 required positional arguments: 'one' and 'two'I remove
oneandtwofrom the parentheses8# def function_01(): 9# def function_01(one, two): 10# def function_01(one, two, first): 11# def function_01(one, two, first, second): 12def function_01(first, second): 13 return Nonethe test passes.
I change the call to
function_02in test_type_error_w_keyword_arguments to be a call tofunction_02oftype_error.pyin thesrcfolder, intest_type_error.py62 src.type_error.function_01( 63 first='first', 64 second={'key': 'value'}, 65 ) 66 # function_02( 67 src.type_error.function_02( 68 third=(0, 1, 2, 'n'), 69 second=[0, 1, 2, 'n'], 70 first={0, 1, 2, 'n'}, 71 )the terminal is my friend, and shows TypeError
TypeError: function_02() got an unexpected keyword argument 'third'I add
thirdto the parentheses of the definition offunction_02intype_error.py16# def function_02(): 17# def function_02(one, two, three): 18def function_02(one, two, three, third): 19 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_02() missing 1 required positional argument: 'third' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_02() got an unexpected keyword argument 'second'I broke a test that was passing.
I add
secondto the parentheses16# def function_02(): 17# def function_02(one, two, three): 18# def function_02(one, two, three, third): 19def function_02(one, two, three, third, second): 20 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_02() missing 2 required positional arguments: 'third' and 'second' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_02() got an unexpected keyword argument 'first'I add
firstto the parentheses16# def function_02(): 17# def function_02(one, two, three): 18# def function_02(one, two, three, third): 19# def function_02(one, two, three, third, second): 20def function_02( 21 one, two, three, 22 third, second, first 23): 24 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_02() missing 3 required positional arguments: 'third', 'second', and 'first' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_02() missing 3 required positional arguments: 'one', 'two', and 'three'I remove
one,twoandthreefrom the parentheses16# def function_02(): 17# def function_02(one, two, three): 18# def function_02(one, two, three, third): 19# def function_02(one, two, three, third, second): 20def function_02( 21 # one, two, three, 22 third, second, first 23): 24 return Nonethe test passes.
I change the call to
function_03in test_type_error_w_keyword_arguments to be a call tofunction_03oftype_error.pyin thesrcfolder, intest_type_error.py67 src.type_error.function_02( 68 third=(0, 1, 2, 'n'), 69 second=[0, 1, 2, 'n'], 70 first={0, 1, 2, 'n'}, 71 ) 72 # function_03( 73 src.type_error.function_03( 74 first=None, 75 second=False, 76 third=True, 77 fourth=4, 78 )the terminal is my friend, and shows TypeError
TypeError: function_03() got an unexpected keyword argument 'first'because …
I add
firstto the parentheses of the definition offunction_03intype_error.py27# def function_03(): 28# def function_03(one, two, three, four): 29def function_03(one, two, three, four, first): 30 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_03() missing 1 required positional argument: 'first' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_03() got an unexpected keyword argument 'second'I add
secondto the parentheses27# def function_03(): 28# def function_03(one, two, three, four): 29# def function_03(one, two, three, four, first): 30def function_03( 31 one, two, three, four, 32 first, second, 33): 34 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_03() missing 2 required positional arguments: 'first' and 'second' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_03() got an unexpected keyword argument 'third'because there is a
call that does not have the same number of arguments as the parentheses of its definition.
call that uses a name (
third) that is not in the parentheses of its definition.
I add
thirdto the parentheses27# def function_03(): 28# def function_03(one, two, three, four): 29# def function_03(one, two, three, four, first): 30def function_03( 31 one, two, three, four, 32 # first, second, 33 first, second, third, 34): 35 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_03() missing 3 required positional arguments: 'first', 'second', and 'third' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_03() got an unexpected keyword argument 'fourth'. Did you mean 'four'?because there is a
call that does not have the same number of arguments as the parentheses of its definition.
call that uses a name (
fourth) that is not in the parentheses of its definition.
I add
fourthto the parentheses27# def function_03(): 28# def function_03(one, two, three, four): 29# def function_03(one, two, three, four, first): 30def function_03( 31 one, two, three, four, 32 # first, second, 33 # first, second, third, 34 first, second, third, fourth, 35): 36 return Nonethe terminal is my friend, and shows TypeError
FAILED ...::test_type_error_w_positional_arguments - TypeError: function_03() missing 4 required positional arguments: 'first', 'second', 'third', and 'fourth' FAILED ...::test_type_error_w_keyword_arguments - TypeError: function_03() missing 4 required positional arguments: 'one', 'two', 'three', and 'four'I remove
one,two,threeandfourfrom the parentheses27# def function_03(): 28# def function_03(one, two, three, four): 29# def function_03(one, two, three, four, first): 30def function_03( 31 # one, two, three, four, 32 # first, second 33 # first, second, third, 34 first, second, third, fourth, 35): 36 return Nonethe test passes.
I change the call to
function_04in test_type_error_w_keyword_arguments to be a call tofunction_04oftype_error.pyin thesrcfolder, intest_type_error.py73 src.type_error.function_03( 74 first=None, 75 second=False, 76 third=True, 77 fourth=4, 78 ) 79 # function_04(argument='value') 80 src.type_error.function_04(argument='value') 81 function_05( 82 argument_0='value_1', 83 argument_1=(0, 1, 2, 'n'), 84 )the terminal is my friend, and shows TypeError
TypeError: function_04() got an unexpected keyword argument 'argument'I change
onetoargumentin the parentheses of the definition offunction_04intype_error.py39# def function_04(): 40# def function_04(one): 41def function_04(argument): 42 return Nonethe test passes.
I change the call to
function_05in test_type_error_w_keyword_arguments to be a call tofunction_05oftype_error.pyin thesrcfolder, intest_type_error.py80 src.type_error.function_04(argument='value') 81 # function_05( 82 src.type_error.function_05( 83 argument_0='value_1', 84 argument_1=(0, 1, 2, 'n'), 85 )the terminal is my friend, and shows TypeError
TypeError: function_05() got an unexpected keyword argument 'argument_0'I change
onetoargument_0in the parentheses of the definition offunction_05intype_error.py45# def function_05(): 46# def function_05(one, two): 47def function_05(argument_0, two): 48 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_05() got an unexpected keyword argument 'argument_1'. Did you mean 'argument_0'?I change
twotoargument_1in the parentheses45# def function_05(): 46# def function_05(one, two): 47# def function_05(argument_0, two): 48def function_05(argument_0, argument_1): 49 return Nonethe test passes.
I change the call to
function_06in test_type_error_w_keyword_arguments to be a call tofunction_06oftype_error.pyin thesrcfolder, intest_type_error.py82 src.type_error.function_05( 83 argument_0='value_1', 84 argument_1=(0, 1, 2, 'n'), 85 ) 86 # function_06( 87 src.type_error.function_06( 88 argument_0='value_1', 89 argument_1=(0, 1, 2, 'n'), 90 argument_2=[0, 1, 2, 'n'], 91 )the terminal is my friend, and shows TypeError
TypeError: function_06() got an unexpected keyword argument 'argument_0'I change
onetoargument_0in the parentheses of the definition offunction_06intype_error.py52# def function_06(): 53# def function_06(one, two, three): 54def function_06(argument_0, two, three): 55 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_06() got an unexpected keyword argument 'argument_1'. Did you mean 'argument_0'?I change
twotoargument_1in the parentheses52# def function_06(): 53# def function_06(one, two, three): 54# def function_06(argument_0, two, three): 55def function_06(argument_0, argument_1, three): 56 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_06() got an unexpected keyword argument 'argument_2'. Did you mean 'argument_0'?I change
threetoargument_2in the parentheses52# def function_06(): 53# def function_06(one, two, three): 54# def function_06(argument_0, two, three): 55# def function_06(argument_0, argument_1, three): 56def function_06(argument_0, argument_1, argument_2): 57 return Nonethe test passes.
I change the call to
function_07in test_type_error_w_keyword_arguments to be a call tofunction_07oftype_error.pyin thesrcfolder, intest_type_error.py87 src.type_error.function_06( 88 argument_0='value_1', 89 argument_1=(0, 1, 2, 'n'), 90 argument_2=[0, 1, 2, 'n'], 91 ) 92 # function_07( 93 src.type_error.function_07( 94 argument_0=(0, 1, 2, 'n'), 95 argument_1=[0, 1, 2, 'n'], 96 argument_2={0, 1, 2, 'n'}, 97 argument_n={'key': 'value'}, 98 )the terminal is my friend, and shows TypeError
TypeError: function_07() got an unexpected keyword argument 'argument_0'because there is a call that uses a name (
argument_0) that is not in the parentheses of its definition.I change
onetoargument_0in the parentheses of the definition offunction_07intype_error.py60# def function_07(): 61# def function_07(one, two, three, four): 62def function_07(argument_0, two, three, four): 63 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_07() got an unexpected keyword argument 'argument_1'. Did you mean 'argument_0'?because …
I change
twotoargument_1in the parentheses60# def function_07(): 61# def function_07(one, two, three, four): 62# def function_07(argument_0, two, three, four): 63def function_07( 64 argument_0, argument_1, three, four 65): 66 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_07() got an unexpected keyword argument 'argument_2'. Did you mean 'argument_0'?because there is a call that uses a name (
argument_2) that is not in the parentheses of its definition.I change
threetoargument_2in the parentheses60# def function_07(): 61# def function_07(one, two, three, four): 62# def function_07(argument_0, two, three, four): 63def function_07( 64 # argument_0, argument_1, three, four 65 argument_0, argument_1, 66 argument_2, four 67): 68 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_07() got an unexpected keyword argument 'argument_n'. Did you mean 'argument_0'?I change
fourtoargument_nin the parentheses60# def function_07(): 61# def function_07(one, two, three, four): 62# def function_07(argument_0, two, three, four): 63def function_07( 64 # argument_0, argument_1, three, four 65 argument_0, argument_1, 66 # argument_2, four 67 argument_2, argument_n 68): 69 return Nonethe test passes.
I change the call to
function_08in test_type_error_w_keyword_arguments to be a call tofunction_08oftype_error.pyin thesrcfolder, intest_type_error.py97 src.type_error.function_07( 98 argument_0=(0, 1, 2, 'n'), 99 argument_1=[0, 1, 2, 'n'], 100 argument_2={0, 1, 2, 'n'}, 101 argument_n={'key': 'value'}, 102 ) 103 # function_08( 104 src.type_error.function_08( 105 argument='positional', 106 name='keyword', 107 ) 108 109 110def test_type_error_w_args_and_kwargs():the terminal is my friend, and shows TypeError
TypeError: function_08() got an unexpected keyword argument 'argument'because there is a call that uses a name (
argument) that is not in the parentheses of its definition.I change
onetoargumentin the parentheses of the definition offunction_08intype_error.py72# def function_08(): 73# def function_08(one, two): 74def function_08(argument, two): 75 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_08() got an unexpected keyword argument 'name'.I change
twotonamein the parentheses72# def function_08(): 73# def function_08(one, two): 74# def function_08(argument, two): 75def function_08(argument, name): 76 return Nonethe test passes.
I remove the commented lines from test_type_error_w_keyword_arguments in
test_type_error.py58def test_type_error_w_keyword_arguments(): 59 src.type_error.function_00(the_input=0) 60 src.type_error.function_01( 61 first='first', 62 second={'key': 'value'}, 63 )64 src.type_error.function_02( 65 third=(0, 1, 2, 'n'), 66 second=[0, 1, 2, 'n'], 67 first={0, 1, 2, 'n'}, 68 )69 src.type_error.function_03( 70 first=None, 71 second=False, 72 third=True, 73 fourth=4, 74 ) 75 src.type_error.function_04(argument='value')76 src.type_error.function_05( 77 argument_0='value_1', 78 argument_1=(0, 1, 2, 'n'), 79 )80 src.type_error.function_06( 81 argument_0='value_1', 82 argument_1=(0, 1, 2, 'n'), 83 argument_2=[0, 1, 2, 'n'], 84 )85 src.type_error.function_07( 86 argument_0=(0, 1, 2, 'n'), 87 argument_1=[0, 1, 2, 'n'], 88 argument_2={0, 1, 2, 'n'}, 89 argument_n={'key': 'value'}, 90 )91 src.type_error.function_08( 92 argument='positional', 93 name='keyword', 94 ) 95 96 97def test_type_error_w_args_and_kwargs():
I change the call to
function_00in test_type_error_w_args_and_kwargs to be a call tofunction_00oftype_error.pyin thesrcfolder, intest_type_error.py97def test_type_error_w_args_and_kwargs(): 98 # function_00('argument') 99 src.type_error.function_00('argument')the test is still green.
I change the call to
function_01tosrc.type_error.function_0197def test_type_error_w_args_and_kwargs(): 98 # function_00('argument') 99 src.type_error.function_00('argument') 100 # function_01(1, 0) 101 src.type_error.function_01(1, 0)still green.
I change the call to
function_02tosrc.type_error.function_02101 src.type_error.function_01(1, 0) 102 # function_02( 103 src.type_error.function_02( 104 third=True, second=False, 105 first=None, 106 )green.
I change the call to
function_03tosrc.type_error.function_03103 src.type_error.function_02( 104 third=True, second=False, 105 first=None, 106 ) 107 # function_03( 108 src.type_error.function_03( 109 second=[0, 1, 2, 'n'], 110 first=(0, 1, 2, 'n'), 111 third={0, 1, 2, 'n'}, 112 fourth={'key': 'value'}, 113 )still green.
I change the call to
function_04tosrc.type_error.function_04108 src.type_error.function_03( 109 second=[0, 1, 2, 'n'], 110 first=(0, 1, 2, 'n'), 111 third={0, 1, 2, 'n'}, 112 fourth={'key': 'value'}, 113 ) 114 # function_04('value') 115 src.type_error.function_04('value')the test is still green.
I change the call to
function_05tosrc.type_error.function_05115 src.type_error.function_04('value') 116 # function_05( 117 src.type_error.function_05( 118 (0, 1, 2, 'n'), 119 [0, 1, 2, 'n'], 120 )still green.
I change the call to
function_06tosrc.type_error.function_06117 src.type_error.function_05( 118 (0, 1, 2, 'n'), 119 [0, 1, 2, 'n'], 120 ) 121 # function_06( 122 src.type_error.function_06( 123 (0, 1, 2, 'n'), 124 [0, 1, 2, 'n'], 125 argument_2={0, 1, 2, 'n'}, 126 )green.
I change the call to
function_07tosrc.type_error.function_07122 src.type_error.function_06( 123 (0, 1, 2, 'n'), 124 [0, 1, 2, 'n'], 125 argument_2={0, 1, 2, 'n'}, 126 ) 127 # function_07( 128 src.type_error.function_07( 129 argument_n={'key': 'value'}, 130 argument_2={0, 1, 2, 'n'}, 131 argument_0=(0, 1, 2, 'n'), 132 argument_1=[0, 1, 2, 'n'], 133 )still green.
I change the call to
function_08tosrc.type_error.function_08128 src.type_error.function_07( 129 argument_n={'key': 'value'}, 130 argument_2={0, 1, 2, 'n'}, 131 argument_0=(0, 1, 2, 'n'), 132 argument_1=[0, 1, 2, 'n'], 133 ) 134 # function_08( 135 src.type_error.function_08( 136 'positional', 137 argument='keyword', 138 ) 139 140 141# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: function_08() got multiple values for argument 'argument'I change the order of the arguments in the definition of
function_08intype_error.py72# def function_08(): 73# def function_08(one, two): 74# def function_08(argument, two): 75# def function_08(argument, name): 76def function_08(name, argument): 77 return Nonethe test passes.
I remove the commented lines from
type_error.py1def function_00(the_input): 2 return None 3 4 5def function_01(first, second): 6 return None9def function_02( 10 third, second, first 11): 12 return None 13 14 15def function_03( 16 first, second, third, fourth, 17): 18 return None21def function_04(argument): 22 return None 23 24 25def function_05(argument_0, argument_1): 26 return None29def function_06(argument_0, argument_1, argument_2): 30 return None 31 32 33def function_07( 34 argument_0, argument_1, 35 argument_2, argument_n 36): 37 return None 38 39 40def function_08(name, argument): 41 return NoneI remove the commented lines from test_type_error_w_args_and_kwargs in
test_type_error.py97def test_type_error_w_args_and_kwargs(): 98 src.type_error.function_00('argument') 99 src.type_error.function_01(1, 0) 100 src.type_error.function_02( 101 third=True, second=False, 102 first=None, 103 )104 src.type_error.function_03( 105 second=[0, 1, 2, 'n'], 106 first=(0, 1, 2, 'n'), 107 third={0, 1, 2, 'n'}, 108 fourth={'key': 'value'}, 109 )110 src.type_error.function_04('value') 111 src.type_error.function_05( 112 (0, 1, 2, 'n'), 113 [0, 1, 2, 'n'], 114 )115 src.type_error.function_06( 116 (0, 1, 2, 'n'), 117 [0, 1, 2, 'n'], 118 argument_2={0, 1, 2, 'n'}, 119 )120 src.type_error.function_07( 121 argument_n={'key': 'value'}, 122 argument_2={0, 1, 2, 'n'}, 123 argument_0=(0, 1, 2, 'n'), 124 argument_1=[0, 1, 2, 'n'], 125 )126 src.type_error.function_08( 127 'positional', 128 argument='keyword', 129 ) 130 131 132# Exceptions seen 133# AssertionError 134# NameError 135# TypeError 136# ModuleNotFoundError 137# AttributeErrorI remove
function_00tofunction_08fromtest_type_error.pybecause they are no longer called1import src.type_error 2 3 4def test_type_error_w_positional_arguments():all the tests are passing because
function_00tofunction_08are now attributes oftype_error.pyin thesrcfolder and I can call them from outside the file withsrc.type_error.function_name()src.type_error.function_name() src └── type_error.py └── def function_name(): return Nonewhere
function_nameis the name of the function.I open a terminal then change directory to the type_error folder
cd type_errorI add
type_error.pyto git for trackinggit add src/type_error.pyI add a git commit message in the new terminal
git commit --all --message \ 'move functions to type_error.py'
close the project
I close
tests/test_type_error.pyandsrc/type_error.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
type_errorcd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
code from the chapter
what is next?
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.