DEV Community

Waylon Walker
Waylon Walker

Posted on • Originally published at waylonwalker.com

5 1

Gradual Typing in Python

I've referenced a video from Anthony Sotile in passing conversation several times. Walking through his gradual typing process has really helped me understand typing better, and has helped me make some projects better over time rather than getting slammed with typing errors.

Step 1

Run Mypy as is, don't get fancy yet. This will not reach into any functions unless they are alreay explicitly typed. It will not enforce you to type them either.

pip install mypy mypy .
# or your specific project to avoid .venvs
mypy src
# or a single file
mypy my-script.py
Enter fullscreen mode Exit fullscreen mode

Step 2

Next we will add check-untyped-defs, this will start checking inside functions that are not typed. To add this to your config create a
setup.cfg with the following.

[mypy] check_untyped_defs = True
Enter fullscreen mode Exit fullscreen mode

Step 3

The final stage to this series is to add disallow_untyped_defs. This will start requiring all of your functions to be type hinted. This one is probably the toughest, because as you type functions mypy can uncover more issues for you to fix. Often times the list of errors grows before it shrinks.

[mypy] check_untyped_defs = True
Enter fullscreen mode Exit fullscreen mode

Anthony's video

Make sure that you watch Anthony's video, give him a sub, he deserves it for all the great things he is doing for the python community.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

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

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

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

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay