TypeError with assertRaises
Since methods are functions in an object I can assume that they have the same behavior as what I tested in test_type_error_w_positional_arguments, test_type_error_w_keyword_arguments and test_type_error_w_args_and_kwargs.
preview
I have these tests by the end of the chapter
questions about TypeError with objects
open the project
I open a terminal
I change directory to the type_error folder in the
pumping_pythonfolderI open
test_type_error.pyI use pytest-watcher to run the tests automatically
the terminal shows
test_type_error.py ... [100%] =================== 3 passed in G.HIs ====================
test_type_error_w_object_methods
RED: make it fail
I add a test with a call to AnObject.method from test_type_error.py
84 src.type_error.function_08(
85 'positional',
86 argument='keyword',
87 )
88
89
90 def test_type_error_w_object_methods():
91 src.type_error.AnObject.method_00()
92
93
94 # Exceptions seen
the terminal is my friend, and shows AttributeError
because AnObject is not defined in type_error.py.
GREEN: make it pass
I open
type_error/__init__.pyfrom thesrcfolderI add an object definition for
AnObjecttotype_error.pythe terminal is my friend, and shows AttributeError
because there is nothing named
method_00inAnObject.I add the name to the object definition
I define
method_00by pointing it to Nonethe terminal is my friend, and shows TypeError
because
method_00points to None and I cannot call None like a function.I change
method_00to a methodthe test passes.
REFACTOR: make it better
I remove the commented lines
I add a call to
src.type_error.AnObject().method_01fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: ‘AnObject’ object
has no attribute ‘method_01’. Did you mean: ‘method_00’?
I add a method definition for
method_01toAnObjectintype_error.pythe terminal is my friend, and shows TypeError
because a method of an instance takes the instance of the object (
self) it belongs to as the first argument.I add @staticmethod to
method_01class AnObject(object): def method_00(): return None @staticmethod def method_01(): return Nonethe test passes because I can use the staticmethod decorator if I do not want to add
selfto the method definition whenit does not use anything that belongs to the object.Both methods look the same. The difference is in how I call them
AnObject.method_00()vsAnObject().method_01().I add a call to
src.type_error.AnObject().method_02fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: ‘AnObject’ object
has no attribute ‘method_02’. Did you mean: ‘method_00’?
I add a definition for
method_02toAnObjectintype_error.pythe terminal is my friend, and shows TypeError
because a method of an instance takes the instance of the object (
self) it belongs to as the first argument.I add @staticmethod to
method_02I add
selfto the parentheses ofmethod_02I comment out the staticmethod decorator
the test passes because a method of an instance takes the instance of the object (
self) it belongs to as the first argument which allows it to use things that belong to the object.I add a call to
src.type_error.AnObject.method_03fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_03’. Did you mean: ‘method_00’?
I add a definition for
method_03toAnObjectintype_error.pyI add
AnObject.beforemethod_02the terminal is my friend, and shows TypeError
because
method_02takes an instance of the object it belongs to as input.I called the method with the object not an instance of the object.
I use an instance of the object to call the method
the test passes. This is a silly example because I used
AnObject()inside a method ofAnObject. I could just useself. I would only need this if I was calling a method of a different object.Here is another silly example. I add a call to
src.type_error.AnObject.method_04fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_04’. Did you mean: ‘method_00’?
I add a method definition for
method_04toAnObjectintype_error.pythe terminal is my friend, and shows TypeError
because
method_02takes an instance of the object it belongs to as input.I called the method with the object not an instance of the object.
I pass
AnObjectas input in the call tomethod_02the test passes. I called a method of
AnObjectand passedAnObjectas input. I can useself.I add a call to
src.type_error.AnObject().method_05fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: ‘AnObject’ object
has no attribute ‘method_05’. Did you mean: ‘method_00’?
I add a method definition for
method_05toAnObjectintype_error.pythe terminal is my friend, and shows TypeError
because a method of an instance takes the instance of the object (
self) it belongs to as the first argument.I add
selfto the parentheses ofmethod_05the terminal is my friend, and shows NameError
because there is no
method_02at the module level oftype_error.py. It is insideAnObjectintype_error.py, I have to be specific.I add
self.beforemethod_02the test passes.
I add a call to
src.type_error.AnObject().method_06fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: ‘AnObject’ object
has no attribute ‘method_06’. Did you mean: ‘method_00’?
I add a method definition for
method_06toAnObjectthe terminal is my friend, and shows TypeError
because a method of an instance takes the instance of the object (
self) it belongs to as the first argument.I add
selfto the parentheses ofmethod_06the test passes.
I add a call to
src.type_error.AnObject().method_07fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_07’. Did you mean: ‘method_00’?
I add a method definition for
method_07toAnObjectintype_error.pythe terminal is my friend, and shows TypeError
because
method_07takes an instance of the object it belongs to as input.I called the method with the object not an instance of the object.
I use an instance of the object to call the method from
test_type_error.pythe terminal is my friend, and shows TypeError
because
method_00takes no input (the parentheses are empty).I called it with an instance of the object (
AnObject()) which passes the instance as input.
I add the staticmethod decorator to
method_00ofAnObjectintype_error.pythe test passes. I can use the staticmethod decorator if I do not want to add
selfto the method definition whenit does not use anything that belongs to the object.I add a call to
src.type_error.AnObject.method_08()fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: type object ‘AnObject’
has no attribute ‘method_08’. Did you mean: ‘method_00’?
I add a method definition for
method_08toAnObjectintype_error.pythe terminal is my friend, and shows TypeError
because I called
method_08like a staticmethod and it is defined as an instance method (It expectsselfas input).I use an instance of the object to call the method from
test_type_error.pybecause
method_04takes no input (the parentheses are empty).I called it with an instance of the object (
AnObject()) which passes the instance as input.
I add the staticmethod decorator to
method_04ofAnObjectintype_error.pythe test passes.
I add a call to
src.type_error.AnObject.method_09()fromtest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: ‘AnObject’ object
has no attribute ‘method_09’. Did you mean: ‘method_00’?
I add a method definition for
method_09toAnObjectintype_error.pythe terminal is my friend, and shows TypeError
because
method_03takes no input (the parentheses are empty).I called it with an instance of the object (
AnObject()) which passes the instance as input.
I add the @staticmethod to
method_03ofAnObjectintype_error.pythe test passes.
I remove the commented lines from
type_error.pyI remove the commented lines from
test_type_error.pyI open a new terminal then make sure I am in the
type_errorfolder
test_type_error_w_the_uncallables
Is every object callable?
RED: make it fail
I go back to the terminal that is running the tests.
I add a test to
test_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
AttributeError: module ‘src.type_error’ has no attribute ‘none’
there is nothing named
noneintype_error.pyin thesrcfolder yet
GREEN: make it pass
I add
noneand point it to None1none = Nonethe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘NoneType’ object is not callable
the
()to the right ofsrc.type_error.nonemakes it a callthe name
nonepoints to None which is NOT callable. Using substitutionnone = None # point the name to the object none() # call the name None() # substitute the value for the nameNone()raises TypeError because I cannot call None like a function.
I make
nonea function intype_error.pyto make it callable1# none = None 2def none(): return None 3 4 5def function_00(the_input): 6 return Nonethe test passes.
I can call a function, I cannot call None
REFACTOR: make it better
I add a call to
falseintest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘false’
falseis not intype_error.pyI add
falsetotype_error.pyand point it to False1# none = None 2def none(): return None 3false = False 4 5 6def function_00(the_input): 7 return Nonethe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘bool’ object is not callable
falsepoints to False which is NOT callable. Using substitutionfalse = False false() False()False()raises TypeError because I cannot call a boolean like a functionI change
falsefrom a variable to a function to make it callable1# none = None 2def none(): return None 3# false = False 4def false(): return False 5 6 7def function_00(the_input): 8 return Nonethe test is green again.
I add a call to the other boolean in
test_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘true’
there is nothing named
trueintype_error.pyI add
trueand point it to True intype_error.pythe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘bool’ object is not callable
I change
truefrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a boolean or None
I add a call to an integer, in
test_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘an_integer’
I add
an_integerand point it to1234intype_error.pythe terminal is my friend, and shows TypeError
the name
an_integerpoints to an integer (1234) which is NOT callable. Using substitutionan_integer = 1234 # point the name to the object an_integer() # call the name 1234() # substitute the value for the name1234()raises TypeError because I cannot call an integer like a function.I change
an_integerfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call an integer, a boolean or None.
I add a call to a float in
test_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘a_float’
I add
a_floatand point it to5.678intype_error.pythe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘float’ object is not callable
a_floatpoints to a float (5.678) which is NOT callable. Using substitutiona_float = 5.678 a_float() 5.678()5.678()raises TypeError because I cannot call a float like a function.I change
a_floatfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a float, integer, boolean or None.
I add a call to a string (anything in quotes) in
test_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘a_string’
I add
a_stringand point it to'a string'intype_error.pythe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘str’ object is not callable
the name
a_stringpoints to a string ('a string') which is NOT callable. Using substitutiona_string = 'a string' # point the name to the object a_string() # call the name 'a string'() # substitute the value for the name'a string'()raises TypeError because I cannot call a string like a function.I change
a_stringfrom a variable to a function to make it callablethe test passes. I can call a function. I cannot call a string, float, integer, boolean or None.
I add a call to a tuple (anything in parentheses
(), separated by a comma) intest_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘a_tuple’
I add
a_tupleand point it to(0, 1, 2, 'n')intype_error.pythe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘tuple’ object is not callable
the name
a_tuplepoints to a tuple ((0, 1, 2, 'n')) which is NOT callable. Using substitutiona_tuple = (0, 1, 2, 'n') a_tuple() (0, 1, 2, 'n')()(0, 1, 2, 'n')()raises TypeError because I cannot call a tuple like a function.I change
a_tuplefrom a variable to a function to make it callablethe test passes. I can call a function. I cannot call a tuple, string, float, integer, boolean or None.
I add a call to a list in
test_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘a_list’
I add
a_listand point it to[0, 1, 2, 'n']intype_error.pythe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘list’ object is not callable
a_listpoints to a list ([0, 1, 2, 'n']) which is NOT callable. Using substitutiona_list = [0, 1, 2, 'n'] a_list() [0, 1, 2, 'n']()[0, 1, 2, 'n']()raises TypeError because I cannot call a list like a function.I change
a_listfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a list, tuple, string, float, integer, boolean or None.
I add a call to a set in
test_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘a_set’
I add
a_setand point it to{0, 1, 2, 'n'}intype_error.pythe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘set’ object is not callable
a_setpoints to a set ({0, 1, 2, 'n'}) which is NOT callable. Using substitutiona_set = {0, 1, 2, 'n'} a_set() {0, 1, 2, 'n'}(){0, 1, 2, 'n'}()raises TypeError because I cannot call a set like a function.I change
a_setfrom a variable to a function to make it callablethe test passes. I can call a function, I cannot call a set, list, tuple, string, float, integer, boolean or None.
I add a call to a dictionary in
test_type_error.pythe terminal is my friend, and shows AttributeError
- caption:
terminal
- AttributeError: module ‘src.type_error’
has no attribute ‘a_dictionary’
I add
a_dictionaryand point it to{'key': 'value'}intype_error.pythe terminal is my friend, and shows TypeError
- caption:
terminal
TypeError: ‘dict’ object is not callable
a_dictionarypoints to a dictionary ({'key': 'value'}) which is NOT callable. Using substitutiona_dictionary = {'key': 'value'} a_dictionary() {'key': 'value'}(){'key': 'value'}()raises TypeError because I cannot call a dictionary like a function.I change
a_dictionaryfrom a variable to a function to make it callablethe test is green again. I can call a function, I cannot call a dictionary, set, list, tuple, string, float, integer, boolean or None.
I remove the commented lines
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
close the project
I close
tests/test_type_error.pyandsrc/type_error.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
The tests show that
If I call a method with an instance, it takes the instance as the first argument (
self)I can use the staticmethod decorator if the method does not use anything that belongs to the object it is part of.
All the tests so far show that I get TypeError when I call an object in a way that is different from its definition.
How many questions can you answer about TypeError with objects?
code from the chapter
what is next?
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.