achevrot / impunity

A Python library to check physical units
https://achevrot.github.io/impunity/
MIT License
10 stars 0 forks source link
converter quantity static units

tests Code Coverage License Checked with mypy

impunity is a Python library consisting of a single decorator function designed to ensure consistency of physical quantities. Compared to other libraries (pint, etc.), it has a minimal overhead on performance because physical units are only manipulated through static analysis and disappear at runtime.

impunity is based on Python “flexible variable and function annotations” (PEP 593) and checks consistency between variables and arguments of functions. If physical units are consistent, impunity rewrites the code by automatically applying conversions in the code of the function.

impunity is compatible with regular type annotations, and functions decorated with impunity remain compatible with other static analysis tools and type checkers like mypy.

In most situations, impunity will only perform minimal sanity checks on your code at import time and not edit anything.

Installation

impunity is available on pip (and soon conda):

pip install impunity

For development purposes, clone the repository and use poetry:

git clone --depth=1 https://github.com/achevrot/impunity
cd impunity
poetry install

Usage

Full documentation available at [website]()

Compatibility with type checkers

Types can be used with the Annotated keyword, which carries

from typing import Annotation
import numpy.types as npt

feet_array = Annotated[npt.ndarray[np.float64], "ft"]
altitudes: feet_array = np.arange(0, 1000, 100)

impunity is implemented and typed with Annotated keywords.

Tests

Tests are supported by the unittest package.

Because AST manipulation can be tricky, continuous integration is ensured by Github Actions for:

Why impunity?

We were searching for a pun with physical units. Things converged on im-pun-unit-y.