Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

TUnits - Fast Python Units

Continuous Integration

Implements unit of measurement arithmetic, where a number is associated with a product of powers of base units and values with compatible units can be added.

The library is written in Cython for speed. The library provides the ability to statically check dimensionality type (see below) and a limited protobuffer serialization support for select units (see below). Contributions to extend support for more units are welcome.

A precompiled wheel can be installed using pip install typedunits [--pre].

Example

>> import tunits
>> from tunits.units import meter, km, N, MHz

>> print(3*MHz)
Frequency(3, 'MHz')

>> print(5*meter + km)
Length(1005.0, 'm')

>> print(N/meter)
N/m

>> print((N/meter).in_base_units())
kg/s^2

>> print(2*km / tunits.Value(3, 's'))
0.666666666667 km/s

Static Type Check

TypedUnits provides the ability to statically check the dimensionality of variables and parameters. For example mypy would complain about incompatible types for the following code.

from tunits import Frequency, LengthArray
from tunits.units import meter, km, MHz

def frequency_fn(f: Frequency) -> Frequency:
    return 3*f

x = 2 * meter
x_arr = LengthArray([1, 2], km)
y = 3 * km
f = 7 * MHz

# okay
print(frequency_fn(f))
print(x + y)
print(x_arr - y)

# not okay
print(frequency_fn(x))
print(f + x)
print(x - f)
frequency_fn(x_arr)
$ mypy my_code.py
my_code.py:18: error: Argument 1 to "frequency_fn" has incompatible type "Length"; expected "Frequency"  [arg-type]
my_code.py:19: error: No overload variant of "__add__" of "Value" matches argument type "Length"  [operator]
my_code.py:19: note: Possible overload variants:
my_code.py:19: note:     def __add__(self, int | float | complex | number[Any], /) -> Frequency
my_code.py:19: note:     def __add__(self, ValueArray | list[Any] | tuple[Any] | ndarray[Any, dtype[Any]], /) -> ValueArray
my_code.py:19: note:     def __add__(self, Frequency, /) -> Frequency
my_code.py:20: error: No overload variant of "__sub__" of "Value" matches argument type "Frequency"  [operator]
my_code.py:20: note: Possible overload variants:
my_code.py:20: note:     def __sub__(self, int | float | complex | number[Any], /) -> Length
my_code.py:20: note:     def __sub__(self, list[Any] | tuple[Any] | ndarray[Any, dtype[Any]], /) -> ValueArray
my_code.py:20: note:     def __sub__(self, Length, /) -> Length
my_code.py:21: error: Argument 1 to "frequency_fn" has incompatible type "LengthArray"; expected "Frequency"  [arg-type]
Found 4 errors in 1 file (checked 1 source file)

Serialization support

TypedUnits provides protobuffer serialization support for selected units. Contributions are welcome to increase serialization coverage.

>> from tunits import Frequency
>> from tunits.units import MHz
>>
>> v = 3*MHz
>> msg = v.to_proto()
>> print(msg)
units {
  unit: HERTZ
  scale: MEGA
}
real_value: 3

>> Frequency.from_proto(msg)
Frequency(3.0, 'MHz')

Installation

  1. To install a precompiled wheel (add --pre for prelease version)

    pip install typedunits # [--pre] 
    
  2. To locally build the latest version from the main branch

    pip install git+https://github.com/quantumlib/TypedUnits
    
  3. For a local editable copy

    git clone https://github.com/quantumlib/TypedUnits
    cd TypedUnits
    pip install -e .
    

Development and Testing

  1. Clone the repository.

    git clone https://github.com/quantumlib/TypedUnits
    
    cd TypedUnits
    

    All future steps assume you are in the repository's directory.

  2. Install dev environment dependencies.

    pip install -r dev_tools/dev.env.txt
    
  3. Install TUnits

    pip install -e .
    
  4. Test.

    pytest
    

Formatting

dev_tools/format.sh  # to format
dev_tools/format.sh --check  # to verify format

Note: This is not an officially supported Google product

Metadata

Release files for typedunits 0.0.1.dev20250421203746

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

Built distributions (wheels)

Table of built distributions (wheels) for typedunits 0.0.1.dev20250421203746
File
typedunits-0.0.1.dev20250421203746-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
typedunits-0.0.1.dev20250421203746-cp313-cp313-win32.whl CPython 3.13 CPython 3.13 Windows x86-32 Details
typedunits-0.0.1.dev20250421203746-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_10_13_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250421203746-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
typedunits-0.0.1.dev20250421203746-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details
typedunits-0.0.1.dev20250421203746-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250421203746-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
typedunits-0.0.1.dev20250421203746-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
typedunits-0.0.1.dev20250421203746-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
typedunits-0.0.1.dev20250421203746-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
typedunits-0.0.1.dev20250421203746-cp310-cp310-win32.whl CPython 3.10 CPython 3.10 Windows x86-32 Details
typedunits-0.0.1.dev20250421203746-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details

Total release size: 23.6 MB

