loading...

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

perigk profile image Periklis Gkolias Originally published at medium.freecodecamp.org on ・3 min read

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?

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

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.


Posted on by:

perigk profile

Periklis Gkolias

@perigk

In love with Python, but I admire all the stacks that offer solutions without testing my patience. Avid productivityist, great-food worshipper, always-smiling.

Discussion

pic
Editor guide
 

A great read, I work on a project created with typing in mind from the outset. There are still issues, such as lack of actually enforcing anything, and random mypy errors, but I feel like this is a good direction for the language to take.

 

Hi Ran,

Glad you liked it. At this very moment(think few months ago when this article was written) mypy is in a dev branch(0.650dev), so it is not recommended for production code. I should have mentioned it probably.

 

I'd really want to start using type checking in a large code base with growing team working on it but look like mypy still have some issue in a large code base, which a bit ironic. I shared my initial experience before dev.to/k4ml/i-started-to-like-mypy....

 

Nice article, thanks for sharing

 

is this

def find\_files\_of\_type(

a legal func name?

 

No, I will check why this happened. Thanks

 

some syntax highlighting might be nice

 

Sure, to be honest I am not sure why it is happening, maybe as part of the transfer between medium and dev.to. I will have a look how this is done. Thanks