what causes TypeError?
TypeError happens when an object (everything in Python is an object) is used in a way that it should not be.
preview
I have these tests by the end of the chapter
1def function_00(the_input):
2 return None
3
4
5def function_01(first, second):
6 return None
7
8
9def function_02(first, second, third):
10 return None
11
12
13def function_03(first, second, third, fourth):
14 return None
15
16
17def function_04(argument):
18 return None
19
20
21def function_05(argument_0, argument_1):
22 return None
23
24
25def function_06(
26 argument_0, argument_1,
27 argument_2,
28):
29 return None
30
31
32def function_07(
33 argument_0, argument_1,
34 argument_2, argument_n
35):
36 return None
37
38
39def function_08(name, argument):
40 return None
41
42
43def test_type_error_w_positional_arguments():
44 function_00('a')
45 function_01('a', 'b')
46 function_02('a', 'b', 'c')
47 function_03('a', 'b', 'c', 'd')
48 function_04('a')
49 function_05('a', 'b')
50 function_06('a', 'b', 'c')
51 function_07('a', 'b', 'c', 'd')
52 function_08('last', 'one')
53
54
55def test_type_error_w_keyword_arguments():
56 function_00(the_input=0)
57 function_01(
58 first='first',
59 second={'key': 'value'},
60 )
61 function_02(
62 third=(0, 1, 2, 'n'),
63 second=[0, 1, 2, 'n'],
64 first={0, 1, 2, 'n'},
65 )
66 function_03(
67 first=None,
68 second=False,
69 third=True,
70 fourth=4,
71 )
72 function_04(argument='value')
73 function_05(
74 argument_0='value_1',
75 argument_1=(0, 1, 2, 'n'),
76 )
77 function_06(
78 argument_0='value_1',
79 argument_1=(0, 1, 2, 'n'),
80 argument_2=[0, 1, 2, 'n'],
81 )
82 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 function_08(
89 argument='positional',
90 name='keyword',
91 )
92
93
94def test_type_error_w_args_and_kwargs():
95 function_00('argument')
96 function_01(1, 0)
97 function_02(
98 third=True, second=False,
99 first=None,
100 )
101 function_03(
102 second=[0, 1, 2, 'n'],
103 first=(0, 1, 2, 'n'),
104 third={0, 1, 2, 'n'},
105 fourth={'key': 'value'},
106 )
107 function_04('value')
108 function_05(
109 (0, 1, 2, 'n'),
110 [0, 1, 2, 'n'],
111 )
112 function_06(
113 (0, 1, 2, 'n'),
114 [0, 1, 2, 'n'],
115 argument_2={0, 1, 2, 'n'},
116 )
117 function_07(
118 argument_n={'key': 'value'},
119 argument_2={0, 1, 2, 'n'},
120 argument_0=(0, 1, 2, 'n'),
121 argument_1=[0, 1, 2, 'n'],
122 )
123 function_08(
124 'positional',
125 argument='keyword',
126 )
127
128
129# Exceptions seen
130# AssertionError
131# NameError
132# TypeError
questions about TypeError
Questions to think about as I go through the chapter
start the project
I name this project
type_errorI open a terminal
I change directory to the
type_errorfolder in thepumping_pythonfoldercd type_errorthe terminal shows
cd: no such file or directory: type_errorI use uv to make a directory for the project and initialize it
uv init type_errorthe terminal shows
Initialized project `type-error` at `../pumping_python/type_error`then goes back to the command line.
I change directory to the project
cd type_errorthe terminal shows I am in the
type_errorfolder.../pumping_python/type_errorI make a directory for the tests
mkdir teststhe terminal goes back to the command line.
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line.
I make an empty file for the tests in the
testsfoldertouch tests/test_type_error.pyNew-Item tests/test_type_error.pythe terminal goes back to the command line.
I open
test_type_error.pyI delete the text in the file then add the first failing test to
test_type_error.py1def test_failure(): 2 assert False is TrueI go back to the terminal to make a requirements file for the Python packages I need
echo "pytest" > requirements.txtthe terminal goes back to the command line.
I add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
I use uv to install pytest-watcher with the requirements file
uv add --requirement requirements.txtthe terminal shows that it installed pytest-watcher and its dependencies.
I add the new files and folder to git for tracking
git add .the terminal goes back to the command line.
I add a git commit message
git commit --all --message 'setup project'the terminal shows
[main (root-commit) a0b12c3] setup project 8 files changed, X insertions(+) create mode 100644 .gitignore create mode 100644 .python-version create mode 100644 README.md create mode 100644 pyproject.toml create mode 100644 requirements.txt create mode 100644 tests/__init__.py create mode 100644 tests/test_type_error.py create mode 100644 uv.lockthen goes back to the command line.
I use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
========================== ERRORS ========================== ______ ERROR collecting tests/test_type_error.py ______ tests/test_type_error.py:2: in <module> assert False is True E assert False is True ================= short test summary info ================== ERROR tests/test_type_error.py - assert False is True !!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!! ===================== 1 error in L.MNs =====================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 . --nowagainI add AssertionError to the list of Exceptions seen, in
test_type_error.py1def test_failure(): 2 assert False is True 3 4 5# Exceptions seen 6# AssertionErrorI change False to True in the assertion
1def test_failure(): 2 # assert False is True 3 assert True is True 4 5 6# Exceptions seen 7# AssertionErrorthe test passes.
test_type_error_w_positional_arguments
TypeError happens when I call a function in a way that is different from its signature. I have to match the number of arguments in the function definition when I call the function with positional arguments.
RED: make it fail
I change
test_failureto test_type_error_w_positional_arguments1def test_type_error_w_positional_arguments(): 2 function_00('a') 3 4 5# Exceptions seen 6# AssertionErrorthe terminal is my friend, and shows NameError
NameError: name 'function_00' is not definedbecause there is nothing named
function_00intest_type_error.py.I add NameError to the list of Exceptions seen
5# Exceptions seen 6# AssertionError 7# NameError
GREEN: make it pass
I add a definition for the function
1def test_type_error_w_positional_arguments(): 2 def function_00(): 3 return None 4 5 function_00('a') 6 7 8# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_positional_arguments .<locals>.function_00() takes 0 positional arguments but 1 was givenbecause
The call to
function_00which belongs to test_type_error_w_positional_arguments used one input ('a').The function definition (signature) of
function_00does not allow any inputs when it is called since the parentheses are empty.The call to a function must match its signature (definition).
I add TypeError to the list of Exceptions seen
8# Exceptions seen 9# AssertionError 10# NameError 11# TypeErrorI add a name in parentheses to the function definition
1def test_type_error_w_positional_arguments(): 2 # def function_00(): 3 def function_00(the_input): 4 return None 5 6 function_00('a') 7 8 9# Exceptions seenthe test passes because the call to the function matches its definition.
REFACTOR: make it better
I add a call to
function_016 function_00('a') 7 8 function_01('a', 'b') 9 10 11# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_01' is not definedbecause there is nothing named
function_01intest_type_error.py.I add a definition for
function_016 function_00('a') 7 8 def function_01(the_input): 9 return None 10 11 function_01('a', 'b') 12 13 14# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_positional_arguments .<locals>.function_01() takes 1 positional argument but 2 were givenbecause
The call to
function_01which belongs to test_type_error_w_positional_arguments uses two positional_arguments ('a'and'b').The function definition (signature) of
function_01only allows one input.The call to a function must match its signature (definition).
I change the name of the first input, then add another name in parentheses so that the call to the function and its definition match
6 function_00('a') 7 8 # def function_01(the_input): 9 def function_01(first, second): 10 return None 11 12 function_01('a', 'b') 13 14 15# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_0212 function_01('a', 'b') 13 14 function_02('a', 'b', 'c') 15 16 17# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_02' is not definedbecause there is nothing named
function_02intest_type_error.py.I add a definition for
function_0212 function_01('a', 'b') 13 14 def function_02(first, second): 15 return None 16 17 function_02('a', 'b', 'c') 18 19 20# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_positional_arguments .<locals>.function_02() takes 2 positional arguments but 3 were givenbecause
The call to
function_02which belongs to test_type_error_w_positional_arguments uses three positional arguments ('a','b'and'c').The function definition (signature) of
function_02only allows two inputs.The call to a function must match its signature (definition).
I add a third name in parentheses so that the call to
function_02and its definition match12 function_01('a', 'b') 13 14 # def function_02(first, second): 15 def function_02(first, second, third): 16 return None 17 18 function_02('a', 'b', 'c') 19 20 21# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_0318 function_02('a', 'b', 'c') 19 20 function_03('a', 'b', 'c', 'd') 21 22 23# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_03' is not definedbecause there is nothing named
function_03intest_type_error.py.I add a definition for
function_0318 function_02('a', 'b', 'c') 19 20 def function_03(first, second, third): 21 return None 22 23 function_03('a', 'b', 'c', 'd') 24 25 26# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_positional_arguments .<locals>.function_03() takes 3 positional arguments but 4 were givenbecause
The call to
function_03which belongs to test_type_error_w_positional_arguments uses four positional arguments ('a','b','c','d').The function definition (signature) of
function_03only allows three inputs.The call to a function must match its signature (definition).
I add a fourth name in parentheses so that the call to
function_03and its definition match18 function_02('a', 'b', 'c') 19 20 # def function_03(first, second, third): 21 def function_03(first, second, third, fourth): 22 return None 23 24 function_03('a', 'b', 'c', 'd') 25 26 27# Exceptions seenthe test passes because the call to the function matches its definition.
I remove the commented lines
1def test_type_error_w_positional_arguments(): 2 def function_00(the_input): 3 return None 4 5 function_00('a')7 def function_01(first, second): 8 return None 9 10 function_01('a', 'b')12 def function_02(first, second, third): 13 return None 14 15 function_02('a', 'b', 'c')17 def function_03(first, second, third, fourth): 18 return None 19 20 function_03('a', 'b', 'c', 'd') 21 22 23# Exceptions seenI open a new terminal then change directories to
type_errorcd type_errorI add a git commit message in the new terminal
git commit --all --message \ 'add test_type_error_w_positional_arguments'the terminal shows a summary of the changes then goes back to the command line.
I have to call a function with the same number of inputs that are in its definition.
test_type_error_w_keyword_arguments
TypeError happens when I call a function in a way that is different from its signature. I have to match the names of arguments in the function definition when I call the function with keyword arguments.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with a call to
function_04with a keyword argument20 function_03('a', 'b', 'c', 'd') 21 22 23def test_type_error_w_keyword_arguments(): 24 function_04(argument='value') 25 26 27# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_04' is not definedbecause there is nothing named
function_04intest_type_error.py.
GREEN: make it pass
I add a definition for
function_0423def test_type_error_w_keyword_arguments(): 24 def function_04(): 25 return None 26 27 function_04(argument='value') 28 29 30# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_keyword_arguments .<locals>.function_04() got an unexpected keyword argument 'argument'because
The call to
function_04which belongs to test_type_error_w_keyword_arguments uses a keyword argument (argument='value').The function definition (signature) of
function_04does not allow any inputs since the parentheses are empty.The call uses a name (
argument) that is not in the parentheses of its definition.The call to a function must match its signature (definition).
I add
argumentin the parentheses so that the call tofunction_04and its definition match23def test_type_error_w_keyword_arguments(): 24 # def function_04(): 25 def function_04(argument): 26 return None 27 28 function_04(argument='value') 29 30 31# Exceptions seenthe test passes because the call to the function matches its definition.
REFACTOR: make it better
I add a call to
function_05with keyword arguments28 function_04(argument='value') 29 30 function_05( 31 argument_0='value_1', 32 argument_1=(0, 1, 2, 'n'), 33 ) 34 35 36# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_05' is not definedI add a definition for
function_0528 function_04(argument='value') 29 30 def function_05(argument): 31 return None 32 33 function_05( 34 argument_0='value_1', 35 argument_1=(0, 1, 2, 'n'), 36 ) 37 38 39# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_keyword_arguments .<locals>.function_05() got an unexpected keyword argument 'argument_0'. Did you mean 'argument'?because
The call to
function_05which belongs to test_type_error_w_keyword_arguments uses keyword arguments (argument_0='value_1'andargument_1=(0, 1, 2, 'n')).The function definition (signature) of
function_05only allows one input, with the nameargument.The call uses a name (
argument_0) that is not in the parentheses of its definition.The call to a function must match its signature (definition).
I change the name of the input in the definition to match the call
28 function_04(argument='value') 29 30 # def function_05(argument): 31 def function_05(argument_0): 32 return None 33 34 function_05( 35 argument_0='value_1', 36 argument_1=(0, 1, 2, 'n'), 37 ) 38 39 40# Exceptions seenTypeError: test_type_error_w_keyword_arguments .<locals>.function_05() got an unexpected keyword argument 'argument_1'. Did you mean 'argument_0'?because the call uses a name (
argument_1) that is not in the parentheses of its definition.I add
argument_1to the parentheses so that the call tofunction_05and its definition match28 function_04(argument='value') 29 30 # def function_05(argument): 31 # def function_05(argument_0): 32 def function_05(argument_0, argument_1): 33 return None 34 35 function_05( 36 argument_0='value_1', 37 argument_1=(0, 1, 2, 'n'), 38 ) 39 40 41# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_06with keyword arguments35 function_05( 36 argument_0='value_1', 37 argument_1=(0, 1, 2, 'n'), 38 ) 39 40 function_06( 41 argument_0='value_1', 42 argument_1=(0, 1, 2, 'n'), 43 argument_2=[0, 1, 2, 'n'], 44 ) 45 46 47# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_06' is not definedI add a definition for
function_0635 function_05( 36 argument_0='value_1', 37 argument_1=(0, 1, 2, 'n'), 38 ) 39 40 def function_06(argument_0, argument_1): 41 return None 42 43 function_06( 44 argument_0='value_1', 45 argument_1=(0, 1, 2, 'n'), 46 argument_2=[0, 1, 2, 'n'], 47 ) 48 49 50# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_keyword_arguments .<locals>.function_06() got an unexpected keyword argument 'argument_2'. Did you mean 'argument_0'?because
The call to
function_06which belongs to test_type_error_w_keyword_arguments uses keyword arguments (argument_0='value_1',argument_1=(0, 1, 2, 'n')andargument_2=[0, 1, 2, 'n']).The function definition (signature) of
function_06only allows two inputs.The call uses a name (
argument_2) that is not in the parentheses of its definition.The call to a function must match its signature (definition).
I add
argument_2to the parentheses so that the call tofunction_06and its definition match35 function_05( 36 argument_0='value_1', 37 argument_1=(0, 1, 2, 'n'), 38 ) 39 40 # def function_06(argument_0, argument_1): 41 def function_06( 42 argument_0, argument_1, 43 argument_2, 44 ): 45 return None 46 47 function_06( 48 argument_0='value_1', 49 argument_1=(0, 1, 2, 'n'), 50 argument_2=[0, 1, 2, 'n'], 51 ) 52 53 54# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_07with keyword arguments47 function_06( 48 argument_0='value_1', 49 argument_1=(0, 1, 2, 'n'), 50 argument_2=[0, 1, 2, 'n'], 51 ) 52 53 function_07( 54 argument_0=(0, 1, 2, 'n'), 55 argument_1=[0, 1, 2, 'n'], 56 argument_2={0, 1, 2, 'n'}, 57 argument_n={'key': 'value'}, 58 ) 59 60 61# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_07' is not definedI add a definition for
function_0747 function_06( 48 argument_0='value_1', 49 argument_1=(0, 1, 2, 'n'), 50 argument_2=[0, 1, 2, 'n'], 51 ) 52 53 def function_07( 54 argument_0, argument_1, 55 argument_2, 56 ): 57 return None 58 59 function_07( 60 argument_0=(0, 1, 2, 'n'), 61 argument_1=[0, 1, 2, 'n'], 62 argument_2={0, 1, 2, 'n'}, 63 argument_n={'key': 'value'}, 64 ) 65 66 67# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_keyword_arguments .<locals>.function_07() got an unexpected keyword argument 'argument_n'. Did you mean 'argument_0'?because
The call to
function_07which belongs to test_type_error_w_keyword_arguments uses keyword arguments (argument_0=(0, 1, 2, 'n'),argument_1=[0, 1, 2, 'n'],argument_2={0, 1, 2, 'n'}andargument_n={'key': 'value'}).The function definition (signature) of
function_07only allows three inputs.The call uses a name (
argument_n) that is not in the parentheses of its definition.The call to a function must match its signature (definition).
I add
argument_nto the parentheses so that the call tofunction_07and its definition match47 function_06( 48 argument_0='value_1', 49 argument_1=(0, 1, 2, 'n'), 50 argument_2=[0, 1, 2, 'n'], 51 ) 52 53 def function_07( 54 argument_0, argument_1, 55 # argument_2 56 argument_2, argument_n, 57 ): 58 return None 59 60 function_07( 61 argument_0=(0, 1, 2, 'n'), 62 argument_1=[0, 1, 2, 'n'], 63 argument_2={0, 1, 2, 'n'}, 64 argument_n={'key': 'value'}, 65 ) 66 67 68# Exceptions seenthe test passes because the call to the function matches its definition.
I remove the commented lines
23def test_type_error_w_keyword_arguments(): 24 def function_04(argument): 25 return None 26 27 function_04(argument='value')32 def function_05(argument_0, argument_1): 33 return None 34 35 function_05( 36 argument_0='value_1', 37 argument_1=(0, 1, 2, 'n'), 38 )37 def function_06( 38 argument_0, argument_1, 39 argument_2, 40 ): 41 return None 42 43 function_06( 44 argument_0='value_1', 45 argument_1=(0, 1, 2, 'n'), 46 argument_2=[0, 1, 2, 'n'], 47 )49 def function_07( 50 argument_0, argument_1, 51 argument_2, argument_n 52 ): 53 return None 54 55 function_07( 56 argument_0=(0, 1, 2, 'n'), 57 argument_1=[0, 1, 2, 'n'], 58 argument_2={0, 1, 2, 'n'}, 59 argument_n={'key': 'value'}, 60 ) 61 62 63# Exceptions seenI add a git commit message in the other terminal
git commit --all --message \ 'add test_type_error_w_keyword_arguments'the terminal shows a summary of the changes then goes back to the command line.
test_type_error_w_args_and_kwargs
TypeError happens when I call a function in a way that is different from its signature. I cannot give two values for the same argument in the function definition when I call a function.
RED: make it fail
I go back to the terminal where the tests are running
I add a test with a call to
function_08with a positional and keyword argument55 function_07( 56 argument_0=(0, 1, 2, 'n'), 57 argument_1=[0, 1, 2, 'n'], 58 argument_2={0, 1, 2, 'n'}, 59 argument_n={'key': 'value'}, 60 ) 61 62 63def test_type_error_w_args_and_kwargs(): 64 function_08( 65 'positional', 66 argument='keyword', 67 ) 68 69 70# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_08' is not definedbecause there is no definition for
function_08in the file.
GREEN: make it pass
I add a function definition for
function_0863def test_type_error_w_args_and_kwargs(): 64 def function_08(argument): 65 return None 66 67 function_08( 68 'positional', 69 argument='keyword', 70 ) 71 72 73# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_args_and_kwargs .<locals>.function_08() got multiple values for argument 'argument'because
The call to
function_08which belongs to test_type_error_w_args_and_kwargs uses positional and keyword arguments ('positional'andargument='keyword').The function definition (signature) of
function_08takes one argument (argument). How does Python know which value to use forargumentif I use the position and the name?The call to a function must match its signature (definition).
I add another name to the parentheses
63def test_type_error_w_args_and_kwargs(): 64 # def function_08(argument): 65 def function_08(argument, name): 66 return Nonethe terminal still shows TypeError because the call gives
'positional'as the value for the first argument which isargumentin the definition, and it gives'keyword'as the value forargumentas a keyword argument.I change the order of the names in the parentheses of the function definition
63def test_type_error_w_args_and_kwargs(): 64 # def function_08(argument): 65 # def function_08(argument, name): 66 def function_08(name, argument): 67 return None 68 69 function_08( 70 'positional', 71 argument='keyword', 72 ) 73 74 75# Exceptions seenthe test passes because the call to the function matches its definition.
REFACTOR: make it better
I add another call to
function_0869 function_08( 70 'positional', 71 argument='keyword', 72 ) 73 function_08( 74 'positional', 75 name='keyword', 76 ) 77 78 79# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: test_type_error_w_args_and_kwargs .<locals>.function_08() got multiple values for argument 'name'because
The call to
function_08which belongs to test_type_error_w_args_and_kwargs uses'positional'as the value for the first argument which isnamein the definition, and uses'keyword'as the value fornameas a keyword argument.The function definition (signature) of
function_08takes two arguments (nameandargument). How does Python know which value to use fornameif I use the position and the name?
I use keyword arguments to make the call clearer
69 function_08( 70 'positional', 71 argument='keyword', 72 ) 73 function_08( 74 # 'positional', 75 argument='positional', 76 name='keyword', 77 ) 78 79 80# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_01with a positional and keyword argument63def test_type_error_w_args_and_kwargs(): 64 # def function_08(argument): 65 # def function_08(argument, name): 66 def function_08(name, argument): 67 return None 68 69 function_01(1, first=0) 70 71 function_08( 72 'positional', 73 argument='keyword', 74 )the terminal is my friend, and shows NameError
NameError: name 'function_01' is not definedbecause
function_01belongs to the test_type_error_w_positional_arguments function and I cannot reach it from outside test_type_error_w_positional_arguments.I move
function_01out of test_type_error_w_positional_arguments so that it can be called from anywhere in the file1def function_01(first, second): 2 return None 3 4 5def test_type_error_w_positional_arguments(): 6 def function_00(the_input): 7 return None 8 9 function_00('a') 10 function_01('a', 'b') 11 12 def function_02(first, second, third):the terminal is my friend, and shows TypeError
TypeError: function_01() got multiple values for argument 'first'because the call to
function_01from test_type_error_w_args_and_kwargs uses1as the value for the first argument which isfirstin the definition, and uses0as the value forfirstas a keyword argument.I change the call from test_type_error_w_args_and_kwargs to only use positional arguments to make it clearer
63def test_type_error_w_args_and_kwargs(): 64 # def function_08(argument): 65 # def function_08(argument, name): 66 def function_08(name, argument): 67 return None 68 69 # function_01(1, first=0) 70 function_01(1, 0) 71 72 function_08( 73 'positional', 74 argument='keyword', 75 )the test passes because the call to the function matches its definition.
I add a call to
function_02with arguments that are not clear69 # function_01(1, first=0) 70 function_01(1, 0) 71 function_02(False, first=None) 72 73 function_08( 74 'positional', 75 argument='keyword', 76 )the terminal is my friend, and shows NameError
NameError: name 'function_02' is not definedbecause
function_02belongs to the test_type_error_w_positional_arguments function and I cannot reach it from outside test_type_error_w_positional_arguments.I move
function_02out of test_type_error_w_positional_arguments so that it can be called from anywhere in the file1def function_01(first, second): 2 return None 3 4 5def function_02(first, second, third): 6 return None9def test_type_error_w_positional_arguments(): 10 def function_00(the_input): 11 return None 12 13 function_00('a') 14 function_01('a', 'b') 15 function_02('a', 'b', 'c') 16 17 def function_03(first, second, third, fourth):the terminal is my friend, and shows TypeError
TypeError: function_02() got multiple values for argument 'first'because the call to
function_02from test_type_error_w_args_and_kwargs usesFalseas the first positional argument which is namedfirstin the definition, and usesNoneas the value forfirstas a keyword argument.I change the first value in the call from test_type_error_w_args_and_kwargs to a keyword argument to make it clearer
63def test_type_error_w_args_and_kwargs(): 64 # def function_08(argument): 65 # def function_08(argument, name): 66 def function_08(name, argument): 67 return None 68 69 # function_01(1, first=0) 70 function_01(1, 0) 71 # function_02(False, first=None) 72 function_02(second=False, first=None) 73 74 function_08( 75 'positional', 76 argument='keyword', 77 )the terminal is my friend, and shows TypeError
TypeError: function_02() missing 1 required positional argument: 'third'because
The call to
function_02from test_type_error_w_args_and_kwargs uses two arguments.The function definition (signature) of
function_02takes three arguments (first,secondandthird).
I add a third argument to the call
69 # function_01(1, first=0) 70 function_01(1, 0) 71 # function_02(False, first=None) 72 # function_02(second=False, first=None) 73 function_02(True, second=False, first=None) 74 75 function_08( 76 'positional', 77 argument='keyword', 78 )the terminal is my friend, and shows TypeError
TypeError: function_02() got multiple values for argument 'first'because the call to
function_02from test_type_error_w_args_and_kwargs usesTrueas the first positional argument which is namedfirstin the definition, and usesNoneas the value forfirstas a keyword argument.I change the call from test_type_error_w_args_and_kwargs to use a keyword argument for
True, to make it clearer69 # function_01(1, first=0) 70 function_01(1, 0) 71 # function_02(False, first=None) 72 # function_02(second=False, first=None) 73 # function_02(True, second=False, first=None) 74 function_02( 75 third=True, second=False, 76 first=None, 77 )the test passes because the call to the function matches its definition.
I add a call to
function_0374 function_02( 75 third=True, second=False, 76 first=None, 77 ) 78 function_03( 79 [0, 1, 2, 'n'], 80 first=(0, 1, 2, 'n'), 81 third={0, 1, 2, 'n'}, 82 fourth={'key': 'value'}, 83 ) 84 85 function_08( 86 'positional', 87 argument='keyword', 88 )the terminal is my friend, and shows NameError
NameError: name 'function_03' is not definedbecause
function_03belongs to the test_type_error_w_positional_arguments function and I cannot reach it from outside test_type_error_w_positional_arguments.I move
function_03out of test_type_error_w_positional_arguments5def function_02(first, second, third): 6 return None 7 8 9def function_03(first, second, third, fourth): 10 return None 11 12 13def test_type_error_w_positional_arguments():13def test_type_error_w_positional_arguments(): 14 def function_00(the_input): 15 return None 16 17 function_00('a') 18 function_01('a', 'b') 19 function_02('a', 'b', 'c') 20 function_03('a', 'b', 'c', 'd') 21 22 23def test_type_error_w_keyword_arguments():the terminal is my friend, and shows TypeError
TypeError: function_03() got multiple values for argument 'first'because the call uses an argument in the first position and uses a keyword argument with the same name as the argument in the first position.
I add a keyword argument for the first value in the call to
function_03from test_type_error_w_args_and_kwargs to make it clearer74 function_02( 75 third=True, second=False, 76 first=None, 77 ) 78 function_03( 79 # [0, 1, 2, 'n'], 80 second=[0, 1, 2, 'n'], 81 first=(0, 1, 2, 'n'), 82 third={0, 1, 2, 'n'}, 83 fourth={'key': 'value'}, 84 )the test passes because the call to the function matches its definition.
I add a call to
function_07from test_type_error_w_args_and_kwargs78 function_03( 79 # [0, 1, 2, 'n'], 80 second=[0, 1, 2, 'n'], 81 first=(0, 1, 2, 'n'), 82 third={0, 1, 2, 'n'}, 83 fourth={'key': 'value'}, 84 ) 85 86 function_07( 87 {0, 1, 2, 'n'}, 88 {'key': 'value'}, 89 argument_0=(0, 1, 2, 'n'), 90 argument_1=[0, 1, 2, 'n'], 91 ) 92 function_08( 93 'positional', 94 argument='keyword', 95 )the terminal is my friend, and shows NameError
NameError: name 'function_07' is not definedbecause
function_07belongs to the test_type_error_w_keyword_arguments function and I cannot reach it from outside test_type_error_w_keyword_arguments.I move
function_07out of test_type_error_w_keyword_arguments9def function_03(first, second, third, fourth): 10 return None 11 12 13def function_07( 14 argument_0, argument_1, 15 argument_2, argument_n 16): 17 return None 18 19 20def test_type_error_w_positional_arguments():50 function_06( 51 argument_0='value_1', 52 argument_1=(0, 1, 2, 'n'), 53 argument_2=[0, 1, 2, 'n'], 54 ) 55 function_07( 56 argument_0=(0, 1, 2, 'n'), 57 argument_1=[0, 1, 2, 'n'], 58 argument_2={0, 1, 2, 'n'}, 59 argument_n={'key': 'value'}, 60 ) 61 62 63def test_type_error_w_args_and_kwargs():the terminal is my friend, and shows TypeError
TypeError: function_07() got multiple values for argument 'argument_0'because
The call to
function_07from test_type_error_w_args_and_kwargs uses an argument in the first position and uses a keyword argument with the same name as the argument in the first position.The function definition (signature) of
function_07takes four arguments. How does Python know which value to use forargument_0if I use both the position and the name of the argument for two values?The call to a function must match its signature (definition).
I change the call to use a keyword argument, then change its position
86 function_07( 87 # {0, 1, 2, 'n'}, 88 {'key': 'value'}, 89 argument_2={0, 1, 2, 'n'}, 90 argument_0=(0, 1, 2, 'n'), 91 argument_1=[0, 1, 2, 'n'], 92 ) 93 function_08( 94 'positional', 95 argument='keyword', 96 )the terminal still shows TypeError because the call uses an argument in the first position and uses a keyword argument with the same name as the argument in the first position.
I use a keyword argument to make it clearer
86 function_07( 87 # {0, 1, 2, 'n'}, 88 # {'key': 'value'}, 89 argument_4={'key': 'value'}, 90 argument_2={0, 1, 2, 'n'}, 91 argument_0=(0, 1, 2, 'n'), 92 argument_1=[0, 1, 2, 'n'], 93 )the terminal is my friend, and shows TypeError
TypeError: function_07() got an unexpected keyword argument 'argument_4'. Did you mean 'argument_0'?because
The call to
function_07from test_type_error_w_args_and_kwargs uses a name (argument_4) that is not in the function definition (signature).
I change the keyword argument to match the function definition
86 function_07( 87 # {0, 1, 2, 'n'}, 88 # {'key': 'value'}, 89 # argument_4={'key': 'value'}, 90 argument_n={'key': 'value'}, 91 argument_2={0, 1, 2, 'n'}, 92 argument_0=(0, 1, 2, 'n'), 93 argument_1=[0, 1, 2, 'n'], 94 )the test passes because the call to the function matches its definition.
I add a call to
function_00from test_type_error_w_args_and_kwargs63def test_type_error_w_args_and_kwargs(): 64 # def function_08(argument): 65 # def function_08(argument, name): 66 def function_08(name, argument): 67 return None 68 69 function_00() 70 # function_01(1, first=0)the terminal is my friend, and shows NameError
NameError: name 'function_00' is not definedbecause
function_00belongs to the test_type_error_w_positional_arguments function and I cannot reach it from outside test_type_error_w_positional_arguments.I move
function_00out of test_type_error_w_positional_arguments1def function_00(the_input): 2 return None 3 4 5def function_01(first, second): 6 return None24def test_type_error_w_positional_arguments(): 25 function_00('a') 26 function_01('a', 'b') 27 function_02('a', 'b', 'c') 28 function_03('a', 'b', 'c', 'd') 29 30 31def test_type_error_w_keyword_arguments():the terminal is my friend, and shows TypeError
TypeError: function_00() missing 1 required positional argument: 'the_input'because the function definition requires calls with one input and it got called with zero inputs.
I change the call from test_type_error_w_args_and_kwargs to match the signature
64def test_type_error_w_args_and_kwargs(): 65 # def function_08(argument): 66 # def function_08(argument, name): 67 def function_08(name, argument): 68 return None 69 70 # function_00() 71 function_00('argument') 72 # function_01(1, first=0)the test passes because the call to the function matches its definition.
I add a call to
function_04from test_type_error_w_args_and_kwargs81 function_03( 82 # [0, 1, 2, 'n'], 83 second=[0, 1, 2, 'n'], 84 first=(0, 1, 2, 'n'), 85 third={0, 1, 2, 'n'}, 86 fourth={'key': 'value'}, 87 ) 88 function_04('value_1', 'value2') 89 90 function_07( 91 # {0, 1, 2, 'n'}, 92 # {'key': 'value'}, 93 # argument_4={'key': 'value'}, 94 argument_n={'key': 'value'}, 95 argument_2={0, 1, 2, 'n'}, 96 argument_0=(0, 1, 2, 'n'), 97 argument_1=[0, 1, 2, 'n'], 98 ) 99 100 101# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_04' is not definedbecause
function_04belongs to the test_type_error_w_keyword_arguments function and I cannot reach it from outside test_type_error_w_keyword_arguments.I move
function_04out of test_type_error_w_keyword_arguments13def function_03(first, second, third, fourth): 14 return None 15 16 17def function_04(argument): 18 return None 19 20 21def function_07( 22 argument_0, argument_1, 23 argument_2, argument_n 24): 25 return None35def test_type_error_w_keyword_arguments(): 36 function_04(argument='value') 37 38 def function_05(argument_0, argument_1): 39 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_04() takes 1 positional argument but 2 were givenbecause the definition only allows one input, and it got called with two.
I change the call from test_type_error_w_args_and_kwargs to match the signature
89 # function_04('value_1', 'value2') 90 function_04('value') 91 92 function_07( 93 # {0, 1, 2, 'n'}, 94 # {'key': 'value'}, 95 # argument_4={'key': 'value'}, 96 argument_n={'key': 'value'}, 97 argument_2={0, 1, 2, 'n'}, 98 argument_0=(0, 1, 2, 'n'), 99 argument_1=[0, 1, 2, 'n'], 100 )the test passes because the call to the function matches its definition.
I add a call to
function_05from test_type_error_w_args_and_kwargs89 # function_04('value_1', 'value2') 90 function_04('value') 91 function_05((0, 1, 2, 'n')) 92 93 function_07( 94 # {0, 1, 2, 'n'}, 95 # {'key': 'value'}, 96 # argument_4={'key': 'value'}, 97 argument_n={'key': 'value'}, 98 argument_2={0, 1, 2, 'n'}, 99 argument_0=(0, 1, 2, 'n'), 100 argument_1=[0, 1, 2, 'n'], 101 )the terminal is my friend, and shows NameError
NameError: name 'function_05' is not definedI move
function_05out of test_type_error_w_keyword_arguments17def function_04(argument): 18 return None 19 20 21def function_05(argument_0, argument_1): 22 return None 23 24 25def function_07( 26 argument_0, argument_1, 27 argument_2, argument_n 28): 29 return None39def test_type_error_w_keyword_arguments(): 40 function_04(argument='value') 41 function_05( 42 argument_0='value_1', 43 argument_1=(0, 1, 2, 'n'), 44 ) 45 46 def function_06( 47 argument_0, argument_1, 48 argument_2, 49 ): 50 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_05() missing 1 required positional argument: 'argument_1'because the definition expects a call with two inputs and only got one input.
I change the call from test_type_error_w_args_and_kwargs to make it match the signature
89 # function_04('value_1', 'value2') 90 function_04('value') 91 # function_05((0, 1, 2, 'n')) 92 function_05( 93 (0, 1, 2, 'n'), 94 [0, 1, 2, 'n'], 95 )the test passes because the call to the function matches its definition.
I add a call to
function_06from test_type_error_w_args_and_kwargs92 function_05( 93 (0, 1, 2, 'n'), 94 [0, 1, 2, 'n'], 95 ) 96 function_06( 97 (0, 1, 2, 'n'), 98 [0, 1, 2, 'n'], 99 argument_2={0, 1, 2, 'n'}, 100 argument_n={'key': 'value'}, 101 ) 102 function_07( 103 # {0, 1, 2, 'n'}, 104 # {'key': 'value'}, 105 # argument_4={'key': 'value'}, 106 argument_n={'key': 'value'}, 107 argument_2={0, 1, 2, 'n'}, 108 argument_0=(0, 1, 2, 'n'), 109 argument_1=[0, 1, 2, 'n'], 110 )the terminal is my friend, and shows NameError
NameError: name 'function_06' is not definedbecause
function_06belongs to the test_type_error_w_keyword_arguments function.I move
function_06out of test_type_error_w_keyword_arguments21def function_05(argument_0, argument_1): 22 return None 23 24 25def function_06( 26 argument_0, argument_1, 27 argument_2, 28): 29 return None 30 31 32def function_07( 33 argument_0, argument_1, 34 argument_2, argument_n 35): 36 return None46def test_type_error_w_keyword_arguments(): 47 function_04(argument='value') 48 function_05( 49 argument_0='value_1', 50 argument_1=(0, 1, 2, 'n'), 51 ) 52 function_06( 53 argument_0='value_1', 54 argument_1=(0, 1, 2, 'n'), 55 argument_2=[0, 1, 2, 'n'], 56 ) 57 function_07( 58 argument_0=(0, 1, 2, 'n'), 59 argument_1=[0, 1, 2, 'n'], 60 argument_2={0, 1, 2, 'n'}, 61 argument_n={'key': 'value'}, 62 ) 63 64 65def test_type_error_w_args_and_kwargs():the terminal is my friend, and shows TypeError
TypeError: function_06() got an unexpected keyword argument 'argument_n'. Did you mean 'argument_0'?because the function got called with a name (keyword) (
argument_n) that is not in its definitionI remove
argument_n={'key': 'value'}from the call from test_type_error_w_args_and_kwargs92 function_05( 93 (0, 1, 2, 'n'), 94 [0, 1, 2, 'n'], 95 ) 96 function_06( 97 (0, 1, 2, 'n'), 98 [0, 1, 2, 'n'], 99 argument_2={0, 1, 2, 'n'}, 100 # argument_n={'key': 'value'}, 101 )the test passes because the call to the function matches its definition.
I remove the commented lines from test_type_error_w_args_and_kwargs
65def test_type_error_w_args_and_kwargs(): 66 def function_08(name, argument): 67 return None 68 69 function_00('argument') 70 function_01(1, 0)71 function_02( 72 third=True, second=False, 73 first=None, 74 ) 75 function_03( 76 second=[0, 1, 2, 'n'], 77 first=(0, 1, 2, 'n'), 78 third={0, 1, 2, 'n'}, 79 fourth={'key': 'value'}, 80 )81 function_04('value') 82 function_05( 83 (0, 1, 2, 'n'), 84 [0, 1, 2, 'n'], 85 ) 86 function_06( 87 (0, 1, 2, 'n'), 88 [0, 1, 2, 'n'], 89 argument_2={0, 1, 2, 'n'}, 90 )91 function_07( 92 argument_n={'key': 'value'}, 93 argument_2={0, 1, 2, 'n'}, 94 argument_0=(0, 1, 2, 'n'), 95 argument_1=[0, 1, 2, 'n'], 96 ) 97 function_08( 98 'positional', 99 argument='keyword', 100 )101 function_08( 102 argument='positional', 103 name='keyword', 104 ) 105 106 107# Exceptions seenI add a git commit message in the other terminal
git commit --all --message \ 'add test_type_error_w_args_and_kwargs'
test TypeError with required and unexpected arguments
RED: make it fail
I go back to the terminal where the tests are running
I add a call to
function_03from test_type_error_w_keyword_arguments46def test_type_error_w_keyword_arguments(): 47 function_03( 48 first=None, 49 second=False, 50 third=True, 51 ) 52 function_04(argument='value')the terminal is my friend, and shows TypeError
TypeError: function_03() missing 1 required positional argument: 'fourth'because the signature only allows calls with four inputs, and it got called with three.
GREEN: make it pass
I add fourth with a value to the call
46def test_type_error_w_keyword_arguments():
47 function_03(
48 first=None,
49 second=False,
50 third=True,
51 fourth=4,
52 )
53 function_04(argument='value')
the test passes because the call to the function matches its definition.
REFACTOR: make it better
I add a call to
function_02from test_type_error_w_keyword_arguments46def test_type_error_w_keyword_arguments(): 47 function_02( 48 fourth=4.0, 49 third=(0, 1, 2, 'n'), 50 second=[0, 1, 2, 'n'], 51 first={0, 1, 2, 'n'}, 52 ) 53 function_03( 54 first=None, 55 second=False, 56 third=True, 57 fourth=4, 58 )the terminal is my friend, and shows TypeError
TypeError: function_02() got an unexpected keyword argument 'fourth'because the call uses a name (
fourth) that is not in the parentheses of its definition.I remove the unexpected keyword argument from the call
46def test_type_error_w_keyword_arguments(): 47 function_02( 48 # fourth=4.0, 49 third=(0, 1, 2, 'n'), 50 second=[0, 1, 2, 'n'], 51 first={0, 1, 2, 'n'}, 52 ) 53 function_03( 54 first=None, 55 second=False, 56 third=True, 57 fourth=4, 58 )the test passes because the call to the function matches its definition.
I add a call to
function_01from test_type_error_w_keyword_arguments46def test_type_error_w_keyword_arguments(): 47 function_01( 48 second={'key': 'value'}, 49 ) 50 function_02( 51 # fourth=4.0, 52 third=(0, 1, 2, 'n'), 53 second=[0, 1, 2, 'n'], 54 first={0, 1, 2, 'n'}, 55 )the terminal is my friend, and shows TypeError
TypeError: function_01() missing 1 required positional argument: 'first'because the signature only allows calls with two inputs, and it got called with one.
I change the call to match the signature
46def test_type_error_w_keyword_arguments(): 47 function_01( 48 first='first', 49 second={'key': 'value'}, 50 )the test passes because the call to the function matches its definition.
I add a call to
function_00from test_type_error_w_keyword_arguments46def test_type_error_w_keyword_arguments(): 47 function_00(second=1, first=0) 48 function_01( 49 first='first', 50 second={'key': 'value'}, 51 )the terminal is my friend, and shows TypeError
TypeError: function_00() got an unexpected keyword argument 'second'I remove
secondfrom the call from test_type_error_w_keyword_arguments46def test_type_error_w_keyword_arguments(): 47 # function_00(second=1, first=0) 48 function_00(first=0) 49 function_01( 50 first='first', 51 second={'key': 'value'}, 52 )the terminal is my friend, and shows TypeError
TypeError: function_00() got an unexpected keyword argument 'first'because the call …
I remove
firstfrom the call46def test_type_error_w_keyword_arguments(): 47 # function_00(second=1, first=0) 48 # function_00(first=0) 49 function_00() 50 function_01( 51 first='first', 52 second={'key': 'value'}, 53 )the terminal is my friend, and shows TypeError
TypeError: function_00() missing 1 required positional argument: 'the_input'-
43def test_type_error_w_keyword_arguments(): 44 # function_00(second=1, first=0) 45 # function_00(first=0) 46 # function_00() 47 function_00(the_input=0) 48 function_01( 49 first='first', 50 second={'key': 'value'}, 51 )the test passes because I used the same name in the definition of the function when I called it with keyword arguments.
I move the call to
function_08from test_type_error_w_args_and_kwargs that uses keyword arguments to test_type_error_w_keyword_arguments77 function_07( 78 argument_0=(0, 1, 2, 'n'), 79 argument_1=[0, 1, 2, 'n'], 80 argument_2={0, 1, 2, 'n'}, 81 argument_n={'key': 'value'}, 82 ) 83 function_08( 84 argument='positional', 85 name='keyword', 86 ) 87 88 89def test_type_error_w_args_and_kwargs():115 function_07( 116 argument_n={'key': 'value'}, 117 argument_2={0, 1, 2, 'n'}, 118 argument_0=(0, 1, 2, 'n'), 119 argument_1=[0, 1, 2, 'n'], 120 ) 121 function_08( 122 'positional', 123 argument='keyword', 124 ) 125 126 127# Exceptions seen 128# AssertionError 129# NameError 130# TypeErrorthe terminal is my friend, and shows NameError
NameError: name 'function_08' is not definedI move
function_08out of test_type_error_w_args_and_kwargs32def function_07( 33 argument_0, argument_1, 34 argument_2, argument_n 35): 36 return None 37 38 39def function_08(name, argument): 40 return None 41 42 43def test_type_error_w_positional_arguments():93def test_type_error_w_args_and_kwargs(): 94 function_00('argument') 95 function_01(1, 0) 96 function_02(third=True, second=False, first=None)the test is green again.
I remove the commented lines from test_type_error_w_keyword_arguments
50def test_type_error_w_keyword_arguments(): 51 function_00(the_input=0) 52 function_01( 53 first='first', 54 second={'key': 'value'}, 55 )56 function_02( 57 third=(0, 1, 2, 'n'), 58 second=[0, 1, 2, 'n'], 59 first={0, 1, 2, 'n'}, 60 ) 61 function_03( 62 first=None, 63 second=False, 64 third=True, 65 fourth=4, 66 )67 function_04(argument='value') 68 function_05( 69 argument_0='value_1', 70 argument_1=(0, 1, 2, 'n'), 71 ) 72 function_06( 73 argument_0='value_1', 74 argument_1=(0, 1, 2, 'n'), 75 argument_2=[0, 1, 2, 'n'], 76 )77 function_07( 78 argument_0=(0, 1, 2, 'n'), 79 argument_1=[0, 1, 2, 'n'], 80 argument_2={0, 1, 2, 'n'}, 81 argument_n={'key': 'value'}, 82 ) 83 function_08( 84 argument='positional', 85 name='keyword', 86 ) 87 88 89def test_type_error_w_args_and_kwargs():
I add a call to
function_04from test_type_error_w_positional_arguments43def test_type_error_w_positional_arguments(): 44 function_00('a') 45 function_01('a', 'b') 46 function_02('a', 'b', 'c') 47 function_03('a', 'b', 'c', 'd') 48 function_04() 49 50 51def test_type_error_w_keyword_arguments():the terminal is my friend, and shows TypeError
TypeError: function_04() missing 1 required positional argument: 'argument'I add a value to the call to
function_0446 function_02('a', 'b', 'c') 47 function_03('a', 'b', 'c', 'd') 48 # function_04() 49 function_04('a') 50 51 52def test_type_error_w_keyword_arguments():the test passes because the call to the function matches its definition.
I add a call to
function_05from test_type_error_w_positional_arguments48 # function_04() 49 function_04('a') 50 function_05('a', 'b', 'c') 51 52 53def test_type_error_w_keyword_arguments():the terminal is my friend, and shows TypeError
TypeError: function_05() takes 2 positional arguments but 3 were givenI remove a value from the call to
function_0548 # function_04() 49 function_04('a') 50 # function_05('a', 'b', 'c') 51 function_05('a', 'b') 52 53 54def test_type_error_w_keyword_arguments():the test passes because the call to the function matches its definition.
I add a call to
function_06from test_type_error_w_positional_arguments50 # function_05('a', 'b', 'c') 51 function_05('a', 'b') 52 function_06('a', 'b') 53 54 55def test_type_error_w_keyword_arguments():the terminal is my friend, and shows TypeError
TypeError: function_06() missing 1 required positional argument: 'argument_2'I add a value to the call to
function_0650 # function_05('a', 'b', 'c') 51 function_05('a', 'b') 52 # function_06('a', 'b') 53 function_06('a', 'b', 'c') 54 55 56def test_type_error_w_keyword_arguments():the test passes because the call to the function matches its definition.
I add a call to
function_07from test_type_error_w_positional_arguments52 # function_06('a', 'b') 53 function_06('a', 'b', 'c') 54 function_07('a', 'b', 'c', 'd', 'e') 55 56 57def test_type_error_w_keyword_arguments():the terminal is my friend, and shows TypeError
TypeError: function_07() takes 4 positional arguments but 5 were givenI remove a value from the call to
function_0752 # function_06('a', 'b') 53 function_06('a', 'b', 'c') 54 # function_07('a', 'b', 'c', 'd', 'e') 55 function_07('a', 'b', 'c', 'd') 56 57 58def test_type_error_w_keyword_arguments():the test passes because the call to the function matches its definition.
I add a call to
function_08from test_type_error_w_positional_arguments54 # function_07('a', 'b', 'c', 'd', 'e') 55 function_07('a', 'b', 'c', 'd') 56 function_08('last') 57 58 59def test_type_error_w_keyword_arguments():the terminal is my friend, and shows TypeError
TypeError: function_08() missing 1 required positional argument: 'argument'I add a value to the call to
function_0854 # function_07('a', 'b', 'c', 'd', 'e') 55 function_07('a', 'b', 'c', 'd') 56 # function_08('last') 57 function_08('last', 'one') 58 59 60def test_type_error_w_keyword_arguments():the test passes because the call to the function matches its definition.
I remove the commented lines from test_type_error_w_positional_arguments
43def test_type_error_w_positional_arguments(): 44 function_00('a') 45 function_01('a', 'b') 46 function_02('a', 'b', 'c') 47 function_03('a', 'b', 'c', 'd') 48 function_04('a') 49 function_05('a', 'b') 50 function_06('a', 'b', 'c') 51 function_07('a', 'b', 'c', 'd') 52 function_08('last', 'one') 53 54 55def test_type_error_w_keyword_arguments():I add a git commit message in the other terminal
git commit --all --message \ 'test with required and unexpected arguments'
I have to call a function with the same number or names of arguments that are in its definition.
close the project
I close
test_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
I ran tests for TypeError with
My problem with the tests is that they all show the correct way to call the functions I made in the file. If someone reads the file or runs it, there is no way for them to know how any of the calls are related to TypeError unless they go through the process with me, there has to be a better way.
code from the chapter
what is next?
Would you like to test using a function to make a string from input?
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.