Skip to main content

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.

Metadata

Release files for impunity 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for impunity 1.0.5
File Size Uploaded
impunity-1.0.5.tar.gz 151.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for impunity 1.0.5
File Interpreter ABI Platform
impunity-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 165.5 kB

Release files / impunity-1.0.5.tar.gz

Download URL impunity-1.0.5.tar.gz
Size 151.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5aa9937e7574affdeb2b5272a33048d2361dce843ae6460400e0efce0974243a
BLAKE2b-256 checksum
How to use checksums
a13ca339666fef00a5a73ff786264dc8c36431de2e5d342d5fcab03cdf2db58e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / impunity-1.0.5-py3-none-any.whl

Download URL impunity-1.0.5-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
236ece4f4d376de70ebb011a9c18c1bbbed64f92d682201f0710327b818f9e82
BLAKE2b-256 checksum
How to use checksums
4ba9c0ad6c5c34f8bcfe1ea536dc0b258326e18e6260e080dea93b7794ebe4db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page