separate and equal TypeError


I want to move the functions 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

 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='value1',
36        argument_1=(0, 1, 2, 'n'),
37    )
38    src.type_error.function_06(
39        argument_0='value1',
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', name='keyword'
51    )
52
53
54def test_type_error_w_args_and_kwargs():
55    src.type_error.function_00('argument')
56    src.type_error.function_01(1, 0)
57    src.type_error.function_02(
58        third=True, second=False, first=None
59    )
60    src.type_error.function_03(
61        second=[0, 1, 2, 'n'],
62        first=(0, 1, 2, 'n'),
63        third={0, 1, 2, 'n'},
64        fourth={'key': 'value'}
65    )
66    src.type_error.function_04('value')
67    src.type_error.function_05(
68        (0, 1, 2, 'n'),
69        [0, 1, 2, 'n'],
70    )
71    src.type_error.function_06(
72        (0, 1, 2, 'n'),
73        [0, 1, 2, 'n'],
74        argument_2={0, 1, 2, 'n'},
75    )
76    src.type_error.function_07(
77        argument_n={'key': 'value'},
78        argument_2={0, 1, 2, 'n'},
79        argument_0=(0, 1, 2, 'n'),
80        argument_1=[0, 1, 2, 'n'],
81    )
82    src.type_error.function_08(
83        'positional', argument='keyword'
84    )
85
86
87# Exceptions seen
88# AssertionError
89# NameError
90# TypeError
91# ModuleNotFoundError
92# AttributeError

open the project

  • I open a terminal

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

    cd type_error
    

    the terminal shows I am in the type_error folder

    .../pumping_python/type_error
    
  • I open test_type_error.py

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal shows

    test_type_error.py ...                              [100%]
    
    =================== 3 passed in X.YZs ====================
    

mass migration

RED: make it fail


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

