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(argument_0, argument_1, argument_2):
26 return None
27
28
29def function_07(
30 argument_0, argument_1,
31 argument_2, argument_n,
32):
33 return None
34
35
36def function_08(name, argument):
37 return None
38
39
40def test_type_error_w_positional_arguments():
41 function_00('a')
42 function_01('a', 'b')
43 function_02('a', 'b', 'c')
44 function_03('a', 'b', 'c', 'd')
45 function_04('a')
46 function_05('a', 'b')
47 function_06('a', 'b', 'c')
48 function_07('a', 'b', 'c', 'd')
49 function_08('last', 'one')
50
51
52def test_type_error_w_keyword_arguments():
53 function_00(the_input=0)
54 function_01(
55 first='first',
56 second={'key': 'value'},
57 )
58 function_02(
59 third=(0, 1, 2, 'n'),
60 second=[0, 1, 2, 'n'],
61 first={0, 1, 2, 'n'},
62 )
63 function_03(
64 first=None,
65 second=False,
66 third=True,
67 fourth=4,
68 )
69 function_04(argument='value')
70 function_05(
71 argument_0='value1',
72 argument_1=(0, 1, 2, 'n'),
73 )
74 function_06(
75 argument_0='value1',
76 argument_1=(0, 1, 2, 'n'),
77 argument_2=[0, 1, 2, 'n'],
78 )
79 function_07(
80 argument_0=(0, 1, 2, 'n'),
81 argument_1=[0, 1, 2, 'n'],
82 argument_2={0, 1, 2, 'n'},
83 argument_n={'key': 'value'},
84 )
85 function_08(argument='positional', name='keyword')
86
87
88def test_type_error_w_args_and_kwargs():
89 function_00('argument')
90 function_01(1, 0)
91 function_02(third=True, second=False, first=None)
92 function_03(
93 second=[0, 1, 2, 'n'],
94 first=(0, 1, 2, 'n'),
95 third={0, 1, 2, 'n'},
96 fourth={'key': 'value'}
97 )
98 function_04('value')
99 function_05(
100 (0, 1, 2, 'n'),
101 [0, 1, 2, 'n'],
102 )
103 function_06(
104 (0, 1, 2, 'n'),
105 [0, 1, 2, 'n'],
106 argument_2={0, 1, 2, 'n'},
107 )
108 function_07(
109 argument_n={'key': 'value'},
110 argument_2={0, 1, 2, 'n'},
111 argument_0=(0, 1, 2, 'n'),
112 argument_1=[0, 1, 2, 'n'],
113 )
114 function_08('positional', argument='keyword')
115
116
117# Exceptions seen
118# AssertionError
119# NameError
120# 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 use the mv program to change the name of
main.pytotest_type_error.pyand move it to thetestsfoldermv main.py tests/test_type_error.pyMove-Item main.py 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_.pyif you ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
fix those errors and try to run
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 with 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 …
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') 6 7 def function_01(first, second): 8 return None 9 10 function_01('a', 'b') 11 12 def function_02(first, second, third): 13 return None 14 15 function_02('a', 'b', 'c') 16 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 another terminal then add a git commit message
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 …
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 to a function must match its signature (definition).
I add
keywordin 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 arguments27 function_04(argument='value') 28 29 function_05( 30 argument_0='value1', 31 argument_1=(0, 1, 2, 'n'), 32 ) 33 34 35# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_05' is not definedI add a definition for
function_0527 function_04(argument='value') 28 29 def function_05(argument): 30 return None 31 32 function_05( 33 argument_0='value1', 34 argument_1=(0, 1, 2, 'n'), 35 ) 36 37 38# 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='value1'andargument_1=(0, 1, 2, 'n')).The function definition (signature) of
function_05only allows one input with the namekeyword.The call to a function must match its signature (definition).
I change the name of the input to match the call
27 function_04(argument='value') 28 29 # def function_05(argument): 30 def function_05(argument_0): 31 return None 32 33 function_05( 34 argument_0='value1', 35 argument_1=(0, 1, 2, 'n'), 36 ) 37 38 39# Exceptions seenTypeError: test_type_error_w_keyword_arguments .<locals>.function_05() got an unexpected keyword argument 'argument_1'. Did you mean 'argument_0'?I add
argument_0to the parentheses so that the call tofunction_05and its definition match27 function_04(argument='value') 28 29 # def function_05(argument): 30 # def function_05(argument_0): 31 def function_05(argument_0, argument_1): 32 return None 33 34 function_05( 35 argument_0='value1', 36 argument_1=(0, 1, 2, 'n'), 37 ) 38 39 40# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_06with keyword arguments34 function_05( 35 argument_0='value1', 36 argument_1=(0, 1, 2, 'n'), 37 ) 38 39 function_06( 40 argument_0='value1', 41 argument_1=(0, 1, 2, 'n'), 42 argument_2=[0, 1, 2, 'n'], 43 ) 44 45 46# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_06' is not definedI add a definition for
function_0634 function_05( 35 argument_0='value1', 36 argument_1=(0, 1, 2, 'n'), 37 ) 38 39 def function_06(argument_0, argument_1): 40 return None 41 42 function_06( 43 argument_0='value1', 44 argument_1=(0, 1, 2, 'n'), 45 argument_2=[0, 1, 2, 'n'], 46 ) 47 48 49# 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='value1',argument_1=(0, 1, 2, 'n')andargument_2=[0, 1, 2, 'n']).The function definition (signature) of
function_06only allows two inputs.The call to a function must match its signature (definition).
I add
argument_2to the parentheses so that the call tofunction_06and its definition match34 function_05( 35 argument_0='value1', 36 argument_1=(0, 1, 2, 'n'), 37 ) 38 39 # def function_06(argument_0, argument_1): 40 def function_06(argument_0, argument_1, argument_2): 41 return None 42 43 function_06( 44 argument_0='value1', 45 argument_1=(0, 1, 2, 'n'), 46 argument_2=[0, 1, 2, 'n'], 47 ) 48 49 50# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_07with keyword arguments43 function_06( 44 argument_0='value1', 45 argument_1=(0, 1, 2, 'n'), 46 argument_2=[0, 1, 2, 'n'], 47 ) 48 49 function_07( 50 argument_0=(0, 1, 2, 'n'), 51 argument_1=[0, 1, 2, 'n'], 52 argument_2={0, 1, 2, 'n'}, 53 argument_n={'key': 'value'}, 54 ) 55 56 57# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_07' is not definedI add a definition for
function_0743 function_06( 44 argument_0='value1', 45 argument_1=(0, 1, 2, 'n'), 46 argument_2=[0, 1, 2, 'n'], 47 ) 48 49 def function_07( 50 argument_0, argument_1, 51 argument_2 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 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 to a function must match its signature (definition).
I add
argument_nto the parentheses so that the call tofunction_07and its definition match43 function_06( 44 argument_0='value1', 45 argument_1=(0, 1, 2, 'n'), 46 argument_2=[0, 1, 2, 'n'], 47 ) 48 49 def function_07( 50 argument_0, argument_1, 51 # argument_2 52 argument_2, argument_n, 53 ): 54 return None 55 56 function_07( 57 argument_0=(0, 1, 2, 'n'), 58 argument_1=[0, 1, 2, 'n'], 59 argument_2={0, 1, 2, 'n'}, 60 argument_n={'key': 'value'}, 61 ) 62 63 64# 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') 28 29 def function_05(argument_0, argument_1): 30 return None 31 32 function_05( 33 argument_0='value1', 34 argument_1=(0, 1, 2, 'n'), 35 ) 36 37 def function_06(argument_0, argument_1, argument_2): 38 return None 39 40 function_06( 41 argument_0='value1', 42 argument_1=(0, 1, 2, 'n'), 43 argument_2=[0, 1, 2, 'n'], 44 ) 45 46 def function_07( 47 argument_0, argument_1, 48 argument_2, argument_n, 49 ): 50 return None 51 52 function_07( 53 argument_0=(0, 1, 2, 'n'), 54 argument_1=[0, 1, 2, 'n'], 55 argument_2={0, 1, 2, 'n'}, 56 argument_n={'key': 'value'}, 57 ) 58 59 60# 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.
I have to call a function with the same names that are in its definition.
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 the 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 argument51 function_07( 52 argument_0=(0, 1, 2, 'n'), 53 argument_1=[0, 1, 2, 'n'], 54 argument_2={0, 1, 2, 'n'}, 55 argument_n={'key': 'value'}, 56 ) 57 58 59def test_type_error_w_args_and_kwargs(): 60 function_08('positional', argument='keyword') 61 62 63# 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_0860def test_type_error_w_args_and_kwargs(): 61 def function_08(argument): 62 return None 63 64 function_08('positional', argument='keyword') 65 66 67# 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
60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 def function_08(argument, name): 63 return None 64 65 function_08('positional', argument='keyword') 66 67 68# Exceptions seenthe 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 inputs
60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 function_08('positional', argument='keyword') 67 68 69# Exceptions seenthe test passes because the call to the function matches its definition.
REFACTOR: make it better
I add another call to
function_0860def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 function_08('positional', argument='keyword') 67 function_08('positional', name='keyword') 68 69 70# 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 in the call to be clearer
60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 function_08('positional', argument='keyword') 67 # function_08('positional', name='keyword') 68 function_08(argument='positional', name='keyword') 69 70 71# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_01with a positional and keyword argument60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 function_01(1, first=0) 67 function_08('positional', argument='keyword') 68 # function_08('positional', name='keyword') 69 function_08(argument='positional', name='keyword') 70 71 72# Exceptions seenthe 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.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): 13 return None 14 15 function_02('a', 'b', 'c') 16 17 def function_03(first, second, third, fourth): 18 return None 19 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_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 to only use positional arguments to make it clearer
60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 # function_01(1, first=0) 67 function_01(1, 0) 68 function_08('positional', argument='keyword') 69 # function_08('positional', name='keyword') 70 function_08(argument='positional', name='keyword') 71 72 73# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_02with arguments that are not clear60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 # function_01(1, first=0) 67 function_01(1, 0) 68 function_02(False, first=None) 69 function_08('positional', argument='keyword') 70 # function_08('positional', name='keyword') 71 function_08(argument='positional', name='keyword') 72 73 74# Exceptions seenthe 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.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 None 7 8 9def 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): 18 return None 19 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_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 to a keyword argument to make it clearer
60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 # function_01(1, first=0) 67 function_01(1, 0) 68 # function_02(False, first=None) 69 function_02(second=False, first=None) 70 function_08('positional', argument='keyword') 71 # function_08('positional', name='keyword') 72 function_08(argument='positional', name='keyword') 73 74 75# Exceptions seenthe 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
60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 # function_01(1, first=0) 67 function_01(1, 0) 68 # function_02(False, first=None) 69 # function_02(second=False, first=None) 70 function_02(True, second=False, first=None) 71 function_08('positional', argument='keyword') 72 # function_08('positional', name='keyword') 73 function_08(argument='positional', name='keyword') 74 75 76# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: function_02() got multiple values for argument 'first'because …
I change it to a keyword argument to make it clearer
60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 # function_01(1, first=0) 67 function_01(1, 0) 68 # function_02(False, first=None) 69 # function_02(second=False, first=None) 70 # function_02(True, second=False, first=None) 71 function_02(third=True, second=False, first=None) 72 function_08('positional', argument='keyword') 73 # function_08('positional', name='keyword') 74 function_08(argument='positional', name='keyword') 75 76 77# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_0366 # function_01(1, first=0) 67 function_01(1, 0) 68 # function_02(False, first=None) 69 # function_02(second=False, first=None) 70 # function_02(True, second=False, first=None) 71 function_02(third=True, second=False, first=None) 72 function_03( 73 [0, 1, 2, 'n'], 74 first=(0, 1, 2, 'n'), 75 third={0, 1, 2, 'n'}, 76 fourth={'key': 'value'} 77 ) 78 function_08('positional', argument='keyword') 79 # function_08('positional', name='keyword') 80 function_08(argument='positional', name='keyword') 81 82 83# Exceptions seenthe 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.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(): 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 name of the argument in the first position.
I add a keyword argument for the first value in the call from test_type_error_w_args_and_kwargs to make it clearer
66 # function_01(1, first=0) 67 function_01(1, 0) 68 # function_02(False, first=None) 69 # function_02(second=False, first=None) 70 # function_02(True, second=False, first=None) 71 function_02(third=True, second=False, first=None) 72 function_03( 73 # [0, 1, 2, 'n'], 74 second=[0, 1, 2, 'n'], 75 first=(0, 1, 2, 'n'), 76 third={0, 1, 2, 'n'}, 77 fourth={'key': 'value'} 78 ) 79 function_08('positional', argument='keyword') 80 # function_08('positional', name='keyword') 81 function_08(argument='positional', name='keyword') 82 83 84# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_0766 # function_01(1, first=0) 67 function_01(1, 0) 68 # function_02(False, first=None) 69 # function_02(second=False, first=None) 70 # function_02(True, second=False, first=None) 71 function_02(third=True, second=False, first=None) 72 function_03( 73 # [0, 1, 2, 'n'], 74 second=[0, 1, 2, 'n'], 75 first=(0, 1, 2, 'n'), 76 third={0, 1, 2, 'n'}, 77 fourth={'key': 'value'} 78 ) 79 80 function_07( 81 {0, 1, 2, 'n'}, 82 {'key': 'value'}, 83 argument_0=(0, 1, 2, 'n'), 84 argument_1=[0, 1, 2, 'n'], 85 ) 86 function_08('positional', argument='keyword') 87 # function_08('positional', name='keyword') 88 function_08(argument='positional', name='keyword') 89 90 91# Exceptions seenthe 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.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(): 21 def function_00(the_input): 22 return None 23 24 function_00('a') 25 function_01('a', 'b') 26 function_02('a', 'b', 'c') 27 function_03('a', 'b', 'c', 'd') 28 29 30def test_type_error_w_keyword_arguments(): 31 def function_04(argument): 32 return None 33 34 function_04(argument='value') 35 36 def function_05(argument_0, argument_1): 37 return None 38 39 function_05( 40 argument_0='value1', 41 argument_1=(0, 1, 2, 'n'), 42 ) 43 44 def function_06(argument_0, argument_1, argument_2): 45 return None 46 47 function_06( 48 argument_0='value1', 49 argument_1=(0, 1, 2, 'n'), 50 argument_2=[0, 1, 2, 'n'], 51 ) 52 function_07( 53 argument_0=(0, 1, 2, 'n'), 54 argument_1=[0, 1, 2, 'n'], 55 argument_2={0, 1, 2, 'n'}, 56 argument_n={'key': 'value'}, 57 ) 58 59def 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 name of 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?The call to a function must match its signature (definition).
I change the call to use a keyword argument, then change its position
78 function_07( 79 # {0, 1, 2, 'n'}, 80 {'key': 'value'}, 81 argument_2={0, 1, 2, 'n'}, 82 argument_0=(0, 1, 2, 'n'), 83 argument_1=[0, 1, 2, 'n'], 84 ) 85 function_08('positional', argument='keyword') 86 # function_08('positional', name='keyword') 87 function_08(argument='positional', name='keyword') 88 89 90# Exceptions seenthe terminal still shows TypeError because the call uses an argument in the first position and uses a keyword argument with the name of the argument in the first position.
I use a keyword argument to make it clearer
78 function_07( 79 # {0, 1, 2, 'n'}, 80 # {'key': 'value'}, 81 argument_4={'key': 'value'}, 82 argument_2={0, 1, 2, 'n'}, 83 argument_0=(0, 1, 2, 'n'), 84 argument_1=[0, 1, 2, 'n'], 85 ) 86 function_08('positional', argument='keyword') 87 # function_08('positional', name='keyword') 88 function_08(argument='positional', name='keyword') 89 90 91# Exceptions seenthe 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 keyword argument that is not in the function definition (signature).The call to a function must match its signature (definition).
I change the keyword argument to match the function definition
78 function_07( 79 # {0, 1, 2, 'n'}, 80 # {'key': 'value'}, 81 # argument_4={'key': 'value'}, 82 argument_n={'key': 'value'}, 83 argument_2={0, 1, 2, 'n'}, 84 argument_0=(0, 1, 2, 'n'), 85 argument_1=[0, 1, 2, 'n'], 86 ) 87 function_08('positional', argument='keyword') 88 # function_08('positional', name='keyword') 89 function_08(argument='positional', name='keyword') 90 91 92# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_0060def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 function_00() 67 # function_01(1, first=0) 68 function_01(1, 0) 69 # function_02(False, first=None) 70 # function_02(second=False, first=None) 71 # function_02(True, second=False, first=None) 72 function_02(third=True, second=False, first=None) 73 function_03( 74 # [0, 1, 2, 'n'], 75 second=[0, 1, 2, 'n'], 76 first=(0, 1, 2, 'n'), 77 third={0, 1, 2, 'n'}, 78 fourth={'key': 'value'} 79 )the terminal is my friend, and shows NameError
NameError: name 'function_00' is not definedbecause …
I move
function_00out of test_type_error_w_positional_argumentsthe terminal is my friend, and shows TypeError
TypeError: function_00() missing 1 required positional argument: 'the_input'because the function definition requires that it be called with one input and it got called with zero inputs.
I change the call to match the signature
60def test_type_error_w_args_and_kwargs(): 61 # def function_08(argument): 62 # def function_08(argument, name): 63 def function_08(name, argument): 64 return None 65 66 # function_00() 67 function_00('argument') 68 # function_01(1, first=0) 69 function_01(1, 0) 70 # function_02(False, first=None) 71 # function_02(second=False, first=None) 72 # function_02(True, second=False, first=None) 73 function_02(third=True, second=False, first=None) 74 function_03( 75 # [0, 1, 2, 'n'], 76 second=[0, 1, 2, 'n'], 77 first=(0, 1, 2, 'n'), 78 third={0, 1, 2, 'n'}, 79 fourth={'key': 'value'} 80 )the test passes because the call to the function matches its definition.
I add a call to
function_0473 function_02(third=True, second=False, first=None) 74 function_03( 75 # [0, 1, 2, 'n'], 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('value1', 'value2') 82 83 function_07( 84 # {0, 1, 2, 'n'}, 85 # {'key': 'value'}, 86 # argument_4={'key': 'value'}, 87 argument_n={'key': 'value'}, 88 argument_2={0, 1, 2, 'n'}, 89 argument_0=(0, 1, 2, 'n'), 90 argument_1=[0, 1, 2, 'n'], 91 ) 92 function_08('positional', argument='keyword') 93 # function_08('positional', name='keyword') 94 function_08(argument='positional', name='keyword') 95 96 97# 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.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 None 26 27 28def test_type_error_w_positional_arguments(): 29 function_00('a') 30 function_01('a', 'b') 31 function_02('a', 'b', 'c') 32 function_03('a', 'b', 'c', 'd') 33 34 35def test_type_error_w_keyword_arguments(): 36 function_04(argument='value') 37 38 def function_05(argument_0, argument_1): 39 return None 40 41 function_05( 42 argument_0='value1', 43 argument_1=(0, 1, 2, 'n'), 44 ) 45 46 def function_06(argument_0, argument_1, argument_2): 47 return None 48 49 function_06( 50 argument_0='value1', 51 argument_1=(0, 1, 2, 'n'), 52 argument_2=[0, 1, 2, 'n'], 53 ) 54 function_07( 55 argument_0=(0, 1, 2, 'n'), 56 argument_1=[0, 1, 2, 'n'], 57 argument_2={0, 1, 2, 'n'}, 58 argument_n={'key': 'value'}, 59 ) 60 61def test_type_error_w_args_and_kwargs():the terminal is my friend, and shows TypeError
TypeError: function_04() takes 1 positional argument but 2 were givenbecause the definition only allows it take one input, and it got called with two.
I change the call to match the signature
74 function_02(third=True, second=False, first=None) 75 function_03( 76 # [0, 1, 2, 'n'], 77 second=[0, 1, 2, 'n'], 78 first=(0, 1, 2, 'n'), 79 third={0, 1, 2, 'n'}, 80 fourth={'key': 'value'} 81 ) 82 # function_04('value1', 'value2') 83 function_04('value') 84 85 function_07( 86 # {0, 1, 2, 'n'}, 87 # {'key': 'value'}, 88 # argument_4={'key': 'value'}, 89 argument_n={'key': 'value'}, 90 argument_2={0, 1, 2, 'n'}, 91 argument_0=(0, 1, 2, 'n'), 92 argument_1=[0, 1, 2, 'n'], 93 ) 94 function_08('positional', argument='keyword') 95 # function_08('positional', name='keyword') 96 function_08(argument='positional', name='keyword') 97 98 99# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_0574 function_02(third=True, second=False, first=None) 75 function_03( 76 # [0, 1, 2, 'n'], 77 second=[0, 1, 2, 'n'], 78 first=(0, 1, 2, 'n'), 79 third={0, 1, 2, 'n'}, 80 fourth={'key': 'value'} 81 ) 82 # function_04('value1', 'value2') 83 function_04('value') 84 function_05((0, 1, 2, 'n')) 85 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 ) 95 function_08('positional', argument='keyword') 96 # function_08('positional', name='keyword') 97 function_08(argument='positional', name='keyword') 98 99 100# Exceptions seenthe 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 None 30 31 32def test_type_error_w_positional_arguments(): 33 function_00('a') 34 function_01('a', 'b') 35 function_02('a', 'b', 'c') 36 function_03('a', 'b', 'c', 'd') 37 38 39def test_type_error_w_keyword_arguments(): 40 function_04(argument='value') 41 function_05( 42 argument_0='value1', 43 argument_1=(0, 1, 2, 'n'), 44 ) 45 46 def function_06(argument_0, argument_1, argument_2): 47 return None 48 49 function_06( 50 argument_0='value1', 51 argument_1=(0, 1, 2, 'n'), 52 argument_2=[0, 1, 2, 'n'], 53 ) 54 function_07( 55 argument_0=(0, 1, 2, 'n'), 56 argument_1=[0, 1, 2, 'n'], 57 argument_2={0, 1, 2, 'n'}, 58 argument_n={'key': 'value'}, 59 ) 60 61def test_type_error_w_args_and_kwargs():the 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 to match the signature
82 # function_04('value1', 'value2') 83 function_04('value') 84 # function_05((0, 1, 2, 'n')) 85 function_05( 86 (0, 1, 2, 'n'), 87 [0, 1, 2, 'n'], 88 ) 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 function_08('positional', argument='keyword') 100 # function_08('positional', name='keyword') 101 function_08(argument='positional', name='keyword') 102 103 104# Exceptions seenthe test passes because the call to the function matches its definition.
I add a call to
function_0682 # function_04('value1', 'value2') 83 function_04('value') 84 # function_05((0, 1, 2, 'n')) 85 function_05( 86 (0, 1, 2, 'n'), 87 [0, 1, 2, 'n'], 88 ) 89 function_06( 90 (0, 1, 2, 'n'), 91 [0, 1, 2, 'n'], 92 argument_2={0, 1, 2, 'n'}, 93 argument_n={'key': 'value'}, 94 ) 95 96 function_07( 97 # {0, 1, 2, 'n'}, 98 # {'key': 'value'}, 99 # argument_4={'key': 'value'}, 100 argument_n={'key': 'value'}, 101 argument_2={0, 1, 2, 'n'}, 102 argument_0=(0, 1, 2, 'n'), 103 argument_1=[0, 1, 2, 'n'], 104 ) 105 function_08('positional', argument='keyword') 106 # function_08('positional', name='keyword') 107 function_08(argument='positional', name='keyword') 108 109 110# Exceptions seenthe 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 and I cannot reach it from outside.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(argument_0, argument_1, argument_2): 26 return None 27 28 29def function_07( 30 argument_0, argument_1, 31 argument_2, argument_n, 32): 33 return None 34 35 36def test_type_error_w_positional_arguments(): 37 function_00('a') 38 function_01('a', 'b') 39 function_02('a', 'b', 'c') 40 function_03('a', 'b', 'c', 'd') 41 42 43def test_type_error_w_keyword_arguments(): 44 function_04(argument='value') 45 function_05( 46 argument_0='value1', 47 argument_1=(0, 1, 2, 'n'), 48 ) 49 function_06( 50 argument_0='value1', 51 argument_1=(0, 1, 2, 'n'), 52 argument_2=[0, 1, 2, 'n'], 53 ) 54 function_07( 55 argument_0=(0, 1, 2, 'n'), 56 argument_1=[0, 1, 2, 'n'], 57 argument_2={0, 1, 2, 'n'}, 58 argument_n={'key': 'value'}, 59 ) 60 61def 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) that is not in its definition
I remove
argument_n={'key': 'value'}from the call82 # function_04('value1', 'value2') 83 function_04('value') 84 # function_05((0, 1, 2, 'n')) 85 function_05( 86 (0, 1, 2, 'n'), 87 [0, 1, 2, 'n'], 88 ) 89 function_06( 90 (0, 1, 2, 'n'), 91 [0, 1, 2, 'n'], 92 argument_2={0, 1, 2, 'n'}, 93 # argument_n={'key': 'value'}, 94 ) 95 96 function_07( 97 # {0, 1, 2, 'n'}, 98 # {'key': 'value'}, 99 # argument_4={'key': 'value'}, 100 argument_n={'key': 'value'}, 101 argument_2={0, 1, 2, 'n'}, 102 argument_0=(0, 1, 2, 'n'), 103 argument_1=[0, 1, 2, 'n'], 104 ) 105 function_08('positional', argument='keyword') 106 # function_08('positional', name='keyword') 107 function_08(argument='positional', name='keyword') 108 109 110# Exceptions seenthe test passes because the call to the function matches its definition.
I remove the commented lines from test_type_error_w_args_and_kwargs
57def test_type_error_w_args_and_kwargs(): 58 def function_08(name, argument): 59 return None 60 61 function_00('argument') 62 function_01(1, 0) 63 function_02(third=True, second=False, first=None) 64 function_03( 65 second=[0, 1, 2, 'n'], 66 first=(0, 1, 2, 'n'), 67 third={0, 1, 2, 'n'}, 68 fourth={'key': 'value'} 69 ) 70 function_04('value') 71 function_05( 72 (0, 1, 2, 'n'), 73 [0, 1, 2, 'n'], 74 ) 75 function_06( 76 (0, 1, 2, 'n'), 77 [0, 1, 2, 'n'], 78 argument_2={0, 1, 2, 'n'}, 79 ) 80 function_07( 81 argument_n={'key': 'value'}, 82 argument_2={0, 1, 2, 'n'}, 83 argument_0=(0, 1, 2, 'n'), 84 argument_1=[0, 1, 2, 'n'], 85 ) 86 function_08('positional', argument='keyword') 87 function_08(argument='positional', name='keyword') 88 89 90# Exceptions seenI add a git commit message in the other terminal
git commit --all --message \ 'add test_type_error_w_args_and_kwargs'
I add a call to
function_03from test_type_error_w_keyword_arguments43def test_type_error_w_keyword_arguments(): 44 function_03( 45 first=None, 46 second=False, 47 third=True, 48 ) 49 function_04(argument='value') 50 function_05( 51 argument_0='value1', 52 argument_1=(0, 1, 2, 'n'), 53 ) 54 function_06( 55 argument_0='value1', 56 argument_1=(0, 1, 2, 'n'), 57 argument_2=[0, 1, 2, 'n'], 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 66def test_type_error_w_args_and_kwargs():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.
I add
fourthwith a value to the call43def test_type_error_w_keyword_arguments(): 44 function_03( 45 first=None, 46 second=False, 47 third=True, 48 fourth=4, 49 ) 50 function_04(argument='value')the test passes because the call to the function matches its definition.
I add a call to
function_02from test_type_error_w_keyword_arguments43def test_type_error_w_keyword_arguments(): 44 function_02( 45 fourth=4.0, 46 third=(0, 1, 2, 'n'), 47 second=[0, 1, 2, 'n'], 48 first={0, 1, 2, 'n'}, 49 ) 50 function_03( 51 first=None, 52 second=False, 53 third=True, 54 fourth=4, 55 )the terminal is my friend, and shows TypeError
TypeError: function_02() got an unexpected keyword argument 'fourth'because the signature only allows calls with three inputs, and it got called with four.
I remove the unexpected keyword argument from the call
43def test_type_error_w_keyword_arguments(): 44 function_02( 45 # fourth=4.0, 46 third=(0, 1, 2, 'n'), 47 second=[0, 1, 2, 'n'], 48 first={0, 1, 2, 'n'}, 49 ) 50 function_03( 51 first=None, 52 second=False, 53 third=True, 54 fourth=4, 55 )the test passes because the call to the function matches its definition.
I add a call to
function_01from test_type_error_w_keyword_arguments43def test_type_error_w_keyword_arguments(): 44 function_01( 45 second={'key': 'value'}, 46 ) 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 )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
43def test_type_error_w_keyword_arguments(): 44 function_01( 45 first='first', 46 second={'key': 'value'}, 47 )the test passes because the call to the function matches its definition.
I add a call to
function_00from test_type_error_w_keyword_arguments43def test_type_error_w_keyword_arguments(): 44 function_00(second=1, first=0) 45 function_01( 46 first='first', 47 second={'key': 'value'}, 48 )the terminal is my friend, and shows TypeError
TypeError: function_00() got an unexpected keyword argument 'second'I remove
secondfrom the call43def test_type_error_w_keyword_arguments(): 44 # function_00(second=1, first=0) 45 function_00(first=0) 46 function_01( 47 first='first', 48 second={'key': 'value'}, 49 )the terminal is my friend, and shows TypeError
TypeError: function_00() got an unexpected keyword argument 'first'I remove
firstfrom the call43def test_type_error_w_keyword_arguments(): 44 # function_00(second=1, first=0) 45 # function_00(first=0) 46 function_00() 47 function_01( 48 first='first', 49 second={'key': 'value'}, 50 )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 the call to the function matches its definition.
I move the call to
function_08that uses keyword arguments from test_type_error_w_args_and_kwargs to test_type_error_w_keyword_arguments74 function_07( 75 argument_0=(0, 1, 2, 'n'), 76 argument_1=[0, 1, 2, 'n'], 77 argument_2={0, 1, 2, 'n'}, 78 argument_n={'key': 'value'}, 79 ) 80 function_08(argument='positional', name='keyword') 81 82 83def test_type_error_w_args_and_kwargs(): 84 def function_08(name, argument): 85 return None 86 87 function_00('argument') 88 function_01(1, 0) 89 function_02(third=True, second=False, first=None) 90 function_03( 91 second=[0, 1, 2, 'n'], 92 first=(0, 1, 2, 'n'), 93 third={0, 1, 2, 'n'}, 94 fourth={'key': 'value'} 95 ) 96 function_04('value') 97 function_05( 98 (0, 1, 2, 'n'), 99 [0, 1, 2, 'n'], 100 ) 101 function_06( 102 (0, 1, 2, 'n'), 103 [0, 1, 2, 'n'], 104 argument_2={0, 1, 2, 'n'}, 105 ) 106 function_07( 107 argument_n={'key': 'value'}, 108 argument_2={0, 1, 2, 'n'}, 109 argument_0=(0, 1, 2, 'n'), 110 argument_1=[0, 1, 2, 'n'], 111 ) 112 function_08('positional', argument='keyword') 113 114 115# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'function_08' is not definedI move
function_08out of test_type_error_w_args_and_kwargs29def function_07( 30 argument_0, argument_1, 31 argument_2, argument_n, 32): 33 return None 34 35 36def function_08(name, argument): 37 return None 38 39 40def test_type_error_w_positional_arguments():87def test_type_error_w_args_and_kwargs(): 88 function_00('argument') 89 function_01(1, 0) 90 function_02(third=True, second=False, first=None) 91 function_03( 92 second=[0, 1, 2, 'n'], 93 first=(0, 1, 2, 'n'), 94 third={0, 1, 2, 'n'}, 95 fourth={'key': 'value'} 96 ) 97 function_04('value') 98 function_05( 99 (0, 1, 2, 'n'), 100 [0, 1, 2, 'n'], 101 ) 102 function_06( 103 (0, 1, 2, 'n'), 104 [0, 1, 2, 'n'], 105 argument_2={0, 1, 2, 'n'}, 106 ) 107 function_07( 108 argument_n={'key': 'value'}, 109 argument_2={0, 1, 2, 'n'}, 110 argument_0=(0, 1, 2, 'n'), 111 argument_1=[0, 1, 2, 'n'], 112 ) 113 function_08('positional', argument='keyword') 114 115 116# Exceptions seenthe test is green again.
I remove the commented lines from test_type_error_w_keyword_arguments
47def test_type_error_w_keyword_arguments(): 48 function_00(the_input=0) 49 function_01( 50 first='first', 51 second={'key': 'value'}, 52 ) 53 function_02( 54 third=(0, 1, 2, 'n'), 55 second=[0, 1, 2, 'n'], 56 first={0, 1, 2, 'n'}, 57 ) 58 function_03( 59 first=None, 60 second=False, 61 third=True, 62 fourth=4, 63 ) 64 function_04(argument='value') 65 function_05( 66 argument_0='value1', 67 argument_1=(0, 1, 2, 'n'), 68 ) 69 function_06( 70 argument_0='value1', 71 argument_1=(0, 1, 2, 'n'), 72 argument_2=[0, 1, 2, 'n'], 73 ) 74 function_07( 75 argument_0=(0, 1, 2, 'n'), 76 argument_1=[0, 1, 2, 'n'], 77 argument_2={0, 1, 2, 'n'}, 78 argument_n={'key': 'value'}, 79 ) 80 function_08(argument='positional', name='keyword') 81 82 83def test_type_error_w_args_and_kwargs():I add a call to
function_04from test_type_error_w_positional_arguments40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 function_04() 46 47 48def 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
40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 48 49def 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_arguments40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 function_05('a', 'b', 'c') 48 49 50def 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
40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 # function_05('a', 'b', 'c') 48 function_05('a', 'b') 49 50 51def 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_arguments40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 # function_05('a', 'b', 'c') 48 function_05('a', 'b') 49 function_06('a', 'b') 50 51 52def 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
40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 # function_05('a', 'b', 'c') 48 function_05('a', 'b') 49 # function_06('a', 'b') 50 function_06('a', 'b', 'c') 51 52 53def 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_arguments40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 # function_05('a', 'b', 'c') 48 function_05('a', 'b') 49 # function_06('a', 'b') 50 function_06('a', 'b', 'c') 51 function_07('a', 'b', 'c', 'd', 'e') 52 53 54def 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
40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 # function_05('a', 'b', 'c') 48 function_05('a', 'b') 49 # function_06('a', 'b') 50 function_06('a', 'b', 'c') 51 # function_07('a', 'b', 'c', 'd', 'e') 52 function_07('a', 'b', 'c', 'd') 53 54 55def 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_arguments40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 # function_05('a', 'b', 'c') 48 function_05('a', 'b') 49 # function_06('a', 'b') 50 function_06('a', 'b', 'c') 51 # function_07('a', 'b', 'c', 'd', 'e') 52 function_07('a', 'b', 'c', 'd') 53 function_08('last') 54 55 56def 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
40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 # function_04() 46 function_04('a') 47 # function_05('a', 'b', 'c') 48 function_05('a', 'b') 49 # function_06('a', 'b') 50 function_06('a', 'b', 'c') 51 # function_07('a', 'b', 'c', 'd', 'e') 52 function_07('a', 'b', 'c', 'd') 53 # function_08('last') 54 function_08('last', 'one') 55 56 57def 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
40def test_type_error_w_positional_arguments(): 41 function_00('a') 42 function_01('a', 'b') 43 function_02('a', 'b', 'c') 44 function_03('a', 'b', 'c', 'd') 45 function_04('a') 46 function_05('a', 'b') 47 function_06('a', 'b', 'c') 48 function_07('a', 'b', 'c', 'd') 49 function_08('last', 'one') 50 51 52def 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.
review
I ran to test for TypeError based on what I have seen so far 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?
so far you know
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.