Getting started

Impunity (/ɪmˈpjuː.nə.ti/) is a Python library that enables static analysis of code annotations to ensure coherence and consistency of physical units. It provides a framework for developers to annotate their Python code with physical units and automatically verifies if the units are compatible and adhere to predefined coherence rules.

This guide will help you get started with using Impunity to ensure coherence and consistency of physical units in your Python code. To do so, it uses the Pint library to check if units in annotations are compatible between each other.

Installation

The best way to install impunity is to use pip in a Python >= 3.8 environment:

pip install impunity

Usage

Using Impunity is straightforward. Follow the steps below to integrate it into your code:

# 1: Import the necessary decorator from the Impunity library:

from impunity import impunity

# 2: Annotate your code with appropriate units:

distance: "meters" = 10
time: "seconds" = 10

# 3: Apply the @impunity decorator to functions or methods that you want to check

@impunity
def speed(distance: "meters", time: "seconds") -> "meters / seconds":
    return distance / time

speed(distance, time) # return 1

Impunity checks the units in annotations statically, before execution. When inconsistencies are found in the decorated code, conversion are performed under the hood when possible:

@impunity
 def speed(distance: "meters", time: "seconds") -> "km / h":
     return distance / time

 speed(distance, time) # return 3.6

When inconsistencies cannot be fixed through conversion, warnings are raised with hints on how to correct the code:

@impunity
def speed(distance: "meters", time: "seconds") -> "kelvin":
    return distance / time

 Warning: "Incompatible unit returned"