TypeError



The TypeError is raised when an object is used in a way that it should not be.

test_type_error_w_non_callables

There are objects that cannot be called

red: make it fail

  • I open a terminal to run makePythonTdd.sh with type_error as the name of the project

    ./makePythonTdd.sh type_error
    

    on Windows without Windows Subsystem Linux use makePythonTdd.ps1

    ./makePythonTdd.ps1 type_error
    

    it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError

    E       AssertionError: True is not false
    
    tests/test_type_error.py:7: AssertionError
    
  • I hold ctrl (windows/linux) or option (mac) on the keyboard and use the mouse to click on tests/test_type_error.py:7 to open it in the editor

  • then change True to False

  • I add an import statement

    import unittest
    import src.type_error
    
  • and change test_failure to test_type_error_w_non_callables

    class TestTypeError(unittest.TestCase):
    
        def test_type_error_w_non_callables(self):
            src.type_error.none()
    

    the terminal shows AttributeError

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

    I add it to the list of Exceptions encountered

    # Exceptions Encountered
    # AssertionError
    # AttributeError
    

green: make it pass

  • then I add the name to type_error.py and point it to None

    none = None
    

    the terminal shows TypeError

    TypeError: 'NoneType' object is not callable
    

    the () to the right of src.type_error.none makes it a call, and the name none is a reference to None which is not callable

  • I add the error to the list of Exceptions encountered

    # Exceptions Encountered
    # AssertionError
    # AttributeError
    # TypeError
    
  • and I make none a function to make it callable

    def none():
        return None
    

    the test passes

refactor: make it better

  • I add another line to the test

    def test_type_error_w_non_callables(self):
        src.type_error.none()
        src.type_error.false()
    

    and the terminal shows AttributeError

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

    when I add the name to type_error.py and point it to False

    def none():
        return None
    
    
    false = False
    

    the terminal shows TypeError

    TypeError: 'bool' object is not callable
    

    I make it a function

    def none():
        return None
    
    def false():
        return False
    

    and the terminal shows green again

  • I add a line to test the other boolean

    def test_type_error_w_non_callables(self):
        src.type_error.none()
        src.type_error.false()
        src.type_error.true()
    

    the terminal shows AttributeError

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

    I add the name to type_error.py and point it to True

    def false():
        return False
    
    
    true = True
    

    and get TypeError

    TypeError: 'bool' object is not callable
    

    when I make it a function

    def false():
        return False
    
    
    def true():
        return True
    

    the test passes

  • I add another line to the test

    def test_type_error_w_non_callables(self):
        src.type_error.none()
        src.type_error.false()
        src.type_error.true()
        src.type_error.a_list()
    

    and the terminal shows AttributeError

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

    I add the name and point it to a list

    def true():
        return True
    
    
    a_list = [1, 2, 3, 'n']
    

    and the terminal shows TypeError

    TypeError: 'list' object is not callable
    

    then I make it a function

    def true():
        return True
    
    
    def a_list():
        return [1, 2, 3, 'n']
    

    and the test passes

  • I add a new failing line

    def test_type_error_w_non_callables(self):
        src.type_error.none()
        src.type_error.false()
        src.type_error.true()
        src.type_error.a_list()
        src.type_error.a_dictionary()
    

    the terminal shows AttributeError

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

    I add the name and point it to a dictionary

    def a_list():
        return [1, 2, 3, 'n']
    
    
    a_dictionary = {'key': 'value'}
    

    and the terminal shows TypeError

    TypeError: 'dict' object is not callable
    

    then I change it to a function

    def a_list():
        return [1, 2, 3, 'n']
    
    
    def a_dictionary():
        return {'key': 'value'}
    

    and the terminal shows green again. It is safe to say that I cannot call data structures.


test_type_error_w_function_signatures

Calls to a function have to match its signature

red: make it fail

  • I add a new test

    def test_type_error_w_function_signatures(self):
        src.type_error.function_00('a')
    

    the terminal shows AttributeError

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

    then I add the function to type_error.py

    def a_dictionary():
        return {'key': 'value'}
    
    
    def function_00():
        return None
    

    and get TypeError

    TypeError: function_00() takes 0 positional arguments but 1 was given
    

    because function_00 is called with 'a' as input but the definition does not accept any inputs

