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
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')
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 )
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
I open a terminal
I change directory to the type_error folder in the
pumping_pythonfolderthe terminal shows I am in the type_error folder
.../pumping_python/type_errorI open
test_type_error.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
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 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.py1import src.type_error 2 3 4def function_00(the_input): 5 return Nonethe terminal is my friend, and shows AttributeError
AttributeError: module 'src.type_error' has no attribute 'function_00'because there is nothing in the
type_errormodule in thesrcfolder with the namefunction_00.I add AttributeError to the list of Exceptions seen
I open
__init__.pyfrom thetype_errorfolder from thesrcfolderI delete the text in the file then add a definition for
function_00tosrc/type_error/__init__.py1def function_00(): 2 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_00() takes 0 positional arguments but 1 was givenI add a name to the parentheses
1# def function_00(): 2def function_00(one): 3 return Nonethe test passes because
Python brings in an object for the
src/type_error/__init__.pyfile from thesrcfolder so I can use it intests/test_type_error.pyassrc.type_errorwhenimport src.type_errorruns.Python calls the
function_00function from the object it imported for thesrc/type_error/__init__.pyfile from thesrcfolder (src.type_error) whensrc.type_error.function_00is called.
I think of
src.type_error.function_00like an addresssrc.type_error.function_00 src/ └── type_error/ └── __init__.py └── def function_00(one): └── return Nonefunction_00is something intype_error, in this case it is a function in__init__.pyin thetype_errorfolder.type_erroris something insrc, in this case it is a folder in thesrcfolder.srcis something Python can import (a module, Python package or folder).function_00is now an attribute/property ofsrc/type_error/__init__.py. I can use it from outside the file withsrc.type_error.function_00.
REFACTOR: make it better
I change the call to
function_01in test_type_error_w_positional_arguments to be a call tofunction_01ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_01tosrc/type_error/__init__.py1# def function_00(): 2def function_00(one): 3 return None 4 5 6def function_01(): 7 return Nonethe terminal is my friend, and shows TypeError
TypeError: function_01() takes 0 positional arguments but 2 were givenI 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 Nonethe test passes because Python follows this path when
src.type_error.function_01is calledsrc.type_error.function_01 src/ └── type_error/ └── __init__.py └── def function_01(one, two): └── return NoneI change the call to
function_02in test_type_error_w_positional_arguments to be a call tofunction_02ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_02tosrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_02() takes 0 positional arguments but 3 were givenI add three names to the parentheses
the test passes because Python follows this path when
src.type_error.function_02is calledsrc.type_error.function_02 src/ └── type_error/ └── __init__.py └── def function_02(one, two, three): └── return NoneI change the call to
function_03in test_type_error_w_positional_arguments to be a call tofunction_03ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_03tosrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_03() takes 0 positional arguments but 4 were givenI add four names to the parentheses
the test passes because Python follows this path when
src.type_error.function_03is calledsrc.type_error.function_03 src/ └── type_error/ └── __init__.py └── def function_03(one, two, three, four): └── return NoneI change the call to
function_04in test_type_error_w_positional_arguments to be a call tofunction_04ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_04tosrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_04() takes 0 positional arguments but 1 was givenI add
oneto the parenthesesthe test passes because Python follows this path when
src.type_error.function_04is calledsrc.type_error.function_04 src/ └── type_error/ └── __init__.py └── def function_04(one): └── return NoneI change the call to
function_05in test_type_error_w_positional_arguments to be a call tofunction_05ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_05tosrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_05() takes 0 positional arguments but 2 were givenI add two names to the parentheses
the test passes because Python follows this path when
src.type_error.function_05is calledsrc.type_error.function_05 src/ └── type_error/ └── __init__.py └── def function_05(one, two): └── return NoneI change the call to
function_06in test_type_error_w_positional_arguments to be a call tofunction_06ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_06tosrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_06() takes 0 positional arguments but 3 were givenI add three names to the parentheses
the test passes because Python follows this path when
src.type_error.function_06is calledsrc.type_error.function_06 src/ └── type_error/ └── __init__.py └── def function_06(one, two, three): └── return NoneI change the call to
function_07in test_type_error_w_positional_arguments to be a call tofunction_07ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_07tosrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_07() takes 0 positional arguments but 4 were givenI add four names to the parentheses
the test passes because Python follows this path when
src.type_error.function_07is calledsrc.type_error.function_07 src/ └── type_error/ └── __init__.py └── def function_07(one, two, three, four): └── return NoneI change the call to
function_08in test_type_error_w_positional_arguments to be a call tofunction_08ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_08tosrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_08() takes 0 positional arguments but 2 were givenI add two names to the parentheses
the test passes because Python follows this path when
src.type_error.function_08is calledsrc.type_error.function_08 src/ └── type_error/ └── __init__.py └── def function_08(one, two): └── return NoneI remove the commented lines from test_type_error_w_positional_arguments in
tests/test_type_error.py
I change the call to
function_00in test_type_error_w_keyword_arguments to be a call tofunction_00ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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_inputto the parentheses of the definition offunction_00insrc/type_error/__init__.py1# def function_00(): 2# def function_00(one): 3def function_00(one, the_input): 4 return Nonethe 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
onefrom the parentheses1# 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_01in test_type_error_w_keyword_arguments to be a call tofunction_01ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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
firstto the parentheses of the definition offunction_01insrc/type_error/__init__.pythe 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
secondto the parenthesesthe 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
oneandtwofrom the parenthesesthe test passes.
I change the call to
function_02in test_type_error_w_keyword_arguments to be a call tofunction_02ofsrc/type_error/__init__.py, intests/test_type_error.pythe terminal is my friend, and shows TypeError
TypeError: function_02() got an unexpected keyword argument 'third'I add
thirdto the parentheses of the definition offunction_02insrc/type_error/__init__.pythe 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
secondto the parenthesesthe 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
firstto the parenthesesthe 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,twoandthreefrom the parenthesesthe test passes.
I change the call to
function_03in test_type_error_w_keyword_arguments to be a call tofunction_03ofsrc/type_error/__init__.py, intests/test_type_error.pythe terminal is my friend, and shows TypeError
TypeError: function_03() got an unexpected keyword argument 'first'because …
I add
firstto the parentheses of the definition offunction_03insrc/type_error/__init__.pythe 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
secondto the parenthesesthe 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
thirdto the parenthesesthe 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
fourthto the parenthesesthe 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,threeandfourfrom the parenthesesthe test passes.
I change the call to
function_04in test_type_error_w_keyword_arguments to be a call tofunction_04ofsrc/type_error/__init__.py, intests/test_type_error.pythe terminal is my friend, and shows TypeError
TypeError: function_04() got an unexpected keyword argument 'argument'I change
onetoargumentin the parentheses of the definition offunction_04insrc/type_error/__init__.pythe test passes.
I change the call to
function_05in test_type_error_w_keyword_arguments to be a call tofunction_05ofsrc/type_error/__init__.py, intests/test_type_error.pythe terminal is my friend, and shows TypeError
TypeError: function_05() got an unexpected keyword argument 'argument_0'I change
onetoargument_0in the parentheses of the definition offunction_05insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_05() got an unexpected keyword argument 'argument_1'. Did you mean 'argument_0'?I change
twotoargument_1in the parenthesesthe test passes.
I change the call to
function_06in test_type_error_w_keyword_arguments to be a call tofunction_06ofsrc/type_error/__init__.py, intests/test_type_error.pythe terminal is my friend, and shows TypeError
TypeError: function_06() got an unexpected keyword argument 'argument_0'I change
onetoargument_0in the parentheses of the definition offunction_06insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_06() got an unexpected keyword argument 'argument_1'. Did you mean 'argument_0'?I change
twotoargument_1in the parenthesesthe terminal is my friend, and shows TypeError
TypeError: function_06() got an unexpected keyword argument 'argument_2'. Did you mean 'argument_0'?I change
threetoargument_2in the parenthesesthe test passes.
I change the call to
function_07in test_type_error_w_keyword_arguments to be a call tofunction_07ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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
onetoargument_0in the parentheses of the definition offunction_07insrc/type_error/__init__.pythe 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
twotoargument_1in the parenthesesthe 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
threetoargument_2in the parenthesesthe terminal is my friend, and shows TypeError
TypeError: function_07() got an unexpected keyword argument 'argument_n'. Did you mean 'argument_0'?I change
fourtoargument_nin the parenthesesthe test passes.
I change the call to
function_08in test_type_error_w_keyword_arguments to be a call tofunction_08ofsrc/type_error/__init__.py, intests/test_type_error.pythe 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
onetoargumentin the parentheses of the definition offunction_08insrc/type_error/__init__.pythe terminal is my friend, and shows TypeError
TypeError: function_08() got an unexpected keyword argument 'name'.I change
twotonamein the parenthesesthe 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_00in test_type_error_w_args_and_kwargs to be a call tofunction_00ofsrc/type_error/__init__.py, intests/test_type_error.pythe test is still green.
I change the call to
function_01tosrc.type_error.function_01still green.
I change the call to
function_02tosrc.type_error.function_02green.
I change the call to
function_03tosrc.type_error.function_03still green.
I change the call to
function_04tosrc.type_error.function_04the test is still green.
I change the call to
function_05tosrc.type_error.function_05still green.
I change the call to
function_06tosrc.type_error.function_06green.
I change the call to
function_07tosrc.type_error.function_07still green.
I change the call to
function_08tosrc.type_error.function_08the 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_08insrc/type_error/__init__.pythe test passes.
I remove the commented lines from
src/type_error/__init__.py1def 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 NoneI remove the commented lines from test_type_error_w_args_and_kwargs in
tests/test_type_error.pyI remove
function_00tofunction_08fromtests/test_type_error.pybecause they are no longer called1import src.type_error 2 3 4def test_type_error_w_positional_arguments():all the tests are passing because
function_00tofunction_08are now attributes ofsrc/type_error/__init__.pyand I can call them from outside the file withsrc.type_error.function_name()src.type_error.function_name() src/ └── type_error/ └── __init__.py └── def function_name(): return Nonewhere
function_nameis the name of the function.I open a terminal then change directory to the type_error folder
close the project
I close
tests/test_type_error.pyandsrc/type_error/__init__.pyI 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_errorthe terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
code from the 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.