separate and equal TypeError
I want to move the functions 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='value1',
36 argument_1=(0, 1, 2, 'n'),
37 )
38 src.type_error.function_06(
39 argument_0='value1',
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', name='keyword'
51 )
52
53
54def test_type_error_w_args_and_kwargs():
55 src.type_error.function_00('argument')
56 src.type_error.function_01(1, 0)
57 src.type_error.function_02(
58 third=True, second=False, first=None
59 )
60 src.type_error.function_03(
61 second=[0, 1, 2, 'n'],
62 first=(0, 1, 2, 'n'),
63 third={0, 1, 2, 'n'},
64 fourth={'key': 'value'}
65 )
66 src.type_error.function_04('value')
67 src.type_error.function_05(
68 (0, 1, 2, 'n'),
69 [0, 1, 2, 'n'],
70 )
71 src.type_error.function_06(
72 (0, 1, 2, 'n'),
73 [0, 1, 2, 'n'],
74 argument_2={0, 1, 2, 'n'},
75 )
76 src.type_error.function_07(
77 argument_n={'key': 'value'},
78 argument_2={0, 1, 2, 'n'},
79 argument_0=(0, 1, 2, 'n'),
80 argument_1=[0, 1, 2, 'n'],
81 )
82 src.type_error.function_08(
83 'positional', argument='keyword'
84 )
85
86
87# Exceptions seen
88# AssertionError
89# NameError
90# TypeError
91# ModuleNotFoundError
92# AttributeError
open the project
I open a terminal
I change directory to the
type_errorfolder in thepumping_pythonfoldercd type_errorthe terminal shows I am in the
type_errorfolder.../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 a call to function_00 of type_error.py in the src folder
40def test_type_error_w_positional_arguments():
41 # function_00('a')
42 src.type_error.function_00('a')
43 function_01('a', 'b')
44 function_02('a', 'b', 'c')
45 function_03('a', 'b', 'c', 'd')
46 function_04('a')
47 function_05('a', 'b')
48 function_06('a', 'b', 'c')
49 function_07('a', 'b', 'c', 'd')
50 function_08('last', 'one')
51
52
53def 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 AttributeError to the list of Exceptions seen
121# Exceptions seen 122# AssertionError 123# NameError 124# TypeError 125# ModuleNotFoundErrorI open another terminal then make sure I am in the
type_errorfolderI 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 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
121# Exceptions seen 122# AssertionError 123# NameError 124# TypeError 125# ModuleNotFoundError 126# AttributeErrorI open
type_error.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
When
import src.type_errorruns, Python brings in an object for thetype_error.pyfile from thesrcfolder so I can use it intest_type_error.pyassrc.type_error.When
src.type_error.function_00is called, Python calls thefunction_00function from the object it imported for thetype_error.pyfile from thesrcfolder (src.type_error).
I think of
src.type_error.function_00like an addresssrc └── type_error.py └── def function_00(one): return Nonefunction_00is something intype_error, in this case it is a function intype_errortype_erroris something insrc, in this case it istype_error.py(a module) in thesrcfoldersrcis 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 a call tofunction_01oftype_error.pyin thesrcfolder, intest_type_error.py43def test_type_error_w_positional_arguments(): 44 # function_00('a') 45 src.type_error.function_00('a') 46 # function_01('a', 'b') 47 src.type_error.function_01('a', 'b') 48 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.py └── def function_01(one, two): return NoneI change the call to
function_02in test_type_error_w_positional_arguments to a call tofunction_02oftype_error.pyin thesrcfolder, intest_type_error.py43def test_type_error_w_positional_arguments(): 44 # function_00('a') 45 src.type_error.function_00('a') 46 # function_01('a', 'b') 47 src.type_error.function_01('a', 'b') 48 # function_02('a', 'b', 'c') 49 src.type_error.function_02('a', 'b', 'c') 50 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.py └── def function_02(one, two, three): return NoneI change the call to
function_03in test_type_error_w_positional_arguments to a call tofunction_03oftype_error.pyin thesrcfolder, intest_type_error.py43def test_type_error_w_positional_arguments(): 44 # function_00('a') 45 src.type_error.function_00('a') 46 # function_01('a', 'b') 47 src.type_error.function_01('a', 'b') 48 # function_02('a', 'b', 'c') 49 src.type_error.function_02('a', 'b', 'c') 50 # function_03('a', 'b', 'c', 'd') 51 src.type_error.function_03('a', 'b', 'c', 'd') 52 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.py └── def function_03(one, two, three, four): return NoneI change the call to
function_04in test_type_error_w_positional_arguments to a call tofunction_04oftype_error.pyin thesrcfolder, intest_type_error.py43def test_type_error_w_positional_arguments(): 44 # function_00('a') 45 src.type_error.function_00('a') 46 # function_01('a', 'b') 47 src.type_error.function_01('a', 'b') 48 # function_02('a', 'b', 'c') 49 src.type_error.function_02('a', 'b', 'c') 50 # function_03('a', 'b', 'c', 'd') 51 src.type_error.function_03('a', 'b', 'c', 'd') 52 # function_04('a') 53 src.type_error.function_04('a') 54 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.py └── def function_04(one): return NoneI change the call to
function_05in test_type_error_w_positional_arguments to a call tofunction_05oftype_error.pyin thesrcfolder, intest_type_error.py43def test_type_error_w_positional_arguments(): 44 # function_00('a') 45 src.type_error.function_00('a') 46 # function_01('a', 'b') 47 src.type_error.function_01('a', 'b') 48 # function_02('a', 'b', 'c') 49 src.type_error.function_02('a', 'b', 'c') 50 # function_03('a', 'b', 'c', 'd') 51 src.type_error.function_03('a', 'b', 'c', 'd') 52 # function_04('a') 53 src.type_error.function_04('a') 54 # function_05('a', 'b') 55 src.type_error.function_05('a', 'b') 56 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.py └── def function_05(one, two): return NoneI change the call to
function_06in test_type_error_w_positional_arguments to a call tofunction_06oftype_error.pyin thesrcfolder, intest_type_error.py43def test_type_error_w_positional_arguments(): 44 # function_00('a') 45 src.type_error.function_00('a') 46 # function_01('a', 'b') 47 src.type_error.function_01('a', 'b') 48 # function_02('a', 'b', 'c') 49 src.type_error.function_02('a', 'b', 'c') 50 # function_03('a', 'b', 'c', 'd') 51 src.type_error.function_03('a', 'b', 'c', 'd') 52 # function_04('a') 53 src.type_error.function_04('a') 54 # function_05('a', 'b') 55 src.type_error.function_05('a', 'b') 56 # function_06('a', 'b', 'c') 57 src.type_error.function_06('a', 'b', 'c') 58 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.py └── def function_06(one, two, three): return NoneI change the call to
function_07in test_type_error_w_positional_arguments to a call tofunction_07oftype_error.pyin thesrcfolder, intest_type_error.py43def test_type_error_w_positional_arguments(): 44 # function_00('a') 45 src.type_error.function_00('a') 46 # function_01('a', 'b') 47 src.type_error.function_01('a', 'b') 48 # function_02('a', 'b', 'c') 49 src.type_error.function_02('a', 'b', 'c') 50 # function_03('a', 'b', 'c', 'd') 51 src.type_error.function_03('a', 'b', 'c', 'd') 52 # function_04('a') 53 src.type_error.function_04('a') 54 # function_05('a', 'b') 55 src.type_error.function_05('a', 'b') 56 # function_06('a', 'b', 'c') 57 src.type_error.function_06('a', 'b', 'c') 58 # function_07('a', 'b', 'c', 'd') 59 src.type_error.function_07('a', 'b', 'c', 'd') 60 function_08('last', 'one')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.py └── def function_07(one, two, three, four): return NoneI change the call to
function_08in test_type_error_w_positional_arguments to a call tofunction_08oftype_error.pyin thesrcfolder, intest_type_error.py43def test_type_error_w_positional_arguments(): 44 # function_00('a') 45 src.type_error.function_00('a') 46 # function_01('a', 'b') 47 src.type_error.function_01('a', 'b') 48 # function_02('a', 'b', 'c') 49 src.type_error.function_02('a', 'b', 'c') 50 # function_03('a', 'b', 'c', 'd') 51 src.type_error.function_03('a', 'b', 'c', 'd') 52 # function_04('a') 53 src.type_error.function_04('a') 54 # function_05('a', 'b') 55 src.type_error.function_05('a', 'b') 56 # function_06('a', 'b', 'c') 57 src.type_error.function_06('a', 'b', 'c') 58 # function_07('a', 'b', 'c', 'd') 59 src.type_error.function_07('a', 'b', 'c', 'd') 60 # function_08('last', 'one') 61 src.type_error.function_08('last', 'one') 62 63 64def 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.py └── def function_08(one, two): return NoneI remove the commented lines from test_type_error_w_positional_arguments in
test_type_error.py43def test_type_error_w_positional_arguments(): 44 src.type_error.function_00('a') 45 src.type_error.function_01('a', 'b') 46 src.type_error.function_02('a', 'b', 'c') 47 src.type_error.function_03('a', 'b', 'c', 'd') 48 src.type_error.function_04('a') 49 src.type_error.function_05('a', 'b') 50 src.type_error.function_06('a', 'b', 'c') 51 src.type_error.function_07('a', 'b', 'c', 'd') 52 src.type_error.function_08('last', 'one') 53 54 55def test_type_error_w_keyword_arguments():
I change the call to
function_00in test_type_error_w_keyword_arguments to a call tofunction_00oftype_error.pyin thesrcfolder, intest_type_error.py55def test_type_error_w_keyword_arguments(): 56 # function_00(the_input=0) 57 src.type_error.function_00(the_input=0) 58 function_01( 59 first='first', 60 second={'key': 'value'}, 61 )the terminal is my friend, and shows TypeError
TypeError: function_00() got an unexpected keyword argument 'the_input'I add
the_inputin 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 a call tofunction_01oftype_error.pyin thesrcfolder, intest_type_error.py55def test_type_error_w_keyword_arguments(): 56 # function_00(the_input=0) 57 src.type_error.function_00(the_input=0) 58 # function_01( 59 src.type_error.function_01( 60 first='first', 61 second={'key': 'value'}, 62 )the terminal is my friend, and shows TypeError
TypeError: function_01() got an unexpected keyword argument 'first'I add
firstin 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 a call tofunction_02oftype_error.pyin thesrcfolder, intest_type_error.py55def test_type_error_w_keyword_arguments(): 56 # function_00(the_input=0) 57 src.type_error.function_00(the_input=0) 58 # function_01( 59 src.type_error.function_01( 60 first='first', 61 second={'key': 'value'}, 62 ) 63 # function_02( 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 )the terminal is my friend, and shows TypeError
TypeError: function_02() got an unexpected keyword argument 'third'I add
thirdin 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 'threeI 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 a call tofunction_03oftype_error.pyin thesrcfolder, intest_type_error.py55def test_type_error_w_keyword_arguments(): 56 # function_00(the_input=0) 57 src.type_error.function_00(the_input=0) 58 # function_01( 59 src.type_error.function_01( 60 first='first', 61 second={'key': 'value'}, 62 ) 63 # function_03( 64 src.type_error.function_03( 65 third=(0, 1, 2, 'n'), 66 second=[0, 1, 2, 'n'], 67 first={0, 1, 2, 'n'}, 68 ) 69 # function_03( 70 src.type_error.function_03( 71 first=None, 72 second=False, 73 third=True, 74 fourth=4, 75 )the terminal is my friend, and shows TypeError
TypeError: function_03() got an unexpected keyword argument 'first'I add
firstin 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'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'?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 a call tofunction_04oftype_error.pyin thesrcfolder, intest_type_error.py55def test_type_error_w_keyword_arguments(): 56 # function_00(the_input=0) 57 src.type_error.function_00(the_input=0) 58 # function_01( 59 src.type_error.function_01( 60 first='first', 61 second={'key': 'value'}, 62 ) 63 # function_02( 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 # function_03( 70 src.type_error.function_03( 71 first=None, 72 second=False, 73 third=True, 74 fourth=4, 75 ) 76 # function_04(argument='value') 77 src.type_error.function_04(argument='value')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 a call tofunction_05oftype_error.pyin thesrcfolder, intest_type_error.py55def test_type_error_w_keyword_arguments(): 56 # function_00(the_input=0) 57 src.type_error.function_00(the_input=0) 58 # function_01( 59 src.type_error.function_01( 60 first='first', 61 second={'key': 'value'}, 62 ) 63 # function_02( 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 # function_03( 70 src.type_error.function_03( 71 first=None, 72 second=False, 73 third=True, 74 fourth=4, 75 ) 76 # function_04(argument='value') 77 src.type_error.function_04(argument='value') 78 # function_05( 79 src.type_error.function_05( 80 argument_0='value1', 81 argument_1=(0, 1, 2, 'n'), 82 )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 a call tofunction_06oftype_error.pyin thesrcfolder, intest_type_error.py78 # function_05( 79 src.type_error.function_05( 80 argument_0='value1', 81 argument_1=(0, 1, 2, 'n'), 82 ) 83 # function_06( 84 src.type_error.function_06( 85 argument_0='value1', 86 argument_1=(0, 1, 2, 'n'), 87 argument_2=[0, 1, 2, 'n'], 88 )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 a call tofunction_07oftype_error.pyin thesrcfolder, intest_type_error.py83 # function_06( 84 src.type_error.function_06( 85 argument_0='value1', 86 argument_1=(0, 1, 2, 'n'), 87 argument_2=[0, 1, 2, 'n'], 88 ) 89 # function_07( 90 src.type_error.function_07( 91 argument_0=(0, 1, 2, 'n'), 92 argument_1=[0, 1, 2, 'n'], 93 argument_2={0, 1, 2, 'n'}, 94 argument_n={'key': 'value'}, 95 )the terminal is my friend, and shows TypeError
TypeError: function_07() got an unexpected keyword argument 'argument_0'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'?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'?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 a call tofunction_08oftype_error.pyin thesrcfolder, intest_type_error.py89 # function_07( 90 src.type_error.function_07( 91 argument_0=(0, 1, 2, 'n'), 92 argument_1=[0, 1, 2, 'n'], 93 argument_2={0, 1, 2, 'n'}, 94 argument_n={'key': 'value'}, 95 ) 96 # function_08(argument='positional', name='keyword') 97 src.type_error.function_08( 98 argument='positional', name='keyword' 99 ) 100 101 102def test_type_error_w_args_and_kwargs():the terminal is my friend, and shows TypeError
TypeError: function_07() got an unexpected keyword argument 'argument'I change
onetoargument_0in 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 parentheses60# def function_08(): 61# def function_08(one, two): 62# def function_08(argument, two): 63def function_08(argument, name): 64 return Nonethe test passes.
I remove the commented lines from test_type_error_w_keyword_arguments in
test_type_error.py55def test_type_error_w_keyword_arguments(): 56 src.type_error.function_00(the_input=0) 57 src.type_error.function_01( 58 first='first', 59 second={'key': 'value'}, 60 ) 61 src.type_error.function_02( 62 third=(0, 1, 2, 'n'), 63 second=[0, 1, 2, 'n'], 64 first={0, 1, 2, 'n'}, 65 ) 66 src.type_error.function_03( 67 first=None, 68 second=False, 69 third=True, 70 fourth=4, 71 ) 72 src.type_error.function_04(argument='value') 73 src.type_error.function_05( 74 argument_0='value1', 75 argument_1=(0, 1, 2, 'n'), 76 ) 77 src.type_error.function_06( 78 argument_0='value1', 79 argument_1=(0, 1, 2, 'n'), 80 argument_2=[0, 1, 2, 'n'], 81 ) 82 src.type_error.function_07( 83 argument_0=(0, 1, 2, 'n'), 84 argument_1=[0, 1, 2, 'n'], 85 argument_2={0, 1, 2, 'n'}, 86 argument_n={'key': 'value'}, 87 ) 88 src.type_error.function_08( 89 argument='positional', name='keyword' 90 ) 91 92 93def test_type_error_w_args_and_kwargs():
I change the call to
function_00in test_type_error_w_args_and_kwargs to a call tofunction_00oftype_error.pyin thesrcfolder, intest_type_error.py93def test_type_error_w_args_and_kwargs(): 94 # function_00('argument') 95 src.type_error.function_00('argument')the test is still green.
I change the call to
function_01tosrc.type_error.function_0193def test_type_error_w_args_and_kwargs(): 94 # function_00('argument') 95 src.type_error.function_00('argument') 96 # function_01(1, 0) 97 src.type_error.function_01(1, 0)still green.
I change the call to
function_02tosrc.type_error.function_0293def test_type_error_w_args_and_kwargs(): 94 # function_00('argument') 95 src.type_error.function_00('argument') 96 # function_01(1, 0) 97 src.type_error.function_01(1, 0) 98 # function_02(third=True, second=False, first=None) 99 src.type_error.function_02( 100 third=True, second=False, first=None 101 )green.
I change the call to
function_03tosrc.type_error.function_0398 # function_02(third=True, second=False, first=None) 99 src.type_error.function_02( 100 third=True, second=False, first=None 101 ) 102 # function_03( 103 src.type_error.function_03( 104 second=[0, 1, 2, 'n'], 105 first=(0, 1, 2, 'n'), 106 third={0, 1, 2, 'n'}, 107 fourth={'key': 'value'} 108 )still green.
I change the call to
function_04tosrc.type_error.function_04102 # function_03( 103 src.type_error.function_03( 104 second=[0, 1, 2, 'n'], 105 first=(0, 1, 2, 'n'), 106 third={0, 1, 2, 'n'}, 107 fourth={'key': 'value'} 108 ) 109 # function_04('value') 110 src.type_error.function_04('value')the test is still green.
I change the call to
function_05tosrc.type_error.function_05109 # function_04('value') 110 src.type_error.function_04('value') 111 # function_05( 112 src.type_error.function_05( 113 (0, 1, 2, 'n'), 114 [0, 1, 2, 'n'], 115 )still green.
I change the call to
function_06tosrc.type_error.function_06111 # function_05( 112 src.type_error.function_05( 113 (0, 1, 2, 'n'), 114 [0, 1, 2, 'n'], 115 ) 116 # function_06( 117 src.type_error.function_06( 118 (0, 1, 2, 'n'), 119 [0, 1, 2, 'n'], 120 argument_2={0, 1, 2, 'n'}, 121 )green.
I change the call to
function_07tosrc.type_error.function_07116 # function_06( 117 src.type_error.function_06( 118 (0, 1, 2, 'n'), 119 [0, 1, 2, 'n'], 120 argument_2={0, 1, 2, 'n'}, 121 ) 122 # function_07( 123 src.type_error.function_07( 124 argument_n={'key': 'value'}, 125 argument_2={0, 1, 2, 'n'}, 126 argument_0=(0, 1, 2, 'n'), 127 argument_1=[0, 1, 2, 'n'], 128 )still green.
I change the call to
function_08tosrc.type_error.function_08122 # function_07( 123 src.type_error.function_07( 124 argument_n={'key': 'value'}, 125 argument_2={0, 1, 2, 'n'}, 126 argument_0=(0, 1, 2, 'n'), 127 argument_1=[0, 1, 2, 'n'], 128 ) 129 # function_08('positional', argument='keyword') 130 src.type_error.function_08( 131 'positional', argument='keyword' 132 ) 133 134 135# 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 None 7 8 9def function_02( 10 third, second, first 11): 12 return None 13 14 15def function_03( 16 first, second, third, fourth 17): 18 return None 19 20 21def function_04(argument): 22 return None 23 24 25def function_05(argument_0, argument_1): 26 return None 27 28 29def 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.pydef test_type_error_w_args_and_kwargs(): src.type_error.function_00('argument') src.type_error.function_01(1, 0) src.type_error.function_02( third=True, second=False, first=None ) src.type_error.function_03( second=[0, 1, 2, 'n'], first=(0, 1, 2, 'n'), third={0, 1, 2, 'n'}, fourth={'key': 'value'} ) src.type_error.function_04('value') src.type_error.function_05( (0, 1, 2, 'n'), [0, 1, 2, 'n'], ) src.type_error.function_06( (0, 1, 2, 'n'), [0, 1, 2, 'n'], argument_2={0, 1, 2, 'n'}, ) src.type_error.function_07( argument_n={'key': 'value'}, argument_2={0, 1, 2, 'n'}, argument_0=(0, 1, 2, 'n'), argument_1=[0, 1, 2, 'n'], ) src.type_error.function_08( 'positional', argument='keyword' ) # Exceptions seen # AssertionError # NameError # TypeError # ModuleNotFoundError # AttributeErrorI remove
functions_00tofunctions_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
functions_00tofunctions_08are now attributes oftype_error.pyin thesrcfolder and I can call them from outside the file withsrc.type_error.function_name()src └── type_error.py └── def function_name(): return Nonewhere
function_nameis the name of the function.I add a git commit message in the other terminal
git commit --all --message \ 'move functions to type_error.py'
I have to call a function with the same number or names of arguments that are in its definition.
review
code from the chapter
what is next?
so far you know
Would you like to test using a class to remove repetition of inputs from functions?
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.