The Incredible Hulk(ython): Making Python Strong(ly Typed)
Tags: python3, static
Published: 2018-08-07 18:01:13

---
title: The Incredible Hulk(ython): Making Python Strong(ly Typed)
published: true
tags: python3,static
canonical_url: https://medium.freecodecamp.org/the-incredible-hulk-ython-making-python-strong-ly-typed-adc1b6ccf686
---

Python is a wonderful [dynamically typed](https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages) 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?

![](https://cdn-images-1.medium.com/max/615/1*Awcmdb1xwSKXM7aID2TP1A.png)

### Dealing with types, without thinking about suicide :)

Python has added, as of version 3.5, _optional_ support for type hints, mostly through the [typing](https://docs.python.org/3/library/typing.html) 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](https://www.typescriptlang.org/docs/handbook/basic-types.html) 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](http://mypy-lang.org/examples.html)

Of course, most IDEs have some functionality towards type checking. [Here](https://www.jetbrains.com/help/pycharm/type-hinting-in-product.html) 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

### Conclusion

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.

_Originally published at_ [_perigk.github.io_](http://perigk.github.io)_._

* * *