what causes TypeError?


TypeError happens when an object (everything in Python is an object) is used in a way that it should not be.


preview

I have these tests by the end of the chapter

type_error/tests/test_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(first, second, third):
10    return None
11
12
13def function_03(first, second, third, fourth):
14    return None
type_error/tests/test_type_error.py
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
type_error/tests/test_type_error.py
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')
type_error/tests/test_type_error.py
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    )
type_error/tests/test_type_error.py
 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(argument='value_2')
108    function_05((0, 1, 2, 'n'), [0, 1, 2, 'n'])
109    function_06(
110        argument_2=(0, 1, 2, 'n'),
111        argument_0=[0, 1, 2, 'n'],
112        argument_1={0, 1, 2, 'n'},
113    )
114    function_07(
115        argument_n={'key': 'value'},
116        argument_2={0, 1, 2, 'n'},
117        argument_0=(0, 1, 2, 'n'),
118        argument_1=[0, 1, 2, 'n'],
119    )
120    function_08(
121        'positional',
122        argument='keyword',
123    )
124
125
126# Exceptions seen
127# AssertionError
128# NameError
129# TypeError

questions about TypeError


start the project

  • I name this project type_error

  • I open a terminal

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

    terminal
    cd type_error
    

    the terminal shows

    cd: no such file or directory: type_error
    

    there is no folder with the name type_error in this folder.

  • I use uv to make a directory for the project and initialize it

    terminal
    uv init type_error
    

    the terminal shows

    Initialized project `type-error`
    at `../pumping_python/type_error`
    

    then goes back to the command line.

  • I change directory to the project

    terminal
    cd type_error
    

    the terminal shows I am in the type_error folder

    .../pumping_python/type_error
    
  • I make a directory for the tests

    terminal
    mkdir tests
    

    the terminal goes back to the command line.

  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    
    New-Item tests/__init__.py
    

    the terminal goes back to the command line.

  • I make an empty file for the tests in the tests folder

    touch tests/test_type_error.py
    
    New-Item tests/test_type_error.py
    

    the terminal goes back to the command line.

  • I open test_type_error.py

  • I add the first failing test to test_type_error.py

    1def test_failure():
    2    assert False is True
    
  • I go back to the terminal

  • I use echo to add pytest to requirements.txt

    echo "pytest" > requirements.txt
    
  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line.

  • I use Out-File to add pytest to requirements.txt

    "pytest" | Out-File requirements.txt -Encoding UTF8
    
  • I add pytest-watcher to the requirements file

    "pytest-watcher" >> requirements.txt
    
  • I use uv to install pytest-watcher with the requirements file

    terminal
    uv add --requirement requirements.txt
    

    the terminal shows that it installed pytest-watcher and its dependencies.

  • I add the new files and folder to git for tracking

    terminal
    git add .
    

    the terminal goes back to the command line.

  • I add a git commit message

    terminal
    git commit --all --message 'setup project'
    

    the terminal shows a summary of the changes then goes back to the command line.

  • I use pytest-watcher to run the tests automatically

    terminal
    uv run pytest-watcher . --now --patterns "**/*.py"
    

    the terminal is my friend, and shows AssertionError

    ========================== ERRORS ==========================
    ______ ERROR collecting tests/test_type_error.py ______
    tests/test_type_error.py:2: in <module>
        assert False is True
    E   assert False is True
    ================= short test summary info ==================
    ERROR tests/test_type_error.py - assert False is True
    !!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!
    ===================== 1 error in L.MNs =====================
    

    because False is NOT True.

    if the terminal does not show the same error, then check if

    • your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    and try uv run pytest-watcher . --now --patterns "**/*.py" again

  • I add AssertionError to the list of Exceptions seen, in test_type_error.py

    1def test_failure():
    2    assert False is True
    3
    4
    5# Exceptions seen
    6# AssertionError
    
  • I change False to True in the assertion

    1def test_failure():
    2    # assert False is True
    3    assert True is True
    4
    5
    6# Exceptions seen
    7# AssertionError
    

    the test passes.


test_type_error_w_positional_arguments

TypeError happens when I call a function in a way that is different from its signature. I have to match the number of arguments in the function definition when I call the function with positional arguments.


RED: make it fail


  • I change test_failure to test_type_error_w_positional_arguments

    1def test_type_error_w_positional_arguments():
    2    function_00('a')
    3
    4
    5# Exceptions seen
    6# AssertionError
    

    the terminal is my friend, and shows NameError

    NameError: name 'function_00' is not defined
    

    because there is nothing named function_00 in test_type_error.py.

  • I add NameError to the list of Exceptions seen


GREEN: make it pass



REFACTOR: make it better


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


test_type_error_w_keyword_arguments

TypeError happens when I call a function in a way that is different from its signature. I have to match the names of arguments in the function definition when I call the function with keyword arguments.


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with a call to function_04 with a keyword argument

    the terminal is my friend, and shows NameError

    NameError: name 'function_04' is not defined
    

    because there is nothing named function_04 in test_type_error.py.


GREEN: make it pass



REFACTOR: make it better


If I call a function with keyword arguments, I have to use the same names that are in its definition.


test_type_error_w_args_and_kwargs

TypeError happens when I call a function in a way that is different from its signature. I cannot give two values for the same argument in the function definition when I call a function.


RED: make it fail



GREEN: make it pass



REFACTOR: make it better



test TypeError with required and unexpected arguments


RED: make it fail



GREEN: make it pass


I add fourth with a value to the call

46def test_type_error_w_keyword_arguments():
47    function_03(
48        first=None,
49        second=False,
50        third=True,
51        fourth=4,
52    )
53    function_04(argument='value')

the test passes because the call to the function matches its definition.


REFACTOR: make it better



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


close the project

  • I close test_type_error.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal goes back to the command line.

  • I change directory to the parent of type_error

    terminal
    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I ran tests for TypeError with

My problem with the tests is that they all show the correct way to call the functions I made in the file. If someone reads the file or runs it, there is no way for them to know how any of the calls are related to TypeError unless they go through the process with me, there has to be a better way.


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

Would you like to test AssertionError with functions that take input?


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.