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

type_error/tests/test_type_error.py
  1def function_00(the_input):
  2    return None
  3
  4
  5def function_01(first, second):
  6    return None
  7
  8
  9def function_02(first, second, third):
 10    return None
 11
 12
 13def function_03(first, second, third, fourth):
 14    return None
 15
 16
 17def function_04(argument):
 18    return None
 19
 20
 21def function_05(argument_0, argument_1):
 22    return None
 23
 24
 25def function_06(
 26    argument_0, argument_1,
 27    argument_2,
 28):
 29    return None
 30
 31
 32def function_07(
 33    argument_0, argument_1,
 34    argument_2, argument_n
 35):
 36    return None
 37
 38
 39def function_08(name, argument):
 40    return None
 41
 42
 43def test_type_error_w_positional_arguments():
 44    function_00('a')
 45    function_01('a', 'b')
 46    function_02('a', 'b', 'c')
 47    function_03('a', 'b', 'c', 'd')
 48    function_04('a')
 49    function_05('a', 'b')
 50    function_06('a', 'b', 'c')
 51    function_07('a', 'b', 'c', 'd')
 52    function_08('last', 'one')
 53
 54
 55def test_type_error_w_keyword_arguments():
 56    function_00(the_input=0)
 57    function_01(
 58        first='first',
 59        second={'key': 'value'},
 60    )
 61    function_02(
 62        third=(0, 1, 2, 'n'),
 63        second=[0, 1, 2, 'n'],
 64        first={0, 1, 2, 'n'},
 65    )
 66    function_03(
 67        first=None,
 68        second=False,
 69        third=True,
 70        fourth=4,
 71    )
 72    function_04(argument='value')
 73    function_05(
 74        argument_0='value_1',
 75        argument_1=(0, 1, 2, 'n'),
 76    )
 77    function_06(
 78        argument_0='value_1',
 79        argument_1=(0, 1, 2, 'n'),
 80        argument_2=[0, 1, 2, 'n'],
 81    )
 82    function_07(
 83        argument_0=(0, 1, 2, 'n'),
 84        argument_1=[0, 1, 2, 'n'],
 85        argument_2={0, 1, 2, 'n'},
 86        argument_n={'key': 'value'},
 87    )
 88    function_08(
 89        argument='positional',
 90        name='keyword',
 91    )
 92
 93
 94def test_type_error_w_args_and_kwargs():
 95    function_00('argument')
 96    function_01(1, 0)
 97    function_02(
 98        third=True, second=False,
 99        first=None,
100    )
101    function_03(
102        second=[0, 1, 2, 'n'],
103        first=(0, 1, 2, 'n'),
104        third={0, 1, 2, 'n'},
105        fourth={'key': 'value'},
106    )
107    function_04('value')
108    function_05(
109        (0, 1, 2, 'n'),
110        [0, 1, 2, 'n'],
111    )
112    function_06(
113        (0, 1, 2, 'n'),
114        [0, 1, 2, 'n'],
115        argument_2={0, 1, 2, 'n'},
116    )
117    function_07(
118        argument_n={'key': 'value'},
119        argument_2={0, 1, 2, 'n'},
120        argument_0=(0, 1, 2, 'n'),
121        argument_1=[0, 1, 2, 'n'],
122    )
123    function_08(
124        'positional',
125        argument='keyword',
126    )
127
128
129# Exceptions seen
130# AssertionError
131# NameError
132# TypeError

questions about TypeError

Questions to think about as I go through the chapter


start the project

  • I name this project type_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 make an empty file for the tests in the tests folder

    touch tests/test_type_error.py
    
    New-Item 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 False is NOT True.

    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

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

    and try 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 there is nothing named function_01 in test_type_error.py.

  • 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')
    
     7    def function_01(first, second):
     8        return None
     9
    10    function_01('a', 'b')
    
    12    def function_02(first, second, third):
    13        return None
    14
    15    function_02('a', 'b', 'c')
    
    17    def function_03(first, second, third, fourth):
    18        return None
    19
    20    function_03('a', 'b', 'c', 'd')
    21
    22
    23# Exceptions seen
    
  • I open a new terminal then change directories to type_error

    cd type_error
    
  • I add a git commit message in the new terminal

    git commit --all --message \
    'add test_type_error_w_positional_arguments'
    

    the terminal shows a summary of the changes then goes back to the command line.

I have to call a function with the same number of inputs that are in its definition.


test_type_error_w_keyword_arguments

