Python Type Checking in Large Applications

The Problem

In my previous post, I outlined how Python makes development a lot faster.

Unfortunately, Python’s lack of a static type checker can really hinder development in large applications.

Puzzle Pieces

As a great example, I was working on some slightly older code just a few days ago.  This code had not been touched in probably six months to a year.

I set a member variable of a class to False.  Unfortunately, I did not realize this same-named member of the class was actually a function.

So later on in the code, something tried to call my False as a function.  This probably took anywhere from 15-60 minutes to debug.

This is a potentially big problem with large applications.  There is no Python type checking; the language will gladly allow you to overwrite just about anything with a value of your choice.

So Is Python Not Good for Large Applications?

This leads several to claim that Python is not a good language for a large application.  Instead, leave that to the “big boys” like C++ or Java.

I disagree.

Wrong

If you need better performance than what Python provides, I can understand using C or C++.  But if the main objection is static type checking, there’s a great solution for this:

Well-written unit tests.

With decent code coverage and well-written unit tests, you should not have to worry about Python type checking.

Unit Testing to Improve Code Quality

Ironically, spending time unit testing to some degree contradicts my claim that Python is a fast development language.  If you’re writing unit tests, you aren’t going to develop (initially) as fast.  Over time, however, you will be faster due to less time spent troubleshooting.

This means if you are writing a very quick script to debug, perhaps you should not care about Python type checking or unit testing.

Rushed

If you are developing a bigger application or something that will be around for a while, it’s probably best to go ahead and write unit tests.

Some might say this defeats the purpose of fast development.  I would argue that bigger applications should be wrapped with tests anyway.  This is regardless of what language you use.

Python just happens to make unit testing a lot easier than some other languages.

So the next time someone complains that Python isn’t good for large applications, ask if they have unit tests around their code.

While Python does have a static type checking problem without tests, any large application should be wrapped with tests to catch these and other types of errors (like run-time errors).

Besides, who really wants to wait several minutes to verify every little change?

Use unit tests to your advantage!

2 comments:

  1. Should also note that Python does [kind of] support static typing. Syntax is defined in PEP 484 and MyPy implements a static type checker

    1. Thanks for the heads up! I had seen annotations before, but not necessarily how those annotations were used to actually do the checking.

      While we don’t use pylint at work, I’m hoping one day to start using it. While pylint doesn’t support PEP 484 yet, it’s on their backlog: https://github.com/PyCQA/pylint/issues/647. I know pylint is a pretty good static code analysis tool, and it’d be nice if it supported that. pylint would work well in Jenkins builds!

Leave a Reply

Your email address will not be published. Required fields are marked *