green: make it pass

  • I add an input parameter to the function definition

    def function_00(argument):
        return None
    

    and the terminal shows passing tests

refactor: make it better

  • I add a new failing line

    def test_type_error_w_function_signatures(self):
        src.type_error.function_00('a')
        src.type_error.function_01('a', 'b')
    

    the terminal shows AttributeError

    AttributeError: module 'src.type_error' has no attribute 'function_01'. Did you mean: 'function_00'?
    

    I add the function

    def function_00(argument):
        return None
    
    
    def function_01(argument):
        return None
    

    and the terminal shows TypeError

    TypeError: function_01() takes 1 positional argument but 2 were given
    

    when I make the number of inputs in the definition match the number of inputs in the call

    def function_01(
          argument_1, argument_2
      ):
      return None
    

    the test passes

  • I add another failing line

    def test_type_error_w_function_signatures(self):
        src.type_error.function_00('a')
        src.type_error.function_01('a', 'b')
        src.type_error.function_02('a', 'b', 'c')
    

    the terminal shows AttributeError

    AttributeError: module 'src.type_error' has no attribute 'function_02'. Did you mean: 'function_00'?
    

    I add the function to type_error.py

    def function_01(
            argument_1, argument_2
        ):
        return None
    
    
    def function_02(
            argument_1, argument_2
        ):
        return None
    

    and the terminal shows TypeError

    TypeError: function_02() takes 2 positional arguments but 3 were given
    

    then I make the number of inputs match

    def function_02(
            argument_1, argument_2,
            argument_3
        ):
        return None
    

    and the terminal shows green again

  • I add one more failing line to the test

    def test_type_error_w_function_signatures(self):
        src.type_error.function_00('a')
        src.type_error.function_01('a', 'b')
        src.type_error.function_02('a', 'b', 'c')
        src.type_error.function_03('a', 'b', 'c', 'd')
    

    the terminal shows AttributeError

    AttributeError: module 'src.type_error' has no attribute 'function_03'. Did you mean: 'function_00'?
    

    I add the function

    def function_02(
            argument_1, argument_2,
            argument_3
        ):
        return None
    
    
    def function_03(
            argument_1, argument_2,
            argument_3
        ):
        return None
    

    and get TypeError

    TypeError: function_03() takes 3 positional arguments but 4 were given
    

    I add a 4th parameter to the definition

    def function_03(
        argument_1, argument_2,
        argument_3, argument_4
    ):
        return None
    

    and the terminal shows both tests are passing.


test_type_error_w_objects_that_do_not_mix

Some operations do not work if the objects are not the same type

red: make it fail

I add a new test with a failing line

def test_type_error_w_objects_that_do_not_mix(self):
    None + 1

the terminal shows TypeError

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

I cannot do arithmetic with None

green: make it pass

I add the assertRaises method

def test_type_error_w_objects_that_do_not_mix(self):
    with self.assertRaises(TypeError):
        None + 1

and the test passes

refactor: make it better

  • I add another line

    def test_type_error_w_objects_that_do_not_mix(self):
        with self.assertRaises(TypeError):
            None + 1
        'text' + 0.1
    

    which gives me TypeError

    TypeError: can only concatenate str (not "float") to str
    

    I cannot add something that is not a string to a string. I add assertRaises

    def test_type_error_w_objects_that_do_not_mix(self):
        with self.assertRaises(TypeError):
            None + 1
        with self.assertRaises(TypeError):
            'text' + 0.1
    

    and the test passes

  • then I add one more line

    def test_type_error_w_objects_that_do_not_mix(self):
        with self.assertRaises(TypeError):
            None + 1
        with self.assertRaises(TypeError):
            'text' + 0.1
        (1, 2, 3, 'n') - {1, 2, 3, 'n'}
    

    the terminal shows TypeError

    TypeError: unsupported operand type(s) for -: 'tuple' and 'set'
    

    I add assertRaises

    def test_type_error_w_objects_that_do_not_mix(self):
        with self.assertRaises(TypeError):
            None + 1
        with self.assertRaises(TypeError):
            'text' + 0.1
        with self.assertRaises(TypeError):
            (1, 2, 3, 'n') - {1, 2, 3, 'n'}
    

    and the terminal shows all tests are passing.


review

I ran tests for TypeError with objects that are not callable, function signatures and objects that do not mix. Would you like to test data structures?


Type Error: tests and solution