A TypeError occurs when Python receives an inappropriate data type for an operation or function. For example, dividing two strings fails because division expects numeric inputs. section 3.6.2
Listing 3.32 A TypeError occurs when an unexpected data type is used for a particular operation, such as attempting to divide two strings.
"a" / "b" #1
# Result of running this line of code: #2
# TypeError: unsupported operand type(s) #2
for /: 'str' and 'str' #2
See the focused example of a TypeError caused by attempting to divide two strings. section 3.6.2
Prevent or communicate type-related failures by:
- Add type hints such as a:int and b:int to make expected function inputs and outputs clear. section 3.4
- Use isinstance to validate parameter types and raise TypeError with a message when inputs do not meet the specification. section 3.4
- Distinguish TypeError from IndexError, KeyError, NameError, SyntaxError, ImportError, and AssertionError when diagnosing failures. section 3.6.2 section 3.9


