separate and equal TypeError


I want to move the functions from test_type_error.py in the tests folder to type_error.py in the src folder so that I can keep the tests and solutions separate.


preview

I have these tests by the end of the chapter

type_error/tests/test_type_error.py
 1import src.type_error
 2
 3
 4def test_type_error_w_positional_arguments():
 5    src.type_error.function_00('a')
 6    src.type_error.function_01('a', 'b')
 7    src.type_error.function_02('a', 'b', 'c')
 8    src.type_error.function_03('a', 'b', 'c', 'd')
 9    src.type_error.function_04('a')
10    src.type_error.function_05('a', 'b')
11    src.type_error.function_06('a', 'b', 'c')
12    src.type_error.function_07('a', 'b', 'c', 'd')
13    src.type_error.function_08('last', 'one')
14
15
16def test_type_error_w_keyword_arguments():
17    src.type_error.function_00(the_input=0)
18    src.type_error.function_01(
19        first='first',
20        second={'key': 'value'},
21    )
22    src.type_error.function_02(
23        third=(0, 1, 2, 'n'),
24        second=[0, 1, 2, 'n'],
25        first={0, 1, 2, 'n'},
26    )
27    src.type_error.function_03(
28        first=None,
29        second=False,
30        third=True,
31        fourth=4,
32    )
33    src.type_error.function_04(argument='value')
34    src.type_error.function_05(
35        argument_0='value_1',
36        argument_1=(0, 1, 2, 'n'),
37    )
38    src.type_error.function_06(
39        argument_0='value_1',
40        argument_1=(0, 1, 2, 'n'),
41        argument_2=[0, 1, 2, 'n'],
42    )
43    src.type_error.function_07(
44        argument_0=(0, 1, 2, 'n'),
45        argument_1=[0, 1, 2, 'n'],
46        argument_2={0, 1, 2, 'n'},
47        argument_n={'key': 'value'},
48    )
49    src.type_error.function_08(
50        argument='positional',
51        name='keyword',
52    )
53
54
55def test_type_error_w_args_and_kwargs():
56    src.type_error.function_00('argument')
57    src.type_error.function_01(1, 0)
58    src.type_error.function_02(
59        third=True, second=False,
60        first=None,
61    )
62    src.type_error.function_03(
63        second=[0, 1, 2, 'n'],
64        first=(0, 1, 2, 'n'),
65        third={0, 1, 2, 'n'},
66        fourth={'key': 'value'},
67    )
68    src.type_error.function_04('value')
69    src.type_error.function_05(
70        (0, 1, 2, 'n'),
71        [0, 1, 2, 'n'],
72    )
73    src.type_error.function_06(
74        (0, 1, 2, 'n'),
75        [0, 1, 2, 'n'],
76        argument_2={0, 1, 2, 'n'},
77    )
78    src.type_error.function_07(
79        argument_n={'key': 'value'},
80        argument_2={0, 1, 2, 'n'},
81        argument_0=(0, 1, 2, 'n'),
82        argument_1=[0, 1, 2, 'n'],
83    )
84    src.type_error.function_08(
85        'positional',
86        argument='keyword',
87    )
88
89
90# Exceptions seen
91# AssertionError
92# NameError
93# TypeError
94# ModuleNotFoundError
95# AttributeError

open the project


mass migration

RED: make it fail


I change the call to function_00 in test_type_error_w_positional_arguments to be a call to function_00 of type_error.py in the src folder

43def test_type_error_w_positional_arguments():
44    # function_00('a')
45    src.type_error.function_00('a')
46    function_01('a', 'b')
47    function_02('a', 'b', 'c')
48    function_03('a', 'b', 'c', 'd')
49    function_04('a')
50    function_05('a', 'b')
51    function_06('a', 'b', 'c')
52    function_07('a', 'b', 'c', 'd')
53    function_08('last', 'one')
54
55
56def test_type_error_w_keyword_arguments():

the terminal is my friend, and shows NameError

NameError: name 'src' is not defined

because src is not defined in test_type_error.py.


