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_error

  • I open a terminal

  • I change directory to the type_error folder in the pumping_python folder

    cd type_error
    

    the terminal shows

    cd: no such file or directory: type_error
    

    there is no folder with the name type_error in this folder.

  • I use uv to make a directory for the project and initialize it

    uv init type_error
    

    the 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_error
    

    the terminal shows I am in the type_error folder

    .../pumping_python/type_error
    
  • I make a directory for the tests

    mkdir tests
    

    the terminal goes back to the command line.

  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    
    New-Item tests/__init__.py
    

    the terminal goes back to the command line.

  • I use the mv program to change the name of main.py to test_type_error.py and move it to the tests folder

    mv main.py tests/test_type_error.py
    
    Move-Item main.py tests/test_type_error.py
    

    the terminal goes back to the command line.

  • I open test_type_error.py

  • I delete the text in the file then add the first failing test to test_type_error.py

    1def test_failure():
    2    assert False is True
    
  • I go back to the terminal to make a requirements file for the Python packages I need

    echo "pytest" > requirements.txt
    

    the terminal goes back to the command line.

  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line.

  • I use uv to install pytest-watcher with the requirements file

    uv add --requirement requirements.txt
    

    the 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.lock
    

    then goes back to the command line.

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the 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 =====================
    

    because True is NOT False

    if the terminal does not show the same error, then check

    • if your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • if you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    fix those errors and try to run uv run pytest-watcher . --now again

  • I add AssertionError to the list of Exceptions seen in test_type_error.py

    1def test_failure():
    2    assert False is True
    3
    4
    5# Exceptions seen
    6# AssertionError
    
  • I 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# AssertionError
    

    the 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_failure to test_type_error_w_positional_arguments

    1def test_type_error_w_positional_arguments():
    2    function_00('a')
    3
    4
    5# Exceptions seen
    6# AssertionError
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_00' is not defined
    

    because there is nothing named function_00 in test_type_error.py.

  • I add NameError to the list of Exceptions seen

    5# Exceptions seen
    6# AssertionError
    7# NameError
    

GREEN: make it pass



REFACTOR: make it better


  • I add a call to function_01

     6    function_00('a')
     7
     8    function_01('a', 'b')
     9
    10
    11# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_01' is not defined
    

    because …

  • I add a definition for function_01

     6    function_00('a')
     7
     8    def function_01(the_input):
     9        return None
    10
    11    function_01('a', 'b')
    12
    13
    14# Exceptions seen
    

    the terminal is my friend, and shows TypeError

    TypeError:
        test_type_error_w_positional_arguments
            .<locals>.function_01()
        takes 1 positional argument but 2 were given
    

    because

  • 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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_02

    12    function_01('a', 'b')
    13
    14    function_02('a', 'b', 'c')
    15
    16
    17# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_02' is not defined
    

    because there is nothing named function_02 in test_type_error.py.

  • I add a definition for function_02

    12    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 seen
    

    the terminal is my friend, and shows TypeError

    TypeError:
        test_type_error_w_positional_arguments
            .<locals>.function_02()
        takes 2 positional arguments but 3 were given
    

    because

  • I add a third name in parentheses so that the call to function_02 and its definition match

    12    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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_03

    18    function_02('a', 'b', 'c')
    19
    20    function_03('a', 'b', 'c', 'd')
    21
    22
    23# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_03' is not defined
    

    because there is nothing named function_03 in test_type_error.py.

  • I add a definition for function_03

    18    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 seen
    

    the terminal is my friend, and shows TypeError

    TypeError:
        test_type_error_w_positional_arguments
            .<locals>.function_03()
        takes 3 positional arguments but 4 were given
    

    because

  • I add a fourth name in parentheses so that the call to function_03 and its definition match

    18    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 seen
    

    the 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 seen
    
  • I 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_04 with a keyword argument

    20    function_03('a', 'b', 'c', 'd')
    21
    22
    23def test_type_error_w_keyword_arguments():
    24    function_04(argument='value')
    25
    26
    27# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_04' is not defined
    

    because …


GREEN: make it pass



REFACTOR: make it better


  • I add a call to function_05 with keyword arguments

    27    function_04(argument='value')
    28
    29    function_05(
    30        argument_0='value1',
    31        argument_1=(0, 1, 2, 'n'),
    32    )
    33
    34
    35# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_05' is not defined
    
  • I add a definition for function_05

    27    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 seen
    

    the 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

  • 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 seen
    

    the terminal shows TypeError

    TypeError:
        test_type_error_w_keyword_arguments
            .<locals>.function_05()
        got an unexpected keyword argument 'argument_1'.
        Did you mean 'argument_0'?
    
  • I add argument_0 to the parentheses so that the call to function_05 and its definition match

    27    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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_06 with keyword arguments

    34    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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_06' is not defined
    
  • I add a definition for function_06

    34    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 seen
    

    the 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

  • I add argument_2 to the parentheses so that the call to function_06 and its definition match

    34    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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_07 with keyword arguments

    43    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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_07' is not defined
    
  • I add a definition for function_07

    43    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 seen
    

    the 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

  • I add argument_n to the parentheses so that the call to function_07 and its definition match

    43    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 seen
    

    the 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 seen
    
  • I 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_08 with a positional and keyword argument

    51    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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_08' is not defined
    

    because there is no definition for function_08 in the file.


