Sub topic 12
Reading TypeErrors from bad calls
This chapter assumes you can define functions with defaults, you know the * and / markers, and you can read a basic traceback. Here you will learn to map each binding error message to the rule it breaks.
How Python binds a call
Python checks a call in a fixed order. Each error message comes from one of these steps.
Errors can appear at steps 1 and 2 too, for example when there are too many positional arguments or a keyword name does not exist.
Five errors in one script
This script uses the connect and clamp signatures from earlier chapters. It triggers each common error and prints the message.
def connect(host, port=5432, *, timeout=10):
return (host, port, timeout)
def clamp(value, /, low=0, high=100):
return max(low, min(value, high))
calls = [
lambda: connect(),
lambda: connect("db.local", 5432, 5),
lambda: connect("db.local", tmeout=5),
lambda: connect("db.local", host="other"),
lambda: clamp(value=5),
]
for call in calls:
try:
call()
except TypeError as exc:
print(exc)On Python 3.10 or later the output is:
connect() missing 1 required positional argument: 'host'
connect() takes from 1 to 2 positional arguments but 3 were given
connect() got an unexpected keyword argument 'tmeout'
connect() got multiple values for argument 'host'
clamp() got some positional-only arguments passed as keyword arguments: 'value'Newer versions may add a "Did you mean" hint to the third message. Older versions print only the function's bare name, which is the same for module-level functions.
What each message means
| Message fragment | Cause | Fix |
|---|---|---|
| `missing N required positional argument` | A parameter with no default received nothing. | Pass it, or give the parameter a default. |
| `missing N required keyword-only argument` | A keyword-only parameter without a default was not named. | Pass it as `name=value`. |
| `takes from X to Y positional arguments but Z were given` | Too many positional values. Keyword-only parameters do not count toward Y. | Name the extra values as keywords. |
| `got an unexpected keyword argument` | The name does not match any parameter. This is usually a typo. | Check the spelling against the signature. |
| `got multiple values for argument` | A value arrived by position and again by keyword. | Remove one of them. |
| `positional-only arguments passed as keyword arguments` | A parameter before `/` was named in the call. | Pass it by position. |
Reading the counts
The phrase "takes from 1 to 2 positional arguments" gives you the signature's limits. The lower number is the count of parameters without defaults. The upper number is the count of all parameters that can be passed by position. When the two numbers are equal, the message says "takes 2 positional arguments" instead.
The count in "but 3 were given" includes every positional value in the call, so check for unpacking. A call like connect(*row) may supply more values than you can see in the source line.
Two habits that speed up diagnosis
- Compare the call to the signature, not to your memory of it. Run
inspect.signature(connect)to see the current parameter list. - Read the function name in the message. In a chain of calls, it tells you which call failed. With a wrapper that forwards
*argsand**kwargs, the error names the inner function, not the wrapper you wrote.
Exercise
Given def log(msg, level="info", *, tag=None): ..., which message does log("hi", "warn", "net") produce, and how do you fix the call?
Show answer
It raises log() takes from 1 to 2 positional arguments but 3 were given. The parameter tag is keyword-only, so it cannot take a third positional value. Fix the call with log("hi", "warn", tag="net").
Exercise
A call connect("db.local", 5432, port=6000) fails. Which error is raised and why?
Show answer
It raises connect() got multiple values for argument 'port'. The second positional value, 5432, already fills port, and the keyword port=6000 supplies it a second time.