In this post I am going to discuss how accessing a value in a dict can easily introduce a bug in your code and how to fix it.
Access
One of the most common scenarios with dictionaries is accessing a value.
value = my_dict[key]
Problem: If key doesn't exist in my_dict, the code will raise a KeyError
Solution: To avoid this scenario you have three options:
-
Check if
keyexists before accessing
if key in my_dict: value = my_dict[key] -
Wrap the access in
try-exceptblock
try: value = my_dict[key] except KeyError: ... -
Use Python's get function to avoid checking for
keyindict
value = my_dict.get(key)
Since, get option (3) looks the neatest, I tend to use that the most.
Now, suppose for a function I am writing, I am accessing a value from dict but I only want to proceed if the given key exists in the dict.
I would write something like this:
value = my_dict.get(key)
if not value:
raise MyError('Found bad key')
# continue with code
At first glance, the code looks fine, but I have introduced a bug in it.
Let me illustrate with an example:
# Assume my_dict as:
my_dict = {
'key1': 'value1',
'key3': [],
}
| Key | Behavior | Expected? |
|---|---|---|
| key1 | Regular code execution | Yes |
| key2 | raises MyError
|
Yes |
| key3 | raises MyError
|
No |
key2raises the exception sincegetfunction will returnNoneby default for non-existing keys.key3exists in thedicthowever my code will still throw an error becauseif not []is alsoTrue, leading to unintentional behavior.
Quick Bad Fix
Reading points about key2 and key3 from above, modifying the validation condition seems like the most obvious fix:
value = my_dict.get(key)
if value is None:
raise MyError('Found bad key')
This, however, only fixes one symptom/effect of the bug that is when the value is an empty data type like '' or []. If the value in my_dict is None the above code will still raise the error.
Fix
Before writing a fix for your code, it is usually a good idea to think about the root cause of the bug, rather than the symptoms that are showing up. In our case, it was not the if condition that was causing the bug - it was the get function.
I only want to proceed if the given key exists in the
dict
To encode the described behavior the correct code block would look something like this:
if key in my_dict:
value = my_dict[key]
# rest of code
else:
raise MyError('Found bad key')
Learning
- Capturing intent is critical in writing robust code.
- First step to fixing bugs should be to list assumptions and work from there.
Top comments (0)