GREEN: make it pass


  • I add an import statement at the top of test_type_error.py

    1import src.type_error
    2
    3
    4def function_00(the_input):
    5    return None
    

    the terminal is my friend, and shows ModuleNotFoundError

    ModuleNotFoundError: No module named 'src'
    

    because there is nothing named src in this project.

  • I add ModuleNotFoundError to the list of Exceptions seen

    133# Exceptions seen
    134# AssertionError
    135# NameError
    136# TypeError
    137# ModuleNotFoundError
    
  • I open another terminal then make sure I am in the type_error folder

    cd type_error
    
  • I use mkdir to make a folder named src

    mkdir src
    
  • I go back to the terminal where the tests are running.

  • I use ctrl/command+s (Windows & Linux/MacOS) on the keyboard in test_type_error.py to run the tests again and the terminal shows ModuleNotFoundError

    ModuleNotFoundError: No module named 'src.type_error'
    

    because there is nothing in the src folder named type_error.

  • I go to the second terminal I opened, then use touch to make type_error.py in the src folder

    touch src/type_error.py
    
  • I add the new file to git for tracking

    git add src/type_error.py
    

    the terminal goes back to the command line.

  • I go back to the terminal where the tests are running and it shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_00'
    

    because there is nothing in type_error.py in the src folder with the name function_00.

  • I add AttributeError to the list of Exceptions seen

    133# Exceptions seen
    134# AssertionError
    135# NameError
    136# TypeError
    137# ModuleNotFoundError
    138# AttributeError
    
  • I open type_error/__init__.py from the src folder

  • I add a definition for function_00 to type_error.py

    1def function_00():
    2    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_00() takes
               0 positional arguments but 1 was given
    
  • I add a name to the parentheses

    1# def function_00():
    2def function_00(one):
    3    return None
    

    the test passes because

    • Python brings in an object for the type_error.py file from the src folder so I can use it in test_type_error.py as src.type_error when import src.type_error runs.

    • Python calls the function_00 function from the object it imported for the type_error.py file from the src folder (src.type_error) when src.type_error.function_00 is called.

    I think of src.type_error.function_00 like an address

    src.type_error.function_00
    src
    └── type_error.py
        └── def function_00(one):
            └── return None
    