TypeError happens when I call a function in a way that is different from its signature. I have to match the names of arguments in the function definition when I call the function with keyword arguments.


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with a call to function_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 there is nothing named function_04 in test_type_error.py.


GREEN: make it pass



REFACTOR: make it better


  • I add a call to function_05 with keyword arguments

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

    the terminal is my friend, and shows NameError

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

    28    function_04(argument='value')
    29
    30    def function_05(argument):
    31        return None
    32
    33    function_05(
    34        argument_0='value_1',
    35        argument_1=(0, 1, 2, 'n'),
    36    )
    37
    38
    39# Exceptions 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 in the definition to match the call

    28    function_04(argument='value')
    29
    30    # def function_05(argument):
    31    def function_05(argument_0):
    32        return None
    33
    34    function_05(
    35        argument_0='value_1',
    36        argument_1=(0, 1, 2, 'n'),
    37    )
    38
    39
    40# Exceptions 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'?
    

    because the call uses a name (argument_1) that is not in the parentheses of its definition.

  • I add argument_1 to the parentheses so that the call to function_05 and its definition match

    28    function_04(argument='value')
    29
    30    # def function_05(argument):
    31    # def function_05(argument_0):
    32    def function_05(argument_0, argument_1):
    33        return None
    34
    35    function_05(
    36        argument_0='value_1',
    37        argument_1=(0, 1, 2, 'n'),
    38    )
    39
    40
    41# Exceptions seen
    

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

  • I add a call to function_06 with keyword arguments

    35    function_05(
    36        argument_0='value_1',
    37        argument_1=(0, 1, 2, 'n'),
    38    )
    39
    40    function_06(
    41        argument_0='value_1',
    42        argument_1=(0, 1, 2, 'n'),
    43        argument_2=[0, 1, 2, 'n'],
    44    )
    45
    46
    47# Exceptions seen
    

    the terminal is my friend, and shows NameError

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

    35    function_05(
    36        argument_0='value_1',
    37        argument_1=(0, 1, 2, 'n'),
    38    )
    39
    40    def function_06(argument_0, argument_1):
    41        return None
    42
    43    function_06(
    44        argument_0='value_1',
    45        argument_1=(0, 1, 2, 'n'),
    46        argument_2=[0, 1, 2, 'n'],
    47    )
    48
    49
    50# Exceptions 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

    35    function_05(
    36        argument_0='value_1',
    37        argument_1=(0, 1, 2, 'n'),
    38    )
    39
    40    # def function_06(argument_0, argument_1):
    41    def function_06(
    42        argument_0, argument_1,
    43        argument_2,
    44    ):
    45        return None
    46
    47    function_06(
    48        argument_0='value_1',
    49        argument_1=(0, 1, 2, 'n'),
    50        argument_2=[0, 1, 2, 'n'],
    51    )
    52
    53
    54# Exceptions seen
    

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

  • I add a call to function_07 with keyword arguments

    47    function_06(
    48        argument_0='value_1',
    49        argument_1=(0, 1, 2, 'n'),
    50        argument_2=[0, 1, 2, 'n'],
    51    )
    52
    53    function_07(
    54        argument_0=(0, 1, 2, 'n'),
    55        argument_1=[0, 1, 2, 'n'],
    56        argument_2={0, 1, 2, 'n'},
    57        argument_n={'key': 'value'},
    58    )
    59
    60
    61# Exceptions seen
    

    the terminal is my friend, and shows NameError

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

    47    function_06(
    48        argument_0='value_1',
    49        argument_1=(0, 1, 2, 'n'),
    50        argument_2=[0, 1, 2, 'n'],
    51    )
    52
    53    def function_07(
    54        argument_0, argument_1,
    55        argument_2,
    56    ):
    57        return None
    58
    59    function_07(
    60        argument_0=(0, 1, 2, 'n'),
    61        argument_1=[0, 1, 2, 'n'],
    62        argument_2={0, 1, 2, 'n'},
    63        argument_n={'key': 'value'},
    64    )
    65
    66
    67# Exceptions 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

    47    function_06(
    48        argument_0='value_1',
    49        argument_1=(0, 1, 2, 'n'),
    50        argument_2=[0, 1, 2, 'n'],
    51    )
    52
    53    def function_07(
    54        argument_0, argument_1,
    55        # argument_2
    56        argument_2, argument_n,
    57    ):
    58        return None
    59
    60    function_07(
    61        argument_0=(0, 1, 2, 'n'),
    62        argument_1=[0, 1, 2, 'n'],
    63        argument_2={0, 1, 2, 'n'},
    64        argument_n={'key': 'value'},
    65    )
    66
    67
    68# Exceptions 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')
    
    32    def function_05(argument_0, argument_1):
    33        return None
    34
    35    function_05(
    36        argument_0='value_1',
    37        argument_1=(0, 1, 2, 'n'),
    38    )
    
    37    def function_06(
    38        argument_0, argument_1,
    39        argument_2,
    40    ):
    41        return None
    42
    43    function_06(
    44        argument_0='value_1',
    45        argument_1=(0, 1, 2, 'n'),
    46        argument_2=[0, 1, 2, 'n'],
    47    )
    
    49    def function_07(
    50        argument_0, argument_1,
    51        argument_2, argument_n
    52    ):
    53        return None
    54
    55    function_07(
    56        argument_0=(0, 1, 2, 'n'),
    57        argument_1=[0, 1, 2, 'n'],
    58        argument_2={0, 1, 2, 'n'},
    59        argument_n={'key': 'value'},
    60    )
    61
    62
    63# Exceptions 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 if I call it with keyword arguments.


