Skip to main content

Static checking for consistency of physical units

Project description

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

  • The most simple usage consists of using units placed as annotations: code is checked and rewritten if need be.

    from impunity import impunity
    
    def speed(distance: "m", time: "s") -> "m/s":
        return distance / time
    
    @impunity
    def regular_conversion():
        altitudes: "ft" = np.arange(0, 1000, 100)
        duration: "mn" = 100
        result = speed(altitudes, duration)
        print(result)  # results in m/s
    
        result_imperial: "ft/mn" = speed(altitudes, duration)
        print(result_imperial)  # results in ft/mn
    
    if __name__ == "__main__":
        regular_conversion()
    
  • The check fails if units are inconsistent:

    @impunity
    def inconsistent_units():
        temperatures: "K" = np.arange(0, 100, 10)
        duration: "s" = 6000
        return speed(temperatures, duration)
    
    # Warning: "K" is not compatible with "m"
    
  • Only check for consistency, do not attempt to rewrite the code:

    @impunity(rewrite=False)  # only check for consistency
    def regular_conversion():
        pass
    
    @impunity(rewrite="output.log")  # check code output in an external file
    def regular_conversion():
        pass
    

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:

  • Linux, MacOS and Windows;
  • all versions of Python since 3.8

Why impunity?

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

impunity-1.0.5.tar.gz (151.8 kB view details)

Uploaded Source

Built Distribution

impunity-1.0.5-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file impunity-1.0.5.tar.gz.

File metadata

  • Download URL: impunity-1.0.5.tar.gz
  • Upload date:
  • Size: 151.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for impunity-1.0.5.tar.gz
Algorithm Hash digest
SHA256 5aa9937e7574affdeb2b5272a33048d2361dce843ae6460400e0efce0974243a
MD5 6e06f6ae6307181e43023f36b65fe508
BLAKE2b-256 a13ca339666fef00a5a73ff786264dc8c36431de2e5d342d5fcab03cdf2db58e

See more details on using hashes here.

File details

Details for the file impunity-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: impunity-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for impunity-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 236ece4f4d376de70ebb011a9c18c1bbbed64f92d682201f0710327b818f9e82
MD5 beacd378ccf8e5596e028b410626a5b5
BLAKE2b-256 4ba9c0ad6c5c34f8bcfe1ea536dc0b258326e18e6260e080dea93b7794ebe4db

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page