REFACTOR: make it better


  • I change the call to function_01 in test_type_error_w_positional_arguments to be a call to function_01 of type_error.py in the src folder, in test_type_error.py

    46def test_type_error_w_positional_arguments():
    47    # function_00('a')
    48    src.type_error.function_00('a')
    49    # function_01('a', 'b')
    50    src.type_error.function_01('a', 'b')
    51    function_02('a', 'b', 'c')
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_01'.
                    Did you mean: 'function_00'?
    
  • I add a function definition for function_01 to type_error.py

    1# def function_00():
    2def function_00(one):
    3    return None
    4
    5
    6def function_01():
    7    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_01() takes
               0 positional arguments but 2 were given
    
  • I add two names in the parentheses

    1# def function_00():
    2def function_00(one):
    3    return None
    4
    5
    6# def function_01():
    7def function_01(one, two):
    8    return None
    

    the test passes because Python follows this path when src.type_error.function_01 is called

    src.type_error.function_01
    src
    └── type_error.py
        └── def function_01(one, two):
            └── return None
    
  • I change the call to function_02 in test_type_error_w_positional_arguments to be a call to function_02 of type_error.py in the src folder, in test_type_error.py

    50    src.type_error.function_01('a', 'b')
    51    # function_02('a', 'b', 'c')
    52    src.type_error.function_02('a', 'b', 'c')
    53    function_03('a', 'b', 'c', 'd')
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_02'.
                    Did you mean: 'function_00'?
    
  • I add a function definition for function_02 to type_error.py

     6# def function_01():
     7def function_01(one, two):
     8    return None
     9
    10
    11def function_02():
    12    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_02() takes
               0 positional arguments but 3 were given
    
  • I add three names to the parentheses

     6# def function_01():
     7def function_01(one, two):
     8    return None
     9
    10
    11# def function_02():
    12def function_02(one, two, three):
    13    return None
    

    the test passes because Python follows this path when src.type_error.function_02 is called

    src.type_error.function_02
    src
    └── type_error.py
        └── def function_02(one, two, three):
            └── return None
    
  • I change the call to function_03 in test_type_error_w_positional_arguments to be a call to function_03 of type_error.py in the src folder, in test_type_error.py

    52    src.type_error.function_02('a', 'b', 'c')
    53    # function_03('a', 'b', 'c', 'd')
    54    src.type_error.function_03('a', 'b', 'c', 'd')
    55    function_04('a')
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_03'.
                    Did you mean: 'function_00'?
    
  • I add a function definition for function_03 to type_error.py

    11# def function_02():
    12def function_02(one, two, three):
    13    return None
    14
    15
    16def function_03():
    17    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_03() takes
               0 positional arguments but 4 were given
    
  • I add four names to the parentheses

    11# def function_02():
    12def function_02(one, two, three):
    13    return None
    14
    15
    16# def function_03():
    17def function_03(one, two, three, four):
    18    return None
    

    the test passes because Python follows this path when src.type_error.function_03 is called

    src.type_error.function_03
    src
    └── type_error.py
        └── def function_03(one, two, three, four):
            └── return None
    
  • I change the call to function_04 in test_type_error_w_positional_arguments to be a call to function_04 of type_error.py in the src folder, in test_type_error.py

    54    src.type_error.function_03('a', 'b', 'c', 'd')
    55    # function_04('a')
    56    src.type_error.function_04('a')
    57    function_05('a', 'b')
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_04'.
                    Did you mean: 'function_00'?
    
  • I add a function definition for function_04 to type_error.py

    16# def function_03():
    17def function_03(one, two, three, four):
    18    return None
    19
    20
    21def function_04():
    22    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_04() takes
               0 positional arguments but 1 was given
    
  • I add one to the parentheses

    16# def function_03():
    17def function_03(one, two, three, four):
    18    return None
    19
    20
    21# def function_04():
    22def function_04(one):
    23    return None
    

    the test passes because Python follows this path when src.type_error.function_04 is called

    src.type_error.function_04
    src
    └── type_error.py
        └── def function_04(one):
            └── return None
    
  • I change the call to function_05 in test_type_error_w_positional_arguments to be a call to function_05 of type_error.py in the src folder, in test_type_error.py

    56    src.type_error.function_04('a')
    57    # function_05('a', 'b')
    58    src.type_error.function_05('a', 'b')
    59    function_06('a', 'b', 'c')
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_05'.
                    Did you mean: 'function_00'?
    
  • I add a function definition for function_05 to type_error.py

    21# def function_04():
    22def function_04(one):
    23    return None
    24
    25
    26def function_05():
    27    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_05() takes
               0 positional arguments but 2 were given
    
  • I add two names to the parentheses

    21# def function_04():
    22def function_04(one):
    23    return None
    24
    25
    26# def function_05():
    27def function_05(one, two):
    28    return None
    

    the test passes because Python follows this path when src.type_error.function_05 is called

    src.type_error.function_05
    src
    └── type_error.py
        └── def function_05(one, two):
            └── return None
    
  • I change the call to function_06 in test_type_error_w_positional_arguments to be a call to function_06 of type_error.py in the src folder, in test_type_error.py

    58    src.type_error.function_05('a', 'b')
    59    # function_06('a', 'b', 'c')
    60    src.type_error.function_06('a', 'b', 'c')
    61    function_07('a', 'b', 'c', 'd')
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_06'.
                    Did you mean: 'function_00'?
    
  • I add a function definition for function_06 to type_error.py

    26# def function_05():
    27def function_05(one, two):
    28    return None
    29
    30
    31def function_06():
    32    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_06() takes
               0 positional arguments but 3 were given
    
  • I add three names to the parentheses

    26# def function_05():
    27def function_05(one, two):
    28    return None
    29
    30
    31# def function_06():
    32def function_06(one, two, three):
    33    return None
    

    the test passes because Python follows this path when src.type_error.function_06 is called

    src.type_error.function_06
    src
    └── type_error.py
        └── def function_06(one, two, three):
            └── return None
    
  • I change the call to function_07 in test_type_error_w_positional_arguments to be a call to function_07 of type_error.py in the src folder, in test_type_error.py

    60    src.type_error.function_06('a', 'b', 'c')
    61    # function_07('a', 'b', 'c', 'd')
    62    src.type_error.function_07('a', 'b', 'c', 'd')
    63    function_08('last', 'one')
    64
    65
    66def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_07'.
                    Did you mean: 'function_00'?
    
  • I add a function definition for function_07 to type_error.py

    31# def function_06():
    32def function_06(one, two, three):
    33    return None
    34
    35
    36def function_07():
    37    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_07() takes
               0 positional arguments but 4 were given
    
  • I add four names to the parentheses

    31# def function_06():
    32def function_06(one, two, three):
    33    return None
    34
    35
    36# def function_07():
    37def function_07(one, two, three, four):
    38    return None
    

    the test passes because Python follows this path when src.type_error.function_07 is called

    src.type_error.function_07
    src
    └── type_error.py
        └── def function_07(one, two, three, four):
            └── return None
    
  • I change the call to function_08 in test_type_error_w_positional_arguments to be a call to function_08 of type_error.py in the src folder, in test_type_error.py

    62    src.type_error.function_07('a', 'b', 'c', 'd')
    63    # function_08('last', 'one')
    64    src.type_error.function_08('last', 'one')
    65
    66
    67def test_type_error_w_keyword_arguments():
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.type_error'
                    has no attribute 'function_08'.
                    Did you mean: 'function_00'?
    
  • I add a function definition for function_08 to type_error.py

    36# def function_07():
    37def function_07(one, two, three, four):
    38    return None
    39
    40
    41def function_08():
    42    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_08() takes
               0 positional arguments but 2 were given
    
  • I add two names to the parentheses

    36# def function_07():
    37def function_07(one, two, three, four):
    38    return None
    39
    40
    41# def function_08():
    42def function_08(one, two):
    43    return None
    

    the test passes because Python follows this path when src.type_error.function_08 is called

    src.type_error.function_08
    src
    └── type_error.py
        └── def function_08(one, two):
            └── return None
    
  • I remove the commented lines from test_type_error_w_positional_arguments in test_type_error.py

    46def test_type_error_w_positional_arguments():
    47    src.type_error.function_00('a')
    48    src.type_error.function_01('a', 'b')
    49    src.type_error.function_02('a', 'b', 'c')
    50    src.type_error.function_03('a', 'b', 'c', 'd')
    51    src.type_error.function_04('a')
    52    src.type_error.function_05('a', 'b')
    53    src.type_error.function_06('a', 'b', 'c')
    54    src.type_error.function_07('a', 'b', 'c', 'd')
    55    src.type_error.function_08('last', 'one')
    56
    57
    58def test_type_error_w_keyword_arguments():
    

  • I change the call to function_00 in test_type_error_w_keyword_arguments to be a call to function_00 of type_error.py in the src folder, in test_type_error.py

    58def test_type_error_w_keyword_arguments():
    59    # function_00(the_input=0)
    60    src.type_error.function_00(the_input=0)
    61    function_01(
    62        first='first',
    63        second={'key': 'value'},
    64    )
    

    the terminal is my friend, and shows TypeError

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

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

  • I add the_input to the parentheses of the definition of function_00 in type_error.py

    1# def function_00():
    2# def function_00(one):
    3def function_00(one, the_input):
    4    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_00() missing
                   1 required positional argument: 'the_input'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_00() missing
                   1 required positional argument: 'one'
    

    my change broke a test that was passing.

  • I remove one from the parentheses

    1# def function_00():
    2# def function_00(one):
    3# def function_00(one, the_input):
    4def function_00(the_input):
    5    return None
    

    the test passes.

  • I change the call to function_01 in test_type_error_w_keyword_arguments to be a call to function_01 of type_error.py in the src folder, in test_type_error.py

    58def test_type_error_w_keyword_arguments():
    59    # function_00(the_input=0)
    60    src.type_error.function_00(the_input=0)
    61    # function_01(
    62    src.type_error.function_01(
    63        first='first',
    64        second={'key': 'value'},
    65    )
    

    the terminal is my friend, and shows TypeError

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

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

  • I add first to the parentheses of the definition of function_01 in type_error.py

     8# def function_01():
     9# def function_01(one, two):
    10def function_01(one, two, first):
    11    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_01() missing
                   1 required positional argument: 'first'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_01() got
                   an unexpected keyword argument 'second'
    

    another change that broke a test that was passing.

  • I add second to the parentheses

     8# def function_01():
     9# def function_01(one, two):
    10# def function_01(one, two, first):
    11def function_01(one, two, first, second):
    12    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_01() missing
                   2 required positional arguments:
                   'first' and 'second'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_01() missing
                   2 required positional arguments:
                   'one' and 'two'
    
  • I remove one and two from the parentheses

     8# def function_01():
     9# def function_01(one, two):
    10# def function_01(one, two, first):
    11# def function_01(one, two, first, second):
    12def function_01(first, second):
    13    return None
    

    the test passes.

  • I change the call to function_02 in test_type_error_w_keyword_arguments to be a call to function_02 of type_error.py in the src folder, in test_type_error.py

    62    src.type_error.function_01(
    63        first='first',
    64        second={'key': 'value'},
    65    )
    66    # function_02(
    67    src.type_error.function_02(
    68        third=(0, 1, 2, 'n'),
    69        second=[0, 1, 2, 'n'],
    70        first={0, 1, 2, 'n'},
    71    )
    

    the terminal is my friend, and shows TypeError

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

    because the call uses a name (third) that …

  • I add third to the parentheses of the definition of function_02 in type_error.py

    16# def function_02():
    17# def function_02(one, two, three):
    18def function_02(one, two, three, third):
    19    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_02() missing
                   1 required positional argument: 'third'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_02() got
                   an unexpected keyword argument 'second'
    

    I broke a test that was passing.

  • I add second to the parentheses

    16# def function_02():
    17# def function_02(one, two, three):
    18# def function_02(one, two, three, third):
    19def function_02(one, two, three, third, second):
    20    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_02() missing
                   2 required positional arguments:
                   'third' and 'second'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_02() got
                   an unexpected keyword argument 'first'
    
  • I add first to the parentheses

    16# def function_02():
    17# def function_02(one, two, three):
    18# def function_02(one, two, three, third):
    19# def function_02(one, two, three, third, second):
    20def function_02(
    21    one, two, three,
    22    third, second, first
    23):
    24    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_02() missing
                   3 required positional arguments:
                   'third', 'second', and 'first'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_02() missing
                   3 required positional arguments:
                   'one', 'two', and 'three'
    
  • I remove one, two and three from the parentheses

    16# def function_02():
    17# def function_02(one, two, three):
    18# def function_02(one, two, three, third):
    19# def function_02(one, two, three, third, second):
    20def function_02(
    21    # one, two, three,
    22    third, second, first
    23):
    24    return None
    

    the test passes.

  • I change the call to function_03 in test_type_error_w_keyword_arguments to be a call to function_03 of type_error.py in the src folder, in test_type_error.py

    67    src.type_error.function_02(
    68        third=(0, 1, 2, 'n'),
    69        second=[0, 1, 2, 'n'],
    70        first={0, 1, 2, 'n'},
    71    )
    72    # function_03(
    73    src.type_error.function_03(
    74        first=None,
    75        second=False,
    76        third=True,
    77        fourth=4,
    78    )
    

    the terminal is my friend, and shows TypeError

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

    because …

  • I add first to the parentheses of the definition of function_03 in type_error.py

    27# def function_03():
    28# def function_03(one, two, three, four):
    29def function_03(one, two, three, four, first):
    30    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_03() missing
                   1 required positional argument: 'first'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_03() got
                   an unexpected keyword argument 'second'
    
  • I add second to the parentheses

    27# def function_03():
    28# def function_03(one, two, three, four):
    29# def function_03(one, two, three, four, first):
    30def function_03(
    31    one, two, three, four,
    32    first, second,
    33):
    34    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_03() missing
                   2 required positional arguments:
                   'first' and 'second'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_03() got
                   an unexpected keyword argument 'third'
    

    because there is a

    • call that does not have the same number of arguments as the parentheses of its definition.

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

  • I add third to the parentheses

    27# def function_03():
    28# def function_03(one, two, three, four):
    29# def function_03(one, two, three, four, first):
    30def function_03(
    31    one, two, three, four,
    32    # first, second,
    33    first, second, third,
    34):
    35    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_03() missing
                   3 required positional arguments:
                   'first', 'second', and 'third'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_03() got
                   an unexpected keyword argument 'fourth'.
                   Did you mean 'four'?
    

    because there is a

    • call that does not have the same number of arguments as the parentheses of its definition.

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

  • I add fourth to the parentheses

    27# def function_03():
    28# def function_03(one, two, three, four):
    29# def function_03(one, two, three, four, first):
    30def function_03(
    31    one, two, three, four,
    32    # first, second,
    33    # first, second, third,
    34    first, second, third, fourth,
    35):
    36    return None
    

    the terminal is my friend, and shows TypeError

    FAILED ...::test_type_error_w_positional_arguments -
        TypeError: function_03() missing
                   4 required positional arguments:
                   'first', 'second', 'third', and 'fourth'
    FAILED ...::test_type_error_w_keyword_arguments -
        TypeError: function_03() missing
                   4 required positional arguments:
                   'one', 'two', 'three', and 'four'
    
  • I remove one, two, three and four from the parentheses

    27# def function_03():
    28# def function_03(one, two, three, four):
    29# def function_03(one, two, three, four, first):
    30def function_03(
    31    # one, two, three, four,
    32    # first, second
    33    # first, second, third,
    34    first, second, third, fourth,
    35):
    36    return None
    

    the test passes.

  • I change the call to function_04 in test_type_error_w_keyword_arguments to be a call to function_04 of type_error.py in the src folder, in test_type_error.py

    73    src.type_error.function_03(
    74        first=None,
    75        second=False,
    76        third=True,
    77        fourth=4,
    78    )
    79    # function_04(argument='value')
    80    src.type_error.function_04(argument='value')
    81    function_05(
    82        argument_0='value_1',
    83        argument_1=(0, 1, 2, 'n'),
    84    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_04() got
               an unexpected keyword argument 'argument'
    
  • I change one to argument in the parentheses of the definition of function_04 in type_error.py

    39# def function_04():
    40# def function_04(one):
    41def function_04(argument):
    42    return None
    

    the test passes.

  • I change the call to function_05 in test_type_error_w_keyword_arguments to be a call to function_05 of type_error.py in the src folder, in test_type_error.py

    80    src.type_error.function_04(argument='value')
    81    # function_05(
    82    src.type_error.function_05(
    83        argument_0='value_1',
    84        argument_1=(0, 1, 2, 'n'),
    85    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_05() got
               an unexpected keyword argument 'argument_0'
    
  • I change one to argument_0 in the parentheses of the definition of function_05 in type_error.py

    45# def function_05():
    46# def function_05(one, two):
    47def function_05(argument_0, two):
    48    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_05() got
               an unexpected keyword argument 'argument_1'.
               Did you mean 'argument_0'?
    
  • I change two to argument_1 in the parentheses

    45# def function_05():
    46# def function_05(one, two):
    47# def function_05(argument_0, two):
    48def function_05(argument_0, argument_1):
    49    return None
    

    the test passes.

  • I change the call to function_06 in test_type_error_w_keyword_arguments to be a call to function_06 of type_error.py in the src folder, in test_type_error.py

    82    src.type_error.function_05(
    83        argument_0='value_1',
    84        argument_1=(0, 1, 2, 'n'),
    85    )
    86    # function_06(
    87    src.type_error.function_06(
    88        argument_0='value_1',
    89        argument_1=(0, 1, 2, 'n'),
    90        argument_2=[0, 1, 2, 'n'],
    91    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_06() got
               an unexpected keyword argument 'argument_0'
    
  • I change one to argument_0 in the parentheses of the definition of function_06 in type_error.py

    52# def function_06():
    53# def function_06(one, two, three):
    54def function_06(argument_0, two, three):
    55    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_06() got
               an unexpected keyword argument 'argument_1'.
               Did you mean 'argument_0'?
    
  • I change two to argument_1 in the parentheses

    52# def function_06():
    53# def function_06(one, two, three):
    54# def function_06(argument_0, two, three):
    55def function_06(argument_0, argument_1, three):
    56    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_06() got
               an unexpected keyword argument 'argument_2'.
               Did you mean 'argument_0'?
    
  • I change three to argument_2 in the parentheses

    52# def function_06():
    53# def function_06(one, two, three):
    54# def function_06(argument_0, two, three):
    55# def function_06(argument_0, argument_1, three):
    56def function_06(argument_0, argument_1, argument_2):
    57    return None
    

    the test passes.

  • I change the call to function_07 in test_type_error_w_keyword_arguments to be a call to function_07 of type_error.py in the src folder, in test_type_error.py

    87    src.type_error.function_06(
    88        argument_0='value_1',
    89        argument_1=(0, 1, 2, 'n'),
    90        argument_2=[0, 1, 2, 'n'],
    91    )
    92    # function_07(
    93    src.type_error.function_07(
    94        argument_0=(0, 1, 2, 'n'),
    95        argument_1=[0, 1, 2, 'n'],
    96        argument_2={0, 1, 2, 'n'},
    97        argument_n={'key': 'value'},
    98    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_07() got
               an unexpected keyword argument 'argument_0'
    

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

  • I change one to argument_0 in the parentheses of the definition of function_07 in type_error.py

    60# def function_07():
    61# def function_07(one, two, three, four):
    62def function_07(argument_0, two, three, four):
    63    return None
    

    the terminal is my friend, and shows TypeError

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

    because …

  • I change two to argument_1 in the parentheses

    60# def function_07():
    61# def function_07(one, two, three, four):
    62# def function_07(argument_0, two, three, four):
    63def function_07(
    64    argument_0, argument_1, three, four
    65):
    66    return None
    

    the terminal is my friend, and shows TypeError

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

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

  • I change three to argument_2 in the parentheses

    60# def function_07():
    61# def function_07(one, two, three, four):
    62# def function_07(argument_0, two, three, four):
    63def function_07(
    64    # argument_0, argument_1, three, four
    65    argument_0, argument_1,
    66    argument_2, four
    67):
    68    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_07() got
               an unexpected keyword argument 'argument_n'.
               Did you mean 'argument_0'?
    
  • I change four to argument_n in the parentheses

    60# def function_07():
    61# def function_07(one, two, three, four):
    62# def function_07(argument_0, two, three, four):
    63def function_07(
    64    # argument_0, argument_1, three, four
    65    argument_0, argument_1,
    66    # argument_2, four
    67    argument_2, argument_n
    68):
    69    return None
    

    the test passes.

  • I change the call to function_08 in test_type_error_w_keyword_arguments to be a call to function_08 of type_error.py in the src folder, in test_type_error.py

     97    src.type_error.function_07(
     98        argument_0=(0, 1, 2, 'n'),
     99        argument_1=[0, 1, 2, 'n'],
    100        argument_2={0, 1, 2, 'n'},
    101        argument_n={'key': 'value'},
    102    )
    103    # function_08(
    104    src.type_error.function_08(
    105        argument='positional',
    106        name='keyword',
    107    )
    108
    109
    110def test_type_error_w_args_and_kwargs():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_08() got
               an unexpected keyword argument 'argument'
    

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

  • I change one to argument in the parentheses of the definition of function_08 in type_error.py

    72# def function_08():
    73# def function_08(one, two):
    74def function_08(argument, two):
    75    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: function_08() got
               an unexpected keyword argument 'name'.
    
  • I change two to name in the parentheses

    72# def function_08():
    73# def function_08(one, two):
    74# def function_08(argument, two):
    75def function_08(argument, name):
    76    return None
    

    the test passes.

  • I remove the commented lines from test_type_error_w_keyword_arguments in test_type_error.py

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

  • I change the call to function_00 in test_type_error_w_args_and_kwargs to be a call to function_00 of type_error.py in the src folder, in test_type_error.py

    97def test_type_error_w_args_and_kwargs():
    98    # function_00('argument')
    99    src.type_error.function_00('argument')
    

    the test is still green.

  • I change the call to function_01 to src.type_error.function_01

     97def test_type_error_w_args_and_kwargs():
     98    # function_00('argument')
     99    src.type_error.function_00('argument')
    100    # function_01(1, 0)
    101    src.type_error.function_01(1, 0)
    

    still green.

  • I change the call to function_02 to src.type_error.function_02

    101    src.type_error.function_01(1, 0)
    102    # function_02(
    103    src.type_error.function_02(
    104        third=True, second=False,
    105        first=None,
    106    )
    

    green.

  • I change the call to function_03 to src.type_error.function_03

    103    src.type_error.function_02(
    104        third=True, second=False,
    105        first=None,
    106    )
    107    # function_03(
    108    src.type_error.function_03(
    109        second=[0, 1, 2, 'n'],
    110        first=(0, 1, 2, 'n'),
    111        third={0, 1, 2, 'n'},
    112        fourth={'key': 'value'},
    113    )
    

    still green.

  • I change the call to function_04 to src.type_error.function_04

    108    src.type_error.function_03(
    109        second=[0, 1, 2, 'n'],
    110        first=(0, 1, 2, 'n'),
    111        third={0, 1, 2, 'n'},
    112        fourth={'key': 'value'},
    113    )
    114    # function_04('value')
    115    src.type_error.function_04('value')
    

    the test is still green.

  • I change the call to function_05 to src.type_error.function_05

    115    src.type_error.function_04('value')
    116    # function_05(
    117    src.type_error.function_05(
    118        (0, 1, 2, 'n'),
    119        [0, 1, 2, 'n'],
    120    )
    

    still green.

  • I change the call to function_06 to src.type_error.function_06

    117    src.type_error.function_05(
    118        (0, 1, 2, 'n'),
    119        [0, 1, 2, 'n'],
    120    )
    121    # function_06(
    122    src.type_error.function_06(
    123        (0, 1, 2, 'n'),
    124        [0, 1, 2, 'n'],
    125        argument_2={0, 1, 2, 'n'},
    126    )
    

    green.

  • I change the call to function_07 to src.type_error.function_07

    122    src.type_error.function_06(
    123        (0, 1, 2, 'n'),
    124        [0, 1, 2, 'n'],
    125        argument_2={0, 1, 2, 'n'},
    126    )
    127    # function_07(
    128    src.type_error.function_07(
    129        argument_n={'key': 'value'},
    130        argument_2={0, 1, 2, 'n'},
    131        argument_0=(0, 1, 2, 'n'),
    132        argument_1=[0, 1, 2, 'n'],
    133    )
    

    still green.

  • I change the call to function_08 to src.type_error.function_08

    128    src.type_error.function_07(
    129        argument_n={'key': 'value'},
    130        argument_2={0, 1, 2, 'n'},
    131        argument_0=(0, 1, 2, 'n'),
    132        argument_1=[0, 1, 2, 'n'],
    133    )
    134    # function_08(
    135    src.type_error.function_08(
    136        'positional',
    137        argument='keyword',
    138    )
    139
    140
    141# Exceptions seen
    

    the terminal is my friend, and shows TypeError

    TypeError: function_08() got
               multiple values for argument 'argument'
    
  • I change the order of the arguments in the definition of function_08 in type_error.py

    72# def function_08():
    73# def function_08(one, two):
    74# def function_08(argument, two):
    75# def function_08(argument, name):
    76def function_08(name, argument):
    77    return None
    

    the test passes.

  • I remove the commented lines from type_error.py

    1def function_00(the_input):
    2    return None
    3
    4
    5def function_01(first, second):
    6    return None
    
     9def function_02(
    10    third, second, first
    11):
    12    return None
    13
    14
    15def function_03(
    16    first, second, third, fourth,
    17):
    18    return None
    
    21def function_04(argument):
    22    return None
    23
    24
    25def function_05(argument_0, argument_1):
    26    return None
    
    29def function_06(argument_0, argument_1, argument_2):
    30    return None
    31
    32
    33def function_07(
    34    argument_0, argument_1,
    35    argument_2, argument_n
    36):
    37    return None
    38
    39
    40def function_08(name, argument):
    41    return None
    
  • I remove the commented lines from test_type_error_w_args_and_kwargs in test_type_error.py

     97def test_type_error_w_args_and_kwargs():
     98    src.type_error.function_00('argument')
     99    src.type_error.function_01(1, 0)
    100    src.type_error.function_02(
    101        third=True, second=False,
    102        first=None,
    103    )
    
    104    src.type_error.function_03(
    105        second=[0, 1, 2, 'n'],
    106        first=(0, 1, 2, 'n'),
    107        third={0, 1, 2, 'n'},
    108        fourth={'key': 'value'},
    109    )
    
    110    src.type_error.function_04('value')
    111    src.type_error.function_05(
    112        (0, 1, 2, 'n'),
    113        [0, 1, 2, 'n'],
    114    )
    
    115    src.type_error.function_06(
    116        (0, 1, 2, 'n'),
    117        [0, 1, 2, 'n'],
    118        argument_2={0, 1, 2, 'n'},
    119    )
    
    120    src.type_error.function_07(
    121        argument_n={'key': 'value'},
    122        argument_2={0, 1, 2, 'n'},
    123        argument_0=(0, 1, 2, 'n'),
    124        argument_1=[0, 1, 2, 'n'],
    125    )
    
    126    src.type_error.function_08(
    127        'positional',
    128        argument='keyword',
    129    )
    130
    131
    132# Exceptions seen
    133# AssertionError
    134# NameError
    135# TypeError
    136# ModuleNotFoundError
    137# AttributeError
    
  • I remove function_00 to function_08 from test_type_error.py because they are no longer called

    1import src.type_error
    2
    3
    4def test_type_error_w_positional_arguments():
    

    all the tests are passing because function_00 to function_08 are now attributes of type_error.py in the src folder and I can call them from outside the file with src.type_error.function_name()

    src.type_error.function_name()
    src
    └── type_error.py
        └── def function_name(): return None
    

    where function_name is the name of the function.

  • I open a terminal then change directory to the type_error folder

    cd type_error
    
  • I add type_error.py to git for tracking

    git add src/type_error.py
    
  • I add a git commit message in the new terminal

    git commit --all --message \
    'move functions to type_error.py'
    

close the project

  • I close tests/test_type_error.py and src/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 can write solutions in a different module from the tests.


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 AttributeError?


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.