test_type_error_w_args_and_kwargs

TypeError happens when I call a function in a way that is different from its signature. I cannot give two values for the same argument in the function definition when I call a function.


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with a call to function_08 with a positional and keyword argument

    55    function_07(
    56        argument_0=(0, 1, 2, 'n'),
    57        argument_1=[0, 1, 2, 'n'],
    58        argument_2={0, 1, 2, 'n'},
    59        argument_n={'key': 'value'},
    60    )
    61
    62
    63def test_type_error_w_args_and_kwargs():
    64    function_08(
    65        'positional',
    66        argument='keyword',
    67    )
    68
    69
    70# 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



REFACTOR: make it better


  • I add another call to function_08

    69    function_08(
    70        'positional',
    71        argument='keyword',
    72    )
    73    function_08(
    74        'positional',
    75        name='keyword',
    76    )
    77
    78
    79# 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 to make the call clearer

    69    function_08(
    70        'positional',
    71        argument='keyword',
    72    )
    73    function_08(
    74        # 'positional',
    75        argument='positional',
    76        name='keyword',
    77    )
    78
    79
    80# Exceptions 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

    63def test_type_error_w_args_and_kwargs():
    64    # def function_08(argument):
    65    # def function_08(argument, name):
    66    def function_08(name, argument):
    67        return None
    68
    69    function_01(1, first=0)
    70
    71    function_08(
    72        'positional',
    73        argument='keyword',
    74    )
    

    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 test_type_error_w_positional_arguments.

  • 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):
    

    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 from test_type_error_w_args_and_kwargs to only use positional arguments to make it clearer

    63def test_type_error_w_args_and_kwargs():
    64    # def function_08(argument):
    65    # def function_08(argument, name):
    66    def function_08(name, argument):
    67        return None
    68
    69    # function_01(1, first=0)
    70    function_01(1, 0)
    71
    72    function_08(
    73        'positional',
    74        argument='keyword',
    75    )
    

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

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

    69    # function_01(1, first=0)
    70    function_01(1, 0)
    71    function_02(False, first=None)
    72
    73    function_08(
    74        'positional',
    75        argument='keyword',
    76    )
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_02' is not defined
    

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

  • I move function_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
    
     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):
    

    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 from test_type_error_w_args_and_kwargs to a keyword argument to make it clearer

    63def test_type_error_w_args_and_kwargs():
    64    # def function_08(argument):
    65    # def function_08(argument, name):
    66    def function_08(name, argument):
    67        return None
    68
    69    # function_01(1, first=0)
    70    function_01(1, 0)
    71    # function_02(False, first=None)
    72    function_02(second=False, first=None)
    73
    74    function_08(
    75        'positional',
    76        argument='keyword',
    77    )
    

    the terminal is my friend, and shows TypeError

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

    because

  • I add a third argument to the call

    69    # function_01(1, first=0)
    70    function_01(1, 0)
    71    # function_02(False, first=None)
    72    # function_02(second=False, first=None)
    73    function_02(True, second=False, first=None)
    74
    75    function_08(
    76        'positional',
    77        argument='keyword',
    78    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_02() got
               multiple values for argument 'first'
    

    because the call to function_02 from test_type_error_w_args_and_kwargs uses True 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 call from test_type_error_w_args_and_kwargs to use a keyword argument for True, to make it clearer

    69    # function_01(1, first=0)
    70    function_01(1, 0)
    71    # function_02(False, first=None)
    72    # function_02(second=False, first=None)
    73    # function_02(True, second=False, first=None)
    74    function_02(
    75        third=True, second=False,
    76        first=None,
    77    )
    

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

  • I add a call to function_03

    74    function_02(
    75        third=True, second=False,
    76        first=None,
    77    )
    78    function_03(
    79        [0, 1, 2, 'n'],
    80        first=(0, 1, 2, 'n'),
    81        third={0, 1, 2, 'n'},
    82        fourth={'key': 'value'},
    83    )
    84
    85    function_08(
    86        'positional',
    87        argument='keyword',
    88    )
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_03' is not defined
    

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

  • I move function_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():
    
    13def test_type_error_w_positional_arguments():
    14    def function_00(the_input):
    15        return None
    16
    17    function_00('a')
    18    function_01('a', 'b')
    19    function_02('a', 'b', 'c')
    20    function_03('a', 'b', 'c', 'd')
    21
    22
    23def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_03() got
               multiple values for argument 'first'
    

    because the call uses an argument in the first position and uses a keyword argument with the same name as the argument in the first position.

  • I add a keyword argument for the first value in the call to function_03 from test_type_error_w_args_and_kwargs to make it clearer

    74    function_02(
    75        third=True, second=False,
    76        first=None,
    77    )
    78    function_03(
    79        # [0, 1, 2, 'n'],
    80        second=[0, 1, 2, 'n'],
    81        first=(0, 1, 2, 'n'),
    82        third={0, 1, 2, 'n'},
    83        fourth={'key': 'value'},
    84    )
    

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

  • I add a call to function_07 from test_type_error_w_args_and_kwargs

    78    function_03(
    79        # [0, 1, 2, 'n'],
    80        second=[0, 1, 2, 'n'],
    81        first=(0, 1, 2, 'n'),
    82        third={0, 1, 2, 'n'},
    83        fourth={'key': 'value'},
    84    )
    85
    86    function_07(
    87        {0, 1, 2, 'n'},
    88        {'key': 'value'},
    89        argument_0=(0, 1, 2, 'n'),
    90        argument_1=[0, 1, 2, 'n'],
    91    )
    92    function_08(
    93        'positional',
    94        argument='keyword',
    95    )
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_07' is not defined
    

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

  • I move function_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():
    
    50    function_06(
    51        argument_0='value_1',
    52        argument_1=(0, 1, 2, 'n'),
    53        argument_2=[0, 1, 2, 'n'],
    54    )
    55    function_07(
    56        argument_0=(0, 1, 2, 'n'),
    57        argument_1=[0, 1, 2, 'n'],
    58        argument_2={0, 1, 2, 'n'},
    59        argument_n={'key': 'value'},
    60    )
    61
    62
    63def test_type_error_w_args_and_kwargs():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_07() got
               multiple values for argument 'argument_0'
    

    because

  • I change the call to use a keyword argument, then change its position

    86    function_07(
    87        # {0, 1, 2, 'n'},
    88        {'key': 'value'},
    89        argument_2={0, 1, 2, 'n'},
    90        argument_0=(0, 1, 2, 'n'),
    91        argument_1=[0, 1, 2, 'n'],
    92    )
    93    function_08(
    94        'positional',
    95        argument='keyword',
    96    )
    

    the terminal still shows TypeError because the call uses an argument in the first position and uses a keyword argument with the same name as the argument in the first position.

  • I use a keyword argument to make it clearer

    86    function_07(
    87        # {0, 1, 2, 'n'},
    88        # {'key': 'value'},
    89        argument_4={'key': 'value'},
    90        argument_2={0, 1, 2, 'n'},
    91        argument_0=(0, 1, 2, 'n'),
    92        argument_1=[0, 1, 2, 'n'],
    93    )
    

    the terminal is my friend, and shows TypeError

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

    because

  • I change the keyword argument to match the function definition

    86    function_07(
    87        # {0, 1, 2, 'n'},
    88        # {'key': 'value'},
    89        # argument_4={'key': 'value'},
    90        argument_n={'key': 'value'},
    91        argument_2={0, 1, 2, 'n'},
    92        argument_0=(0, 1, 2, 'n'),
    93        argument_1=[0, 1, 2, 'n'],
    94    )
    

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

  • I add a call to function_00 from test_type_error_w_args_and_kwargs

    63def test_type_error_w_args_and_kwargs():
    64    # def function_08(argument):
    65    # def function_08(argument, name):
    66    def function_08(name, argument):
    67        return None
    68
    69    function_00()
    70    # function_01(1, first=0)
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_00' is not defined
    

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

  • I move function_00 out of test_type_error_w_positional_arguments

    1def function_00(the_input):
    2    return None
    3
    4
    5def function_01(first, second):
    6    return None
    
    24def test_type_error_w_positional_arguments():
    25    function_00('a')
    26    function_01('a', 'b')
    27    function_02('a', 'b', 'c')
    28    function_03('a', 'b', 'c', 'd')
    29
    30
    31def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_00() missing
               1 required positional argument: 'the_input'
    

    because the function definition requires calls with one input and it got called with zero inputs.

  • I change the call from test_type_error_w_args_and_kwargs to match the signature

    64def test_type_error_w_args_and_kwargs():
    65    # def function_08(argument):
    66    # def function_08(argument, name):
    67    def function_08(name, argument):
    68        return None
    69
    70    # function_00()
    71    function_00('argument')
    72    # function_01(1, first=0)
    

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

  • I add a call to function_04 from test_type_error_w_args_and_kwargs

     81    function_03(
     82        # [0, 1, 2, 'n'],
     83        second=[0, 1, 2, 'n'],
     84        first=(0, 1, 2, 'n'),
     85        third={0, 1, 2, 'n'},
     86        fourth={'key': 'value'},
     87    )
     88    function_04('value_1', 'value2')
     89
     90    function_07(
     91        # {0, 1, 2, 'n'},
     92        # {'key': 'value'},
     93        # argument_4={'key': 'value'},
     94        argument_n={'key': 'value'},
     95        argument_2={0, 1, 2, 'n'},
     96        argument_0=(0, 1, 2, 'n'),
     97        argument_1=[0, 1, 2, 'n'],
     98    )
     99
    100
    101# Exceptions 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 test_type_error_w_keyword_arguments.

  • 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
    
    35def test_type_error_w_keyword_arguments():
    36    function_04(argument='value')
    37
    38    def function_05(argument_0, argument_1):
    39        return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_04() takes
               1 positional argument but 2 were given
    

    because the definition only allows one input, and it got called with two.

  • I change the call from test_type_error_w_args_and_kwargs to match the signature

     89    # function_04('value_1', 'value2')
     90    function_04('value')
     91
     92    function_07(
     93        # {0, 1, 2, 'n'},
     94        # {'key': 'value'},
     95        # argument_4={'key': 'value'},
     96        argument_n={'key': 'value'},
     97        argument_2={0, 1, 2, 'n'},
     98        argument_0=(0, 1, 2, 'n'),
     99        argument_1=[0, 1, 2, 'n'],
    100    )
    

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

  • I add a call to function_05 from test_type_error_w_args_and_kwargs

     89    # function_04('value_1', 'value2')
     90    function_04('value')
     91    function_05((0, 1, 2, 'n'))
     92
     93    function_07(
     94        # {0, 1, 2, 'n'},
     95        # {'key': 'value'},
     96        # argument_4={'key': 'value'},
     97        argument_n={'key': 'value'},
     98        argument_2={0, 1, 2, 'n'},
     99        argument_0=(0, 1, 2, 'n'),
    100        argument_1=[0, 1, 2, 'n'],
    101    )
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_05' is not 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
    
    39def test_type_error_w_keyword_arguments():
    40    function_04(argument='value')
    41    function_05(
    42        argument_0='value_1',
    43        argument_1=(0, 1, 2, 'n'),
    44    )
    45
    46    def function_06(
    47        argument_0, argument_1,
    48        argument_2,
    49    ):
    50        return None
    

    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 from test_type_error_w_args_and_kwargs to make it match the signature

    89    # function_04('value_1', 'value2')
    90    function_04('value')
    91    # function_05((0, 1, 2, 'n'))
    92    function_05(
    93        (0, 1, 2, 'n'),
    94        [0, 1, 2, 'n'],
    95    )
    

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

  • I add a call to function_06 from test_type_error_w_args_and_kwargs

     92    function_05(
     93        (0, 1, 2, 'n'),
     94        [0, 1, 2, 'n'],
     95    )
     96    function_06(
     97        (0, 1, 2, 'n'),
     98        [0, 1, 2, 'n'],
     99        argument_2={0, 1, 2, 'n'},
    100        argument_n={'key': 'value'},
    101    )
    102    function_07(
    103        # {0, 1, 2, 'n'},
    104        # {'key': 'value'},
    105        # argument_4={'key': 'value'},
    106        argument_n={'key': 'value'},
    107        argument_2={0, 1, 2, 'n'},
    108        argument_0=(0, 1, 2, 'n'),
    109        argument_1=[0, 1, 2, 'n'],
    110    )
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_06' is not defined
    

    because function_06 belongs to the test_type_error_w_keyword_arguments function.

  • 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(
    26    argument_0, argument_1,
    27    argument_2,
    28):
    29    return None
    30
    31
    32def function_07(
    33    argument_0, argument_1,
    34    argument_2, argument_n
    35):
    36    return None
    
    46def test_type_error_w_keyword_arguments():
    47    function_04(argument='value')
    48    function_05(
    49        argument_0='value_1',
    50        argument_1=(0, 1, 2, 'n'),
    51    )
    52    function_06(
    53        argument_0='value_1',
    54        argument_1=(0, 1, 2, 'n'),
    55        argument_2=[0, 1, 2, 'n'],
    56    )
    57    function_07(
    58        argument_0=(0, 1, 2, 'n'),
    59        argument_1=[0, 1, 2, 'n'],
    60        argument_2={0, 1, 2, 'n'},
    61        argument_n={'key': 'value'},
    62    )
    63
    64
    65def test_type_error_w_args_and_kwargs():
    

    the terminal is my friend, and shows TypeError

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

    because the function got called with a name (keyword) (argument_n) that is not in its definition

  • I remove argument_n={'key': 'value'} from the call from test_type_error_w_args_and_kwargs

     92    function_05(
     93        (0, 1, 2, 'n'),
     94        [0, 1, 2, 'n'],
     95    )
     96    function_06(
     97        (0, 1, 2, 'n'),
     98        [0, 1, 2, 'n'],
     99        argument_2={0, 1, 2, 'n'},
    100        # argument_n={'key': 'value'},
    101    )
    

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

  • I remove the commented lines from test_type_error_w_args_and_kwargs

    65def test_type_error_w_args_and_kwargs():
    66    def function_08(name, argument):
    67        return None
    68
    69    function_00('argument')
    70    function_01(1, 0)
    
    71    function_02(
    72        third=True, second=False,
    73        first=None,
    74    )
    75    function_03(
    76        second=[0, 1, 2, 'n'],
    77        first=(0, 1, 2, 'n'),
    78        third={0, 1, 2, 'n'},
    79        fourth={'key': 'value'},
    80    )
    
    81    function_04('value')
    82    function_05(
    83        (0, 1, 2, 'n'),
    84        [0, 1, 2, 'n'],
    85    )
    86    function_06(
    87        (0, 1, 2, 'n'),
    88        [0, 1, 2, 'n'],
    89        argument_2={0, 1, 2, 'n'},
    90    )
    
     91    function_07(
     92        argument_n={'key': 'value'},
     93        argument_2={0, 1, 2, 'n'},
     94        argument_0=(0, 1, 2, 'n'),
     95        argument_1=[0, 1, 2, 'n'],
     96    )
     97    function_08(
     98        'positional',
     99        argument='keyword',
    100    )
    
    101    function_08(
    102        argument='positional',
    103        name='keyword',
    104    )
    105
    106
    107# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'add test_type_error_w_args_and_kwargs'
    

test TypeError with required and unexpected arguments


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a call to function_03 from test_type_error_w_keyword_arguments

    46def test_type_error_w_keyword_arguments():
    47    function_03(
    48        first=None,
    49        second=False,
    50        third=True,
    51    )
    52    function_04(argument='value')
    

    the terminal is my friend, and shows TypeError

    TypeError: function_03() missing
               1 required positional argument: 'fourth'
    

    because the signature only allows calls with four inputs, and it got called with three.


GREEN: make it pass


I add fourth with a value to the call

46def test_type_error_w_keyword_arguments():
47    function_03(
48        first=None,
49        second=False,
50        third=True,
51        fourth=4,
52    )
53    function_04(argument='value')

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


REFACTOR: make it better


  • I add a call to function_02 from test_type_error_w_keyword_arguments

    46def test_type_error_w_keyword_arguments():
    47    function_02(
    48        fourth=4.0,
    49        third=(0, 1, 2, 'n'),
    50        second=[0, 1, 2, 'n'],
    51        first={0, 1, 2, 'n'},
    52    )
    53    function_03(
    54        first=None,
    55        second=False,
    56        third=True,
    57        fourth=4,
    58    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_02() got
               an unexpected keyword argument 'fourth'
    

    because the call uses a name (fourth) that is not in the parentheses of its definition.

  • I remove the unexpected keyword argument from the call

    46def test_type_error_w_keyword_arguments():
    47    function_02(
    48        # fourth=4.0,
    49        third=(0, 1, 2, 'n'),
    50        second=[0, 1, 2, 'n'],
    51        first={0, 1, 2, 'n'},
    52    )
    53    function_03(
    54        first=None,
    55        second=False,
    56        third=True,
    57        fourth=4,
    58    )
    

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

  • I add a call to function_01 from test_type_error_w_keyword_arguments

    46def test_type_error_w_keyword_arguments():
    47    function_01(
    48        second={'key': 'value'},
    49    )
    50    function_02(
    51        # fourth=4.0,
    52        third=(0, 1, 2, 'n'),
    53        second=[0, 1, 2, 'n'],
    54        first={0, 1, 2, 'n'},
    55    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_01() missing
               1 required positional argument: 'first'
    

    because the signature only allows calls with two inputs, and it got called with one.

  • I change the call to match the signature

    46def test_type_error_w_keyword_arguments():
    47    function_01(
    48        first='first',
    49        second={'key': 'value'},
    50    )
    

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

  • I add a call to function_00 from test_type_error_w_keyword_arguments

    46def test_type_error_w_keyword_arguments():
    47    function_00(second=1, first=0)
    48    function_01(
    49        first='first',
    50        second={'key': 'value'},
    51    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_00() got
               an unexpected keyword argument 'second'
    

    because the call uses a name (second) that …

  • I remove second from the call from test_type_error_w_keyword_arguments

    46def test_type_error_w_keyword_arguments():
    47    # function_00(second=1, first=0)
    48    function_00(first=0)
    49    function_01(
    50        first='first',
    51        second={'key': 'value'},
    52    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_00() got
               an unexpected keyword argument 'first'
    

    because the call

  • I remove first from the call

    46def test_type_error_w_keyword_arguments():
    47    # function_00(second=1, first=0)
    48    # function_00(first=0)
    49    function_00()
    50    function_01(
    51        first='first',
    52        second={'key': 'value'},
    53    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_00() missing
               1 required positional argument: 'the_input'
    
  • 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 I used the same name in the definition of the function when I called it with keyword arguments.

  • I move the call to function_08 from test_type_error_w_args_and_kwargs that uses keyword arguments to test_type_error_w_keyword_arguments

    77    function_07(
    78        argument_0=(0, 1, 2, 'n'),
    79        argument_1=[0, 1, 2, 'n'],
    80        argument_2={0, 1, 2, 'n'},
    81        argument_n={'key': 'value'},
    82    )
    83    function_08(
    84        argument='positional',
    85        name='keyword',
    86    )
    87
    88
    89def test_type_error_w_args_and_kwargs():
    
    115    function_07(
    116        argument_n={'key': 'value'},
    117        argument_2={0, 1, 2, 'n'},
    118        argument_0=(0, 1, 2, 'n'),
    119        argument_1=[0, 1, 2, 'n'],
    120    )
    121    function_08(
    122        'positional',
    123        argument='keyword',
    124    )
    125
    126
    127# Exceptions seen
    128# AssertionError
    129# NameError
    130# TypeError
    

    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

    32def function_07(
    33    argument_0, argument_1,
    34    argument_2, argument_n
    35):
    36    return None
    37
    38
    39def function_08(name, argument):
    40    return None
    41
    42
    43def test_type_error_w_positional_arguments():
    
    93def test_type_error_w_args_and_kwargs():
    94    function_00('argument')
    95    function_01(1, 0)
    96    function_02(third=True, second=False, first=None)
    

    the test is green again.

  • I remove the commented lines from test_type_error_w_keyword_arguments

    50def test_type_error_w_keyword_arguments():
    51    function_00(the_input=0)
    52    function_01(
    53        first='first',
    54        second={'key': 'value'},
    55    )
    
    56    function_02(
    57        third=(0, 1, 2, 'n'),
    58        second=[0, 1, 2, 'n'],
    59        first={0, 1, 2, 'n'},
    60    )
    61    function_03(
    62        first=None,
    63        second=False,
    64        third=True,
    65        fourth=4,
    66    )
    
    67    function_04(argument='value')
    68    function_05(
    69        argument_0='value_1',
    70        argument_1=(0, 1, 2, 'n'),
    71    )
    72    function_06(
    73        argument_0='value_1',
    74        argument_1=(0, 1, 2, 'n'),
    75        argument_2=[0, 1, 2, 'n'],
    76    )
    
    77    function_07(
    78        argument_0=(0, 1, 2, 'n'),
    79        argument_1=[0, 1, 2, 'n'],
    80        argument_2={0, 1, 2, 'n'},
    81        argument_n={'key': 'value'},
    82    )
    83    function_08(
    84        argument='positional',
    85        name='keyword',
    86    )
    87
    88
    89def test_type_error_w_args_and_kwargs():
    

  • I add a call to function_04 from test_type_error_w_positional_arguments

    43def test_type_error_w_positional_arguments():
    44    function_00('a')
    45    function_01('a', 'b')
    46    function_02('a', 'b', 'c')
    47    function_03('a', 'b', 'c', 'd')
    48    function_04()
    49
    50
    51def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_04() missing
               1 required positional argument: 'argument'
    
  • I add a value to the call to function_04

    46    function_02('a', 'b', 'c')
    47    function_03('a', 'b', 'c', 'd')
    48    # function_04()
    49    function_04('a')
    50
    51
    52def test_type_error_w_keyword_arguments():
    

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

  • I add a call to function_05 from test_type_error_w_positional_arguments

    48    # function_04()
    49    function_04('a')
    50    function_05('a', 'b', 'c')
    51
    52
    53def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_05() takes
               2 positional arguments but 3 were given
    
  • I remove a value from the call to function_05

    48    # function_04()
    49    function_04('a')
    50    # function_05('a', 'b', 'c')
    51    function_05('a', 'b')
    52
    53
    54def test_type_error_w_keyword_arguments():
    

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

  • I add a call to function_06 from test_type_error_w_positional_arguments

    50    # function_05('a', 'b', 'c')
    51    function_05('a', 'b')
    52    function_06('a', 'b')
    53
    54
    55def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_06() missing
               1 required positional argument: 'argument_2'
    
  • I add a value to the call to function_06

    50    # function_05('a', 'b', 'c')
    51    function_05('a', 'b')
    52    # function_06('a', 'b')
    53    function_06('a', 'b', 'c')
    54
    55
    56def test_type_error_w_keyword_arguments():
    

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

  • I add a call to function_07 from test_type_error_w_positional_arguments

    52    # function_06('a', 'b')
    53    function_06('a', 'b', 'c')
    54    function_07('a', 'b', 'c', 'd', 'e')
    55
    56
    57def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_07() takes
               4 positional arguments but 5 were given
    
  • I remove a value from the call to function_07

    52    # function_06('a', 'b')
    53    function_06('a', 'b', 'c')
    54    # function_07('a', 'b', 'c', 'd', 'e')
    55    function_07('a', 'b', 'c', 'd')
    56
    57
    58def test_type_error_w_keyword_arguments():
    

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

  • I add a call to function_08 from test_type_error_w_positional_arguments

    54    # function_07('a', 'b', 'c', 'd', 'e')
    55    function_07('a', 'b', 'c', 'd')
    56    function_08('last')
    57
    58
    59def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_08() missing
               1 required positional argument: 'argument'
    
  • I add a value to the call to function_08

    54    # function_07('a', 'b', 'c', 'd', 'e')
    55    function_07('a', 'b', 'c', 'd')
    56    # function_08('last')
    57    function_08('last', 'one')
    58
    59
    60def test_type_error_w_keyword_arguments():
    

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

  • I remove the commented lines from test_type_error_w_positional_arguments

    43def test_type_error_w_positional_arguments():
    44    function_00('a')
    45    function_01('a', 'b')
    46    function_02('a', 'b', 'c')
    47    function_03('a', 'b', 'c', 'd')
    48    function_04('a')
    49    function_05('a', 'b')
    50    function_06('a', 'b', 'c')
    51    function_07('a', 'b', 'c', 'd')
    52    function_08('last', 'one')
    53
    54
    55def test_type_error_w_keyword_arguments():
    
  • I add a git commit message in the other terminal

    git commit --all --message \
    'test with required and unexpected arguments'
    

I have to call a function with the same number or names of arguments that are in its definition.


close the project

  • I close test_type_error.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal goes back to the command line.

  • I change directory to the parent of type_error

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I ran tests for TypeError with

My problem with the tests is that they all show the correct way to call the functions I made in the file. If someone reads the file or runs it, there is no way for them to know how any of the calls are related to TypeError unless they go through the process with me, there has to be a better way.


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

Would you like to test using a function to make a string from input?


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.