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 slam…

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



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



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



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.


Print Share Comment Cite Upload Translate
APA
Waylon Walker | Sciencx (2024-03-29T06:58:10+00:00) » Gradual Typing in Python. Retrieved from https://www.scien.cx/2022/01/21/gradual-typing-in-python/.
MLA
" » Gradual Typing in Python." Waylon Walker | Sciencx - Friday January 21, 2022, https://www.scien.cx/2022/01/21/gradual-typing-in-python/
HARVARD
Waylon Walker | Sciencx Friday January 21, 2022 » Gradual Typing in Python., viewed 2024-03-29T06:58:10+00:00,<https://www.scien.cx/2022/01/21/gradual-typing-in-python/>
VANCOUVER
Waylon Walker | Sciencx - » Gradual Typing in Python. [Internet]. [Accessed 2024-03-29T06:58:10+00:00]. Available from: https://www.scien.cx/2022/01/21/gradual-typing-in-python/
CHICAGO
" » Gradual Typing in Python." Waylon Walker | Sciencx - Accessed 2024-03-29T06:58:10+00:00. https://www.scien.cx/2022/01/21/gradual-typing-in-python/
IEEE
" » Gradual Typing in Python." Waylon Walker | Sciencx [Online]. Available: https://www.scien.cx/2022/01/21/gradual-typing-in-python/. [Accessed: 2024-03-29T06:58:10+00:00]
rf:citation
» Gradual Typing in Python | Waylon Walker | Sciencx | https://www.scien.cx/2022/01/21/gradual-typing-in-python/ | 2024-03-29T06:58:10+00:00
https://github.com/addpipe/simple-recorderjs-demo