DEV Community

Discussion on: The Comprehensive Guide to mypy

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 :)