DEV Community

Cover image for Python 🐍 and variable types
Gabor Szabo
Gabor Szabo

Posted on • Originally published at szabgab.com

1

Python 🐍 and variable types

Python is one of the most flexible languages out there. For example you can easily use the same variable, to store first a number then assign a string to and later even a list of booleans. Python will accept whatever you put in the variable.

This is great but people who arrived to Python from strongly typed languages where you had to declare each variable with its type felt very uncomfortable with all that flexibility. They felt the compiler does not protect them.

So the Python community came up with some type-annotation that you can add to your code. 🎁

The problem is that Python actually disregards these type annotations. 🌲

That's how you can encounter code that looks like this:

answer :str = 42
Enter fullscreen mode Exit fullscreen mode

Here we define a variable to be a string and then promptly assign an integer to it. 🦧

Python will be happy with this code.

So then what is the value of these type-annotations? Why bother and how did this happen?

The thing is that Python still disregards these type annotations, but there are external tools that can make use of them. One of them, called mypy will check that

the code actually abides by the rules the programmers added.

I noticed at several companies that a developer started to add type annotations to the code-base, but apparently never ran mypy to actually check the type-annotation.

So when I started to help at the company we actually had misleading type annotations.

The first thing I did was setting up a CI system that would run mypy on the code-base and will fail the process if something was broken.

Well, this wasn't that simple, I had to do this gradually and it took quite a long time to clean the existing type annotations and to add more, but the whole process

made the code a lot safer and easier to maintain. 💊

Do you use type annotation in your Python code-base? If not, why not?

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay