Bitperi(Periklis Gkolias)

Productivity tips and coding for thriving people

The Incredible Hulk(ython): Making Python Strong(ly Typed)

Python is a wonderful dynamically typed language, but quite a few people consider this as its biggest disadvantage.

But why?

Even though dynamically typed languages, remove the headache of writing “mundane” type declarations and make writing more pleasant and a little bit faster, this need is just delegated to the runtime environment of the language.

That means, that some bugs that could have been eliminated, almost immediately after they are introduced, they will now remain silent till the code is invoked, And you know when this is going to happen, right?

The ultimate truth

Dealing with types, without thinking about suicide :)

Python has added, as of version 3.5, optional support for type hints, mostly through the typing module.

It looks like, they care to compromise both words. From the one side, the people who love the liberty of dynamic typing can continue…ignoring the type hints. From the other side, the people who love the safety of static typing can benefit from utilizing the new functionality.

How to use it

The way to use it, or at least, to start using it is quite simple and straightforward. More specifically it looks a lot like the typescript way of static typing, in case you are familiar. Here is an example:

# Typing is the core module that supports type checking. 
# In here we import List, which provided equivalent functionality to 
# the list() function or the [] equivalent shorthand
from typing import List

# We define a function, as usually but we add the expected
# type to the args and we add a return type too
def find_files_of_type(type: str, files_types: List[str]) -> bool:
    return (type in files_types)

files_types: List[str] = ['ppt', 'vcf', 'png']

type_to_search: str = 'ppt'

print('Found files of type {} in list? {}'.format(type_to_search, 
    find_files_of_type(type_to_search, files_types)))

A bit awkward, but still clear, right? :)

The trap the Hulk has fallen into

You might have noticed that I mentioned the word ‘optional’ few lines above. So, at the time of writing this article, there is no enforcement on the type-checking.

You might add whatever irrelevant type you want to your variables, do the most invalid, irrelevant and “perverted” operations to them but python won’t bat an eye.

If you want to enforce the type checking, you should use a type checker(duh?), like the great mypy

Of course, most IDEs have some functionality towards type checking. Here is the relevant documentation for Pycharm.

Thinks I would like to see in the future

  • Integrate a type-checking mechanism in the core of the language
  • As a result of the above, more seamless type hints. For example, if the type-checking is on then I should not have to use the class List or Tuple to do it. The [] and () shorthands should be enough


Thank you for reading this article. This is by no means an extended guide, to this great functionality of python, but rather a primer to lead in more research.

If you are starting now, a new project in python 3.5+, I would strongly recommend you to experiment a bit with the type checking. I would love to see your suggestions and thoughts about this feature, so feel free to leave a comment.

comments powered by Disqus