TypeError: tests and solution


what causes TypeError?: tests

the code in type_error/tests/test_type_error.py from what causes TypeError?

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

separate and equal TypeError: tests and solutions

separate and equal TypeError tests


the code in type_error/tests/test_type_error.py from separate and equal TypeError

 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

separate and equal TypeError solutions


the code in type_error/src/type_error.py from separate and equal TypeError

 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

TypeError with classes: tests and solutions

TypeError with classes tests


the code in type_error/tests/test_type_error.py from TypeError with classes

  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
 90def test_type_error_w_class_methods():
 91    src.type_error.AClass.method_00()
 92    src.type_error.AClass().method_01()
 93    src.type_error.AClass().method_02()
 94    src.type_error.AClass.method_03()
 95    src.type_error.AClass.method_04()
 96    src.type_error.AClass().method_05()
 97    src.type_error.AClass().method_06()
 98    src.type_error.AClass().method_07()
 99    src.type_error.AClass().method_08()
100    src.type_error.AClass().method_09()
101
102
103def test_type_error_w_the_uncallables():
104    src.type_error.none()
105    src.type_error.false()
106    src.type_error.true()
107    src.type_error.an_integer()
108    src.type_error.a_float()
109    src.type_error.a_string()
110    src.type_error.a_tuple()
111    src.type_error.a_list()
112    src.type_error.a_set()
113    src.type_error.a_dictionary()
114
115
116# Exceptions seen
117# AssertionError
118# NameError
119# TypeError
120# ModuleNotFoundError
121# AttributeError

TypeError with classes solutions


the code in type_error/src/type_error.py from TypeError with classes

 1def none(): return None
 2def false(): return False
 3def true(): return True
 4def an_integer(): return 1234
 5def a_float(): return 5.678
 6def a_string(): return 'a string'
 7def a_tuple(): return (0, 1, 2, 'n')
 8def a_list(): return [0, 1, 2, 'n']
 9def a_set(): return {0, 1, 2, 'n'}
10def a_dictionary(): return {'key': 'value'}
11
12
13def function_00(the_input):
14    return None
15
16
17def function_01(first, second):
18    return None
19
20
21def function_02(
22    third, second, first
23):
24    return None
25
26
27def function_03(
28    first, second, third, fourth,
29):
30    return None
31
32
33def function_04(argument):
34    return None
35
36
37def function_05(argument_0, argument_1):
38    return None
39
40
41def function_06(argument_0, argument_1, argument_2):
42    return None
43
44
45def function_07(
46    argument_0, argument_1,
47    argument_2, argument_n
48):
49    return None
50
51
52def function_08(name, argument):
53    return None
54
55
56class AClass(object):
57
58    @staticmethod
59    def method_00(): return None
60
61    @staticmethod
62    def method_01(): return None
63
64    def method_02(self):
65        return self.method_01()
66
67    @staticmethod
68    def method_03():
69        return AClass().method_02()
70
71    @staticmethod
72    def method_04():
73        return AClass.method_02(AClass)
74
75    def method_05(self):
76        return self.method_02()
77
78    def method_06(self):
79        return self.method_01()
80
81    def method_07(self):
82        return self.method_00()
83
84    def method_08(self):
85        return self.method_04()
86
87    def method_09(self):
88        return self.method_03()