40def test_type_error_w_positional_arguments():
41    # function_00('a')
42    src.type_error.function_00('a')
43    function_01('a', 'b')
44    function_02('a', 'b', 'c')
45    function_03('a', 'b', 'c', 'd')
46    function_04('a')
47    function_05('a', 'b')
48    function_06('a', 'b', 'c')
49    function_07('a', 'b', 'c', 'd')
50    function_08('last', 'one')
51
52
53def 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 AttributeError to the list of Exceptions seen

    121# Exceptions seen
    122# AssertionError
    123# NameError
    124# TypeError
    125# ModuleNotFoundError
    
  • I open another terminal then make sure I am in the type_error folder

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

    121# Exceptions seen
    122# AssertionError
    123# NameError
    124# TypeError
    125# ModuleNotFoundError
    126# AttributeError
    
  • I open type_error.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

    • When import src.type_error runs, 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 src.type_error.function_00 is called, Python calls the function_00 function from the object it imported for the type_error.py file from the src folder (src.type_error).

    I think of src.type_error.function_00 like an address

    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 a call to function_01 of type_error.py in the src folder, in test_type_error.py

    43def test_type_error_w_positional_arguments():
    44    # function_00('a')
    45    src.type_error.function_00('a')
    46    # function_01('a', 'b')
    47    src.type_error.function_01('a', 'b')
    48    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.py
        └── def function_01(one, two):
                return None
    
  • I change the call to function_02 in test_type_error_w_positional_arguments to a call to function_02 of type_error.py in the src folder, in test_type_error.py

    43def test_type_error_w_positional_arguments():
    44    # function_00('a')
    45    src.type_error.function_00('a')
    46    # function_01('a', 'b')
    47    src.type_error.function_01('a', 'b')
    48    # function_02('a', 'b', 'c')
    49    src.type_error.function_02('a', 'b', 'c')
    50    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.py
        └── def function_02(one, two, three):
                return None
    
  • I change the call to function_03 in test_type_error_w_positional_arguments to a call to function_03 of type_error.py in the src folder, in test_type_error.py

    43def test_type_error_w_positional_arguments():
    44    # function_00('a')
    45    src.type_error.function_00('a')
    46    # function_01('a', 'b')
    47    src.type_error.function_01('a', 'b')
    48    # function_02('a', 'b', 'c')
    49    src.type_error.function_02('a', 'b', 'c')
    50    # function_03('a', 'b', 'c', 'd')
    51    src.type_error.function_03('a', 'b', 'c', 'd')
    52    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.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 a call to function_04 of type_error.py in the src folder, in test_type_error.py

    43def test_type_error_w_positional_arguments():
    44    # function_00('a')
    45    src.type_error.function_00('a')
    46    # function_01('a', 'b')
    47    src.type_error.function_01('a', 'b')
    48    # function_02('a', 'b', 'c')
    49    src.type_error.function_02('a', 'b', 'c')
    50    # function_03('a', 'b', 'c', 'd')
    51    src.type_error.function_03('a', 'b', 'c', 'd')
    52    # function_04('a')
    53    src.type_error.function_04('a')
    54    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.py
        └── def function_04(one):
                return None
    
  • I change the call to function_05 in test_type_error_w_positional_arguments to a call to function_05 of type_error.py in the src folder, in test_type_error.py

    43def test_type_error_w_positional_arguments():
    44    # function_00('a')
    45    src.type_error.function_00('a')
    46    # function_01('a', 'b')
    47    src.type_error.function_01('a', 'b')
    48    # function_02('a', 'b', 'c')
    49    src.type_error.function_02('a', 'b', 'c')
    50    # function_03('a', 'b', 'c', 'd')
    51    src.type_error.function_03('a', 'b', 'c', 'd')
    52    # function_04('a')
    53    src.type_error.function_04('a')
    54    # function_05('a', 'b')
    55    src.type_error.function_05('a', 'b')
    56    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.py
        └── def function_05(one, two):
                return None
    
  • I change the call to function_06 in test_type_error_w_positional_arguments to a call to function_06 of type_error.py in the src folder, in test_type_error.py

    43def test_type_error_w_positional_arguments():
    44    # function_00('a')
    45    src.type_error.function_00('a')
    46    # function_01('a', 'b')
    47    src.type_error.function_01('a', 'b')
    48    # function_02('a', 'b', 'c')
    49    src.type_error.function_02('a', 'b', 'c')
    50    # function_03('a', 'b', 'c', 'd')
    51    src.type_error.function_03('a', 'b', 'c', 'd')
    52    # function_04('a')
    53    src.type_error.function_04('a')
    54    # function_05('a', 'b')
    55    src.type_error.function_05('a', 'b')
    56    # function_06('a', 'b', 'c')
    57    src.type_error.function_06('a', 'b', 'c')
    58    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.py
        └── def function_06(one, two, three):
                return None
    
  • I change the call to function_07 in test_type_error_w_positional_arguments to a call to function_07 of type_error.py in the src folder, in test_type_error.py

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

    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.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 a call to function_08 of type_error.py in the src folder, in test_type_error.py

    43def test_type_error_w_positional_arguments():
    44    # function_00('a')
    45    src.type_error.function_00('a')
    46    # function_01('a', 'b')
    47    src.type_error.function_01('a', 'b')
    48    # function_02('a', 'b', 'c')
    49    src.type_error.function_02('a', 'b', 'c')
    50    # function_03('a', 'b', 'c', 'd')
    51    src.type_error.function_03('a', 'b', 'c', 'd')
    52    # function_04('a')
    53    src.type_error.function_04('a')
    54    # function_05('a', 'b')
    55    src.type_error.function_05('a', 'b')
    56    # function_06('a', 'b', 'c')
    57    src.type_error.function_06('a', 'b', 'c')
    58    # function_07('a', 'b', 'c', 'd')
    59    src.type_error.function_07('a', 'b', 'c', 'd')
    60    # function_08('last', 'one')
    61    src.type_error.function_08('last', 'one')
    62
    63
    64def 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.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

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

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

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

    the terminal is my friend, and shows TypeError

    TypeError: function_00() got
               an unexpected keyword argument 'the_input'
    
  • I add the_input in 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 a call to function_01 of type_error.py in the src folder, in test_type_error.py

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

    the terminal is my friend, and shows TypeError

    TypeError: function_01() got
               an unexpected keyword argument 'first'
    
  • I add first in 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 a call to function_02 of type_error.py in the src folder, in test_type_error.py

    55def test_type_error_w_keyword_arguments():
    56    # function_00(the_input=0)
    57    src.type_error.function_00(the_input=0)
    58    # function_01(
    59    src.type_error.function_01(
    60        first='first',
    61        second={'key': 'value'},
    62    )
    63    # function_02(
    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    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_02() got
               an unexpected keyword argument 'third'
    
  • I add third in 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 a call to function_03 of type_error.py in the src folder, in test_type_error.py

    55def test_type_error_w_keyword_arguments():
    56    # function_00(the_input=0)
    57    src.type_error.function_00(the_input=0)
    58    # function_01(
    59    src.type_error.function_01(
    60        first='first',
    61        second={'key': 'value'},
    62    )
    63    # function_03(
    64    src.type_error.function_03(
    65        third=(0, 1, 2, 'n'),
    66        second=[0, 1, 2, 'n'],
    67        first={0, 1, 2, 'n'},
    68    )
    69    # function_03(
    70    src.type_error.function_03(
    71        first=None,
    72        second=False,
    73        third=True,
    74        fourth=4,
    75    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_03() got
               an unexpected keyword argument 'first'
    
  • I add first in 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'
    
  • 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'?
    
  • 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 a call to function_04 of type_error.py in the src folder, in test_type_error.py

    55def test_type_error_w_keyword_arguments():
    56    # function_00(the_input=0)
    57    src.type_error.function_00(the_input=0)
    58    # function_01(
    59    src.type_error.function_01(
    60        first='first',
    61        second={'key': 'value'},
    62    )
    63    # function_02(
    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    # function_03(
    70    src.type_error.function_03(
    71        first=None,
    72        second=False,
    73        third=True,
    74        fourth=4,
    75    )
    76    # function_04(argument='value')
    77    src.type_error.function_04(argument='value')
    

    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 a call to function_05 of type_error.py in the src folder, in test_type_error.py

    55def test_type_error_w_keyword_arguments():
    56    # function_00(the_input=0)
    57    src.type_error.function_00(the_input=0)
    58    # function_01(
    59    src.type_error.function_01(
    60        first='first',
    61        second={'key': 'value'},
    62    )
    63    # function_02(
    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    # function_03(
    70    src.type_error.function_03(
    71        first=None,
    72        second=False,
    73        third=True,
    74        fourth=4,
    75    )
    76    # function_04(argument='value')
    77    src.type_error.function_04(argument='value')
    78    # function_05(
    79    src.type_error.function_05(
    80        argument_0='value1',
    81        argument_1=(0, 1, 2, 'n'),
    82    )
    

    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 a call to function_06 of type_error.py in the src folder, in test_type_error.py

    78    # function_05(
    79    src.type_error.function_05(
    80        argument_0='value1',
    81        argument_1=(0, 1, 2, 'n'),
    82    )
    83    # function_06(
    84    src.type_error.function_06(
    85        argument_0='value1',
    86        argument_1=(0, 1, 2, 'n'),
    87        argument_2=[0, 1, 2, 'n'],
    88    )
    

    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 a call to function_07 of type_error.py in the src folder, in test_type_error.py

    83    # function_06(
    84    src.type_error.function_06(
    85        argument_0='value1',
    86        argument_1=(0, 1, 2, 'n'),
    87        argument_2=[0, 1, 2, 'n'],
    88    )
    89    # function_07(
    90    src.type_error.function_07(
    91        argument_0=(0, 1, 2, 'n'),
    92        argument_1=[0, 1, 2, 'n'],
    93        argument_2={0, 1, 2, 'n'},
    94        argument_n={'key': 'value'},
    95    )
    

    the terminal is my friend, and shows TypeError

    TypeError: function_07() got
               an unexpected keyword argument 'argument_0'
    
  • 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'?
    
  • 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'?
    
  • 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 a call to function_08 of type_error.py in the src folder, in test_type_error.py

     89    # function_07(
     90    src.type_error.function_07(
     91        argument_0=(0, 1, 2, 'n'),
     92        argument_1=[0, 1, 2, 'n'],
     93        argument_2={0, 1, 2, 'n'},
     94        argument_n={'key': 'value'},
     95    )
     96    # function_08(argument='positional', name='keyword')
     97    src.type_error.function_08(
     98        argument='positional', name='keyword'
     99    )
    100
    101
    102def test_type_error_w_args_and_kwargs():
    

    the terminal is my friend, and shows TypeError

    TypeError: function_07() got
               an unexpected keyword argument 'argument'
    
  • I change one to argument_0 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

    60# def function_08():
    61# def function_08(one, two):
    62# def function_08(argument, two):
    63def function_08(argument, name):
    64    return None
    

    the test passes.

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

    55def test_type_error_w_keyword_arguments():
    56    src.type_error.function_00(the_input=0)
    57    src.type_error.function_01(
    58        first='first',
    59        second={'key': 'value'},
    60    )
    61    src.type_error.function_02(
    62        third=(0, 1, 2, 'n'),
    63        second=[0, 1, 2, 'n'],
    64        first={0, 1, 2, 'n'},
    65    )
    66    src.type_error.function_03(
    67        first=None,
    68        second=False,
    69        third=True,
    70        fourth=4,
    71    )
    72    src.type_error.function_04(argument='value')
    73    src.type_error.function_05(
    74        argument_0='value1',
    75        argument_1=(0, 1, 2, 'n'),
    76    )
    77    src.type_error.function_06(
    78        argument_0='value1',
    79        argument_1=(0, 1, 2, 'n'),
    80        argument_2=[0, 1, 2, 'n'],
    81    )
    82    src.type_error.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    src.type_error.function_08(
    89        argument='positional', name='keyword'
    90    )
    91
    92
    93def test_type_error_w_args_and_kwargs():
    

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

    93def test_type_error_w_args_and_kwargs():
    94    # function_00('argument')
    95    src.type_error.function_00('argument')
    

    the test is still green.

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

    93def test_type_error_w_args_and_kwargs():
    94    # function_00('argument')
    95    src.type_error.function_00('argument')
    96    # function_01(1, 0)
    97    src.type_error.function_01(1, 0)
    

    still green.

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

     93def test_type_error_w_args_and_kwargs():
     94    # function_00('argument')
     95    src.type_error.function_00('argument')
     96    # function_01(1, 0)
     97    src.type_error.function_01(1, 0)
     98    # function_02(third=True, second=False, first=None)
     99    src.type_error.function_02(
    100        third=True, second=False, first=None
    101    )
    

    green.

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

     98    # function_02(third=True, second=False, first=None)
     99    src.type_error.function_02(
    100        third=True, second=False, first=None
    101    )
    102    # function_03(
    103    src.type_error.function_03(
    104        second=[0, 1, 2, 'n'],
    105        first=(0, 1, 2, 'n'),
    106        third={0, 1, 2, 'n'},
    107        fourth={'key': 'value'}
    108    )
    

    still green.

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

    102    # function_03(
    103    src.type_error.function_03(
    104        second=[0, 1, 2, 'n'],
    105        first=(0, 1, 2, 'n'),
    106        third={0, 1, 2, 'n'},
    107        fourth={'key': 'value'}
    108    )
    109    # function_04('value')
    110    src.type_error.function_04('value')
    

    the test is still green.

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

    109    # function_04('value')
    110    src.type_error.function_04('value')
    111    # function_05(
    112    src.type_error.function_05(
    113        (0, 1, 2, 'n'),
    114        [0, 1, 2, 'n'],
    115    )
    

    still green.

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

    111    # function_05(
    112    src.type_error.function_05(
    113        (0, 1, 2, 'n'),
    114        [0, 1, 2, 'n'],
    115    )
    116    # function_06(
    117    src.type_error.function_06(
    118        (0, 1, 2, 'n'),
    119        [0, 1, 2, 'n'],
    120        argument_2={0, 1, 2, 'n'},
    121    )
    

    green.

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

    116    # function_06(
    117    src.type_error.function_06(
    118        (0, 1, 2, 'n'),
    119        [0, 1, 2, 'n'],
    120        argument_2={0, 1, 2, 'n'},
    121    )
    122    # function_07(
    123    src.type_error.function_07(
    124        argument_n={'key': 'value'},
    125        argument_2={0, 1, 2, 'n'},
    126        argument_0=(0, 1, 2, 'n'),
    127        argument_1=[0, 1, 2, 'n'],
    128    )
    

    still green.

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

    122    # function_07(
    123    src.type_error.function_07(
    124        argument_n={'key': 'value'},
    125        argument_2={0, 1, 2, 'n'},
    126        argument_0=(0, 1, 2, 'n'),
    127        argument_1=[0, 1, 2, 'n'],
    128    )
    129    # function_08('positional', argument='keyword')
    130    src.type_error.function_08(
    131        'positional', argument='keyword'
    132    )
    133
    134
    135# 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
     7
     8
     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
    19
    20
    21def function_04(argument):
    22    return None
    23
    24
    25def function_05(argument_0, argument_1):
    26    return None
    27
    28
    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

    def test_type_error_w_args_and_kwargs():
        src.type_error.function_00('argument')
        src.type_error.function_01(1, 0)
        src.type_error.function_02(
            third=True, second=False, first=None
        )
        src.type_error.function_03(
            second=[0, 1, 2, 'n'],
            first=(0, 1, 2, 'n'),
            third={0, 1, 2, 'n'},
            fourth={'key': 'value'}
        )
        src.type_error.function_04('value')
        src.type_error.function_05(
            (0, 1, 2, 'n'),
            [0, 1, 2, 'n'],
        )
        src.type_error.function_06(
            (0, 1, 2, 'n'),
            [0, 1, 2, 'n'],
            argument_2={0, 1, 2, 'n'},
        )
        src.type_error.function_07(
            argument_n={'key': 'value'},
            argument_2={0, 1, 2, 'n'},
            argument_0=(0, 1, 2, 'n'),
            argument_1=[0, 1, 2, 'n'],
        )
        src.type_error.function_08(
            'positional', argument='keyword'
        )
    
    
    # Exceptions seen
    # AssertionError
    # NameError
    # TypeError
    # ModuleNotFoundError
    # AttributeError
    
  • I remove functions_00 to functions_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 functions_00 to functions_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.py
        └── def function_name(): return None
    

    where function_name is the name of the function.

  • I add a git commit message in the other terminal

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

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


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?

so far you know

Would you like to test using a class to remove repetition of inputs from functions?


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.