Release files / typedunits-0.0.1.dev20250421203746-cp313-cp313-win_amd64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp313-cp313-win_amd64.whl
Size 783.1 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
ccd3847aa2563073e93f46878257aa8a125372ff2a84e4cb0f2f88795ffc3887
BLAKE2b-256 checksum
How to use checksums
6f076638116bb6bc498ed17867bd59572ab667eca3bc4295a063e54d78a118e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250421203746-cp313-cp313-win32.whl
Size 741.2 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
b39d012c73fca8cb51ebebc3b1eb72f2f765b140f1f2c4698fa2295560a58c1b
BLAKE2b-256 checksum
How to use checksums
d9e7243295cd38b1bc5939d516b3a0e73af18ef92c66daa560ec8d8923857cd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
bd3304776158a387217d76077cc9a3cbac5cd396278bc1cbbd145f468d0b0466
BLAKE2b-256 checksum
How to use checksums
33dc543e39d26402bb9daf6e2493182166c91a1722a7241fb25a4a5856813563
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_11_0_arm64.whl
Size 813.9 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5a1c20a5cfcf9978e3e56acc188f4496b9407449691d6781228435b0493f3e0a
BLAKE2b-256 checksum
How to use checksums
25ab955c36ab6fd9d85a93a8e1ad737c322876e7014a858fd7bfc812b0fadd99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp313-cp313-macosx_10_13_x86_64.whl
Size 854.3 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
4d3964bce2587f00e73400860bd48ff61723c8ce9bfcc261e08ecc803dc79787
BLAKE2b-256 checksum
How to use checksums
e847f3605a74ecabdb7772421a8851d5cad13f52a174243099f059ac75dd9ac8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp312-cp312-win_amd64.whl
Size 784.1 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
3659852b84d595d787876668571ea3c61cdbf3753444f7b17b73beda854fafd1
BLAKE2b-256 checksum
How to use checksums
0bb4edcf3516f3dac2d38075c35dec1ccf6dd9e42dc389e07f619e9333734f32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250421203746-cp312-cp312-win32.whl
Size 742.4 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
87205f00c8cf7179814723784711d6cdcddb9e792d69e73046f35d750ed511a5
BLAKE2b-256 checksum
How to use checksums
58dbb082f845f5194a9f549546bf10849646d11bdf526351b7aab4541e4331e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
562c0c15a9b836200bcab80759d285ff8c9a2e9f6f1bf4e32bb984be1d319ee0
BLAKE2b-256 checksum
How to use checksums
94407063cc50a6cc2c91acdaec8b72d7619d24bf4f81d6c6412472862cbe35a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_11_0_arm64.whl
Size 821.2 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b4915e6a44459c471be05e85885b6c894fa4c35da17789e59ad14d218dd959c7
BLAKE2b-256 checksum
How to use checksums
3ce7ea8f98acda8f9ee7a9e1a558c7471e4bfd7596fd62eb168936233e1790f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp312-cp312-macosx_10_13_x86_64.whl
Size 862.1 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
75e5b9754182c9ac9ccac74bbcd249dadc2288fca119f13d4706f9a7377112b4
BLAKE2b-256 checksum
How to use checksums
212ddbb717c236debb52623e8471c0dca36fae3648e4da08f78fb241b4ff7d86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp311-cp311-win_amd64.whl
Size 812.7 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
e8f48e1116cb5bba5949b05b3b3fad6ce3753f0119897d167f0503a139b53cbe
BLAKE2b-256 checksum
How to use checksums
c3cc4cca18d5f2d1c5a0ebcf470e40c894ba1e622b145b0b78695a6166a78f0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250421203746-cp311-cp311-win32.whl
Size 777.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
81d6735ddd521f0868dba1677152e3a9dc2b7722de524840a7afb57ac805aa25
BLAKE2b-256 checksum
How to use checksums
b5b556aeddb87962db8752acb796464bef1581171dc9e9481a17cd17a577c51c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.8 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
125e6fa8c5fd3a7e9004742b89712b82eb65ed0ba59e2ad73605973ac5e21e31
BLAKE2b-256 checksum
How to use checksums
83070274b113d12600c8a6fea78af32bc1589000bca960446b0a4913dc091658
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_11_0_arm64.whl
Size 836.8 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c0214222c13aac2e630d22fee148867dad48c6f4666926afcf3a1c317ac20b54
BLAKE2b-256 checksum
How to use checksums
9c65483bffefcf31a09a763f6dba91b421f93b70250a135148ef319d21a78a92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp311-cp311-macosx_10_9_x86_64.whl
Size 898.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
63e5b554963e6eed5030e0af8366f4faa74eca04223513c41af5a501faae377d
BLAKE2b-256 checksum
How to use checksums
afc245a3212f90e6ec2f422b065c7da670013adf0824a0bf8d3650f9599bd5c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp310-cp310-win_amd64.whl
Size 809.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
67877fb9ab43fbc985c73123daa0dc076bb33e39e2773f1128276871a987eaf3
BLAKE2b-256 checksum
How to use checksums
38fd350f1ce541cb123906a9426847763b65037e5ced47c60cf23f369fb38639
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250421203746-cp310-cp310-win32.whl
Size 744.9 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
00a304c4ce6cc4ff6c2cb8544dc1fdb858fd190b09aa6f497e11bf0e644884d8
BLAKE2b-256 checksum
How to use checksums
32580f14d93bd0db4c8d13713b790175ff26b8330da5517742e99cc1c40fb407
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
387d31e6a69a4d5038e30edb2a3b76db092366579f8c66414ebda1e0187b2854
BLAKE2b-256 checksum
How to use checksums
431c1ce26e9d5912b46c0e85b9f9c4d584bef04aa1039dce9ee5576145edbb34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_11_0_arm64.whl
Size 802.6 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
53bc81fe6670fc2fc6cde1481e0d16177aa26be6f92820e73636b304367d1582
BLAKE2b-256 checksum
How to use checksums
4389c536fba877811549ec33191f59b4d71a75d6d24a43aaa9a310d0f8873794
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250421203746-cp310-cp310-macosx_10_9_x86_64.whl
Size 861.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d9e70cae42f444af4d7bc6f87bd2444eb1587a0295647da9233c0c3bd1549bf2
BLAKE2b-256 checksum
How to use checksums
9d0fa6c1748c1b00890750948bd346791b5cd53f7f1fb62493c82cc991197d7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release history Release notifications | RSS feed

0.0.2

30 release files

0.0.1

24 release files

This release
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