Help your IDE help your python project be less buggy and more readable by type hinting your code
By: Imran Ali
Tags: Pycharm Ide, Scaling Up, Python Programming, Type Annotation

Use your IDE’s IntelliSense and Static Analysis to its full capacity

Photo by Sean Lim on Unsplash

While Python is a great everyday language which finds its place in every single domain whether it be Finance, Data Science, Web Development, or IT Infrastructure; its usage in large scale projects is often limited and the well known reason given is in its dynamically typed nature which can make the code unreadable and its quite hard to tell what is going on at any given place

Since in large projects the code is more often read than written….

“Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. …[Therefore,] making it easy to read makes it easier to write.” ― Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship

This creates a problem on various different fronts. Firstly, a lot of documentation is required and needs to be maintained for other developers to be able to read and follow your code (which no developer enjoys). Secondly, the Integrated Development Environment are not that helpful in finding type issues and lot of bugs are left to be discovered during runtime. Lastly, because of not having the capability of finding these bugs during development a lot of test code is required which also most developers don’t enjoy.

So, in this article I will present a case with an example on how simple type annotations in your code can vastly increase your experience working with IDEs and also help find those bugs which would otherwise be discovered at runtime traditionally with Python by comparing code before adding type annotation and after. I will be using Pycharm as my IDE.

Without type hinting

Let’s say we want to write a simple add function that adds two integers

no type warning

Our first usage of the add function on line 5 is as expected by adding two integers but the second usage is incorrect when we try to add an integer to string ‘a’. This gives us the following runtime error

runtime error

Unfortunately, our heavyweight IDE is helpless in this scenario and gives us no warnings.

The intellisense isn’t so useful either

not so useful intellisense either

Since the IDE isn’t sure of the type returned by function it gives us suggestions to use string methods on an integer which is horrible!

With type hinting

Lets add type hinting to our code now

type warning

Immediately we can see the warning from Pycharm in my second usage of the add function on line 6 telling me that the function expects an integer but got a string instead. Now, I don’t have to run the code in order to fix this error and can fix it before runtime.

Lets look at how the intellisense is doing now

a useful intellisense

It only shows the methods that can be used on an integer and is now much more concise and clear.

Final Thoughts

Not sure about how you feel but I feel like not only did we find the incorrect usage issue earlier and a much more useful intellisense but the code is also much more clear and readable.

The developers will not be forced to document the code as diligently and a lot of unnecessary tests that would have to be written to catch bugs like these are no longer required and the focus can shift to the tests that add real business value and finds useful bugs

I used a very simple example to convey how useful type hinting could be in your projects but perhaps the powerfulness of this isn’t so clear in this small project but imagine yourself dealing with a ginormous project with hundreds of python files scattered among different packages and using all sorts of complicated data structures and user defined objects. Having the IDE warn us of incorrect usage of objects and providing a much concise and clear intellisense can greatly improve the adoption of python into larger projects.

Resources