DEV Community

Why I started using Python type annotations, and why you should too

Florimond Manca on July 27, 2018

When Python 3.6 was released back in december 2016, I was excited at some of the new features it provided. Among them, f-strings were quite a highl...
Collapse
 
real_ishan profile image
Ishan Sharma

Great post!

I stumbled into numpy style docstrings when I was looking for a good way to comment code and they do make life a lot easier. Combined with an PyCharm (or any other IDE that understands them), they are very helpful.

I didn't know about data classes, looking forward to your post about them.

Collapse
 
florimondmanca profile image
Florimond Manca

Agreed on the Numpy doc style! I actually found out that you could change the documentation style in Pycharm, and now the first thing I do for new projects is switching from RestructuredText to NumPy right away. ;) It’s a simple, exhaustive and good looking style.

Collapse
 
real_ishan profile image
Ishan Sharma

That's something that I do as well. I wish there was a setting to set it as default for new projects. I went through documentation and searched but it isn't there. 😒

Thread Thread
 
florimondmanca profile image
Florimond Manca • Edited

Never thought of looking that up, but in that case, yes — it's a shame the default setting doesn't exist!

Collapse
 
epogrebnyak profile image
Evgeny Pogrebnyak

Liked to namedtuple example! My experience with annotations is quite limited though: I add simple types as a substitute for docstrings, but when it comes to compose types, eg list of dicts, importing typing module is not a habit for me yet. Mypy is not integrated to spyder, an IDE I use, so I do not get any benefits other than readibility. Annotations are not yet substitutes for strong typing, which is some of the promises of python 4, I think. I wish I had an option of using python as a strongly typed language.

Collapse
 
florimondmanca profile image
Florimond Manca

Thanks! It's a shame that Spyder doesn't support type checking. I know Atom doesn't either (I use Atom at home and PyCharm at work) but it manages to use the annotations for autocompletion (i.e. displaying methods of an object annotated as a certain class), which is already pretty handy. Does Spyder's autocompletion support this as well?

If I may, I'm not sure "strong typing" is the correct word there — Python is a very strongly typed language. Everything has a type, even the type object itself!
However it is does not have "true" static typing, yet — and I don't think it ever should. Adding attributes/methods at runtime is one of the most powerful things dynamic typing allows, and that would have to go away with static typing.
However, if a very optional and controllable way of saying "this variable should never be assigned to a variable of type other than XYZ", I'd be interested as well. Not sure how much overhead that would create, though, as the interpreter would have to do checks on every assignments.
If you have resources on forecast static typing features for Python 4, I'd be glad to check them out! :)

Collapse
 
matyashvladislav profile image
Vladislav Matyash

Hello. You may read this article:
medium.com/@ageitgey/learn-how-to-...