Make your Python Code Bug free
How Type Checking helped me reduce 90% production level errors.
Static Type checking in Python is not something new but is ignored by new developers. For me coming from a Data Science background, I had no knowledge about software development. I used to train my model and then build an API for it and hope it doesn’t fall apart. With every new input, I discovered new bugs. Sometimes these bugs required 2–3 days of code refactoring. I learned my lesson and started to add type checking with every variable in method call or method value return.
Type Checking seems like a really tiring task. So, does it increase performance? Not really. But Python being a Dynamically Typed language, it becomes hard when your program becomes large with too many classes and methods. With Type Checking, you would instantly know if you are using any method or variable in the wrong way.
But PyCharm does all that, so why I should bother to learn all these? Learning is never wasted. Maybe you would move to an organization that restricts the use of PyCharm or any third-party software. You should always learn new things because you never know which might come handy.
So, let’s not waste any more time
- Passing arguments to a method
Notice how good the code looks after add type checking. The need to add type checking may not seem very reasonable as of now but bear with me. We’ll be seeing a lot more examples
2. Returning Values from a method
This is looking very simple and actually it is. Now, let’s look at little complex cases.
3. When you are returning multiple values
Here, we are returning 2 values which are both int type. Hence, we are using Union which means both values are included.
Let’s look at another example
CASE 2. When there are 2 return types depending on a condition. For example, you are returning a bool or string depending on a condition. So, you would use Tuple, which means you could return any value out of these.
So, what we are doing here is if the first number is 0 then we are returning the result as false else we are returning the sum of both numbers.
I know this a stupid example but there are other cases where you may find it useful.
A case where I discovered a bug that I wasn’t aware of.
I was working on a facial recognition program. There was a class that had the specific task of finding facial points and returning them. If no points are detected then it would return None. The method called this class would then apply the split method on the result. During the demo of this project, I was asked to upload a photo that had no face and boom the program crashed. The error was “NoneType has no attribute split”. At that time my mind stopped working and I couldn’t guess why the error occurred, what is the case I missed. Now, I realize if I had added Type Checking on the return type then it would show be an error that NoneType cannot be returned.
Also, if your program is returning multiple data types depending on a condition say a list or tuple. You may accidentally add append a new element. With Type Checking at the time of appending it would show an error that cannot add a new item to the tuple.
References
Happy Learning!