GREEN: make it pass


  • I add a function definition for function_08

    60def 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 seen
    

    the 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

  • 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 seen
    

    the terminal still shows TypeError because the call gives 'positional' as the value for the first argument which is argument in the definition, and it gives 'keyword' as the value for argument as 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 seen
    

    the test passes because the call to the function matches its definition.


REFACTOR: make it better


  • I add another call to function_08

    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
    69
    70# Exceptions seen
    

    the 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

  • 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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_01 with a positional and keyword argument

    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_08('positional', argument='keyword')
    68    # function_08('positional', name='keyword')
    69    function_08(argument='positional', name='keyword')
    70
    71
    72# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_01' is not defined
    

    because function_01 belongs to the test_type_error_w_positional_arguments function and I cannot reach it from outside.

  • I move function_01 out of test_type_error_w_positional_arguments so that it can be called from anywhere in the file

     1def 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_01 from test_type_error_w_args_and_kwargs uses 1 as the value for the first argument which is first in the definition, and uses 0 as the value for first as 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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_02 with arguments that are not clear

    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_08('positional', argument='keyword')
    70    # function_08('positional', name='keyword')
    71    function_08(argument='positional', name='keyword')
    72
    73
    74# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_02' is not defined
    

    because function_02 belongs to the test_type_error_w_positional_arguments function and I cannot reach it from outside.

  • I move function_02 out of test_type_error_w_positional_arguments so that it can be called from anywhere in the file

     1def 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_02 from test_type_error_w_args_and_kwargs uses False as the first positional argument which is named first in the definition, and uses None as the value for first as 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 seen
    

    the terminal is my friend, and shows TypeError

    TypeError: function_02() missing
               1 required positional argument: 'third'
    

    because

  • 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 seen
    

    the 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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_03

    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        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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_03' is not defined
    

    because function_03 belongs to the test_type_error_w_positional_arguments function and I cannot reach it from outside.

  • I move function_03 out of test_type_error_w_positional_arguments

     5def 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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_07

    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
    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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_07' is not defined
    

    because function_07 belongs to the test_type_error_w_keyword_arguments function and I cannot reach it from outside.

  • I move function_07 out of test_type_error_w_keyword_arguments

     9def 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

  • 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 seen
    

    the 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 seen
    

    the terminal is my friend, and shows TypeError

    TypeError: function_07() got
               an unexpected keyword argument 'argument_4'.
               Did you mean 'argument_0'?
    

    because

  • 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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_00

    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_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 defined
    

    because …

  • I move function_00 out of test_type_error_w_positional_arguments

    the 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_04

    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    )
    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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_04' is not defined
    

    because function_04 belongs to the test_type_error_w_keyword_arguments function and I cannot reach it from outside.

  • I move function_04 out of test_type_error_w_keyword_arguments

    13def 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 given
    

    because 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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_05

     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    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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_05' is not defined
    
  • I move function_05 out of test_type_error_w_keyword_arguments

    17def 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 seen
    

    the test passes because the call to the function matches its definition.

  • I add a call to function_06

     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    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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_06' is not defined
    

    because function_06 belongs to the test_type_error_w_keyword_arguments function and I cannot reach it from outside.

  • I move function_06 out of test_type_error_w_keyword_arguments

    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 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 call

     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    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 seen
    

    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

    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 seen
    
  • I 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_03 from test_type_error_w_keyword_arguments

    43def 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 fourth with a value to the call

    43def 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_02 from test_type_error_w_keyword_arguments

    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 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_01 from test_type_error_w_keyword_arguments

    43def 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_00 from test_type_error_w_keyword_arguments

    43def 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 second from the call

    43def 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 first from the call

    43def 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'
    
  • I use the name in the call

    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_08 that uses keyword arguments from test_type_error_w_args_and_kwargs to test_type_error_w_keyword_arguments

     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():
     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 seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_08' is not defined
    
  • I move function_08 out of test_type_error_w_args_and_kwargs

    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():
    
     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 seen
    

    the 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_04 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()
    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_05 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()
    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 given
    
  • I 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_06 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()
    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_07 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()
    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 given
    
  • I 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_08 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()
    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

Do you want to see all the CODE I typed in this 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.