DEV Community

Cover image for The Comprehensive Guide to mypy

The Comprehensive Guide to mypy

Tushar Sadhwani on May 05, 2021

Read the original on my personal blog Mypy is a static type checker for Python. It acts as a linter, that allows you to write statical...
Collapse
 
artalus profile image
Artalus

I thought I use typehints a lot, but I have not yet encountered half of the things described here! Thank you for such an awesome and thorough article :3

Question. What do you think would be best approach on separating types for several concepts that share the same builtin type underneath? To avoid something like:

time_seconds: int = 1
time_ms: int = 1000
weight_kg = 61
def foo(start_ms: int, end_ms: int): ...
foo(time_seconds, weight_kg)
Enter fullscreen mode Exit fullscreen mode

In modern C++ there is a concept of ratio heavily used in std::chrono to convert seconds in milliseconds and vice versa, and there are strict-typing libraries for various SI units. Would be nice to have some alternative for that in python.

Collapse
 
tusharsadhwani profile image
Tushar Sadhwani • Edited

oh yea, that's the one thing that I omitted from the article because I couldn't think up a reason to use it. mypy has NewType which less you subtype any other type

like you can do ms = NewType('ms', int) and now if your function requires a ms it won't work with an int, you need to specifically do ms(1000). But in python code, it's still just an int. I think that's exactly what you need.

Collapse
 
artalus profile image
Artalus

I think that's exactly what you need.

Totally! The ultimate syntactic sugar now would be an option to provide automatic "conversion constructors" for those custom types, like def __ms__(seconds: s): return ms(s*1000) - but that's not a big deal compared to ability to differentiate integral types semantically.

Thank you :)

Thread Thread
 
tusharsadhwani profile image
Tushar Sadhwani

Knowing that it's Python, I'm pretty sure that's easy to patch in on your side as well :)

Collapse
 
tusharsadhwani profile image
Tushar Sadhwani

I'm going to add NewType to the article now that I have a reason to :)

Collapse
 
golinvauxb profile image
Benjamin Golinvaux

Thanks for this very interesting article. Same as Artalus below, I use types a lot in all my recent Py modules, but I learned a lot of new tricks by reading this.

Collapse
 
mhihasan profile image
Hasanul Islam • Edited

Superb!
I am using pyproject.toml as a configuration file and stubs folder for my custom-types for third party packages.

Running from CLI, mypy . runs successfully. but when it runs at pre-commit, it fails (probably assuming stubs not present and thus return type is Any)

Collapse
 
coreyjs profile image
Corey Schaf

Great post! I use type hinting all the time in python, it helps readability in larger projects.

Collapse
 
0916dhkim profile image
Danny Kim

In JavaScript ecosystem, some third-party libraries have no Typescript support at all or sometimes have incorrect types which can be a major hassle during development. How's the status of mypy in Python ecosystem?

Collapse
 
tusharsadhwani profile image
Tushar Sadhwani

Mypy is still fairly new, it was essentially unknown as early as 4 years ago. But the good thing about both of them is that you can add types to projects even if the original authors don't, using type stub files, and most common libraries have either type support or stubs available :)

Not much different than TypeScript honestly.

Collapse
 
miguendes profile image
Miguel Brito

What a great post! This is the most comprehensive article about mypy I have ever found, really good. Congratulations!

Collapse
 
tusharsadhwani profile image
Tushar Sadhwani

Thanks a lot, that's what I aimed it to be :D

Collapse
 
cavo789 profile image
Christophe Avonture

Used mypy for the first time today and sometimes quite obscur. Many, many thanks for your GREAT article !