separate and equal TypeError


I want to move the functions from tests/test_type_error.py to src/type_error/__init__.py 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')
type_error/tests/test_type_error.py
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    )
type_error/tests/test_type_error.py
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(argument='value_2')
69    src.type_error.function_05(
70        (0, 1, 2, 'n'), [0, 1, 2, 'n']
71    )
72    src.type_error.function_06(
73        argument_2=(0, 1, 2, 'n'),
74        argument_0=[0, 1, 2, 'n'],
75        argument_1={0, 1, 2, 'n'},
76    )
77    src.type_error.function_07(
78        argument_n={'key': 'value'},
79        argument_2={0, 1, 2, 'n'},
80        argument_0=(0, 1, 2, 'n'),
81        argument_1=[0, 1, 2, 'n'],
82    )
83    src.type_error.function_08(
84        'positional',
85        argument='keyword',
86    )
87
88
89# Exceptions seen
90# AssertionError
91# NameError
92# TypeError
93# 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 the type_error module 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 tests/test_type_error.py.


GREEN: make it pass


  • I add an import statement at the top of tests/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 AttributeError

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

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

  • I add AttributeError to the list of Exceptions seen

  • I open __init__.py from the type_error folder from the src folder

  • I delete the text in the file then add a definition for function_00 to src/type_error/__init__.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 src/type_error/__init__.py file from the src folder so I can use it in tests/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 src/type_error/__init__.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/
        └── __init__.py
            └── def function_00(one):
                └── return None
    
    • function_00 is something in type_error, in this case it is a function in __init__.py in the type_error folder.

    • type_error is something in src, in this case it is a folder in the src folder.

    • src is something Python can import (a module, Python package or folder).

    • function_00 is now an attribute/property of src/type_error/__init__.py. I can use it from outside the file with src.type_error.function_00.


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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.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/
        └── __init__.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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

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

    src.type_error.function_02
    src/
    └── type_error/
        └── __init__.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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

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

    src.type_error.function_03
    src/
    └── type_error/
        └── __init__.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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

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

    src.type_error.function_04
    src/
    └── type_error/
        └── __init__.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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

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

    src.type_error.function_05
    src/
    └── type_error/
        └── __init__.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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

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

    src.type_error.function_06
    src/
    └── type_error/
        └── __init__.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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

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

    src.type_error.function_07
    src/
    └── type_error/
        └── __init__.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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

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

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


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

    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 src/type_error/__init__.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
    6
    7
    8# def function_01():
    

    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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

    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

    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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

    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

    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

    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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

    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

    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

    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

    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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

    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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

    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

    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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

    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

    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

    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 src/type_error/__init__.py, in tests/test_type_error.py

    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 src/type_error/__init__.py

    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

    the test passes.

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


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

    the test is still green.

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

    still green.

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

    green.

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

    still green.

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

    the test is still green.

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

    still green.

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

    green.

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

    still green.

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

    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 src/type_error/__init__.py

    the test passes.

  • I remove the commented lines from src/type_error/__init__.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
    
  • I remove the commented lines from test_type_error_w_args_and_kwargs in tests/test_type_error.py

  • I remove function_00 to function_08 from tests/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 src/type_error/__init__.py and I can call them from outside the file with src.type_error.function_name()

    src.type_error.function_name()
    src/
    └── type_error/
        └── __init__.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

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

    terminal
    git commit --all --message \
    'move functions to src'
    

close the project

  • I close tests/test_type_error.py and src/type_error/__init__.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

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

AttributeError happened when separating the solutions from the tests, 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.