Python Forum

Full Version: Statically checking physical units in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone !

This is my first post here so I hope I am not posting things in the wrong category.

I wanted to present to you the new library I programmed called impunity.
It will hunt physical annotations in the Python AST and check if there are any inconsistencies between physical units.
It simply uses a decorator for the checks for now.

The code is here.

It works like that :

@impunity
def speed(d: "m", t: "s") -> "m / s":
   return d / t

speed(10, 10) # returns 1

@impunity
def speed(d: "m", t: "s") -> "km / h":
   return d / t

speed(10, 10) # returns 3.6
And will raise an error when conversions are not possible.
It uses the Pint library to get the conversions but will have almost no overhead since the checks are statics.

I really look forward to have some people giving feedbacks and / or contribute if this library can help them.

Have a great day ! Heart