Enhanced Debugging in Python: Tracebacks Just Got a Major Upgrade | by Thomas A Dorfer | Mar, 2023
How location-enriched tracebacks facilitate the debugging experience in Python 3.11
In Python, a traceback is a report that is displayed when an exception occurs, along with a (hopefully) helpful error message that can guide the user to the problem.
Up until now, tracebacks only showed the line in which the exception occurred, but did not provide any location-specific information about which part of that line is affected.
To illustrate this, let’s assume we have a dictionary object containing some summary statistics about some random data. Based on that, we’d like to calculate the lower and upper bounds of the confidence interval. This can be done as follows:
import numpy as npstats = {'n': 50,
'mean': 20,
'std': 2.5,
'z': None}
def ci(stats):
lower = stats['mean'] - stats['z'] * (stats['std'] / np.sqrt(stats['n']))
upper = stats['mean'] + stats['z'] * (stats['std'] / np.sqrt(stats['n']))
return lower, upper
ci(stats)
I deliberately assigned None
to the z-value in our dictionary object to provoke an exception. For Python versions lower than 3.11, this is what the traceback would look like:
While it correctly shows the line where the exception occurred, the user is not given any information about whether the issue lies with the mean, the z-value, the standard deviation, or the sample size n.
This has changed in Python 3.11 with its new, location-enriched traceback feature:
Here, it clearly highlights that the error occurred with the term stats['z']
, making it a lot easier for the user to debug the program.
Let’s now look at another example. Imagine you have a Python script that performs some computation on matrices. Remember, with matrix multiplication, the inner dimensions must be the same. In other words, the number of columns in the first matrix must be the same as the number of rows in the second matrix.
In the code below, I introduced a dimension mismatch between matrices c
and d
.
import numpy as npa = np.random.rand(3, 5)
b = np.random.rand(5, 4)
c = np.random.rand(3, 5)
d = np.random.rand(3, 5)
def mat_mul(a, b, c, d):
return (a @ b) + (c @ d)
mat_mul(a, b, c, d)
As expected, this results in an exception when trying to multiply them. Here’s what it looks like in Python versions lower than 3.11:
If this code was part of a larger program, or involved more matrices, it would be very difficult and time-consuming to figure out which matrix is the one with the incorrect dimensions.
In Python 3.11, the traceback pinpoints to the exact part in the formula that contains the matrices whose dimensions mismatch, guiding the user directly to the core of the issue.
Location-enriched tracebacks considerably facilitate the programmer’s debugging experience and can lead to speedier resolutions when faced with bugs in the code. In order to enable this feature, however, one has to update Python to version 3.11.
I hope this feature will make your debugging experience easier and save you some valuable time while doing so. Happy coding!
How location-enriched tracebacks facilitate the debugging experience in Python 3.11
In Python, a traceback is a report that is displayed when an exception occurs, along with a (hopefully) helpful error message that can guide the user to the problem.
Up until now, tracebacks only showed the line in which the exception occurred, but did not provide any location-specific information about which part of that line is affected.
To illustrate this, let’s assume we have a dictionary object containing some summary statistics about some random data. Based on that, we’d like to calculate the lower and upper bounds of the confidence interval. This can be done as follows:
import numpy as npstats = {'n': 50,
'mean': 20,
'std': 2.5,
'z': None}
def ci(stats):
lower = stats['mean'] - stats['z'] * (stats['std'] / np.sqrt(stats['n']))
upper = stats['mean'] + stats['z'] * (stats['std'] / np.sqrt(stats['n']))
return lower, upper
ci(stats)
I deliberately assigned None
to the z-value in our dictionary object to provoke an exception. For Python versions lower than 3.11, this is what the traceback would look like:
While it correctly shows the line where the exception occurred, the user is not given any information about whether the issue lies with the mean, the z-value, the standard deviation, or the sample size n.
This has changed in Python 3.11 with its new, location-enriched traceback feature:
Here, it clearly highlights that the error occurred with the term stats['z']
, making it a lot easier for the user to debug the program.
Let’s now look at another example. Imagine you have a Python script that performs some computation on matrices. Remember, with matrix multiplication, the inner dimensions must be the same. In other words, the number of columns in the first matrix must be the same as the number of rows in the second matrix.
In the code below, I introduced a dimension mismatch between matrices c
and d
.
import numpy as npa = np.random.rand(3, 5)
b = np.random.rand(5, 4)
c = np.random.rand(3, 5)
d = np.random.rand(3, 5)
def mat_mul(a, b, c, d):
return (a @ b) + (c @ d)
mat_mul(a, b, c, d)
As expected, this results in an exception when trying to multiply them. Here’s what it looks like in Python versions lower than 3.11:
If this code was part of a larger program, or involved more matrices, it would be very difficult and time-consuming to figure out which matrix is the one with the incorrect dimensions.
In Python 3.11, the traceback pinpoints to the exact part in the formula that contains the matrices whose dimensions mismatch, guiding the user directly to the core of the issue.
Location-enriched tracebacks considerably facilitate the programmer’s debugging experience and can lead to speedier resolutions when faced with bugs in the code. In order to enable this feature, however, one has to update Python to version 3.11.
I hope this feature will make your debugging experience easier and save you some valuable time while doing so. Happy coding!