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.dev20250129041221

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.dev20250129041221
File
typedunits-0.0.1.dev20250129041221-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
typedunits-0.0.1.dev20250129041221-cp313-cp313-win32.whl CPython 3.13 CPython 3.13 Windows x86-32 Details
typedunits-0.0.1.dev20250129041221-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.dev20250129041221-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250129041221-cp313-cp313-macosx_10_13_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250129041221-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
typedunits-0.0.1.dev20250129041221-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details
typedunits-0.0.1.dev20250129041221-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.dev20250129041221-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250129041221-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250129041221-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
typedunits-0.0.1.dev20250129041221-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
typedunits-0.0.1.dev20250129041221-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.dev20250129041221-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250129041221-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
typedunits-0.0.1.dev20250129041221-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
typedunits-0.0.1.dev20250129041221-cp310-cp310-win32.whl CPython 3.10 CPython 3.10 Windows x86-32 Details
typedunits-0.0.1.dev20250129041221-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.dev20250129041221-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250129041221-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details

Total release size: 21.7 MB

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

Download URL typedunits-0.0.1.dev20250129041221-cp313-cp313-win_amd64.whl
Size 721.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
f3df309a484b4d2232b1b7f47d38cebf8df87ee1872707f8697bbe1cc34c9877
BLAKE2b-256 checksum
How to use checksums
b6e2b6d16d0dcc04df2d497e8c7e60c14f61b1afabbb0c6c154adc0513b014c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp313-cp313-win32.whl
Size 681.7 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
6b0289b5cc474905f669c97568b2495a0be14ebcd11ea10b49fac2f081d24c50
BLAKE2b-256 checksum
How to use checksums
f00573bc481924ced4cf453c3c3191a67cbc5b1dd6a00e3553dfaab7874e1f8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

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

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

Download URL typedunits-0.0.1.dev20250129041221-cp313-cp313-macosx_11_0_arm64.whl
Size 748.3 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ab14ff688d11fddfdbed3e0b3c9657f240dc6d25cc65ca2df6cf06d9f51b87a7
BLAKE2b-256 checksum
How to use checksums
0ee7f296f783072b0a9e853637d3af1d0ded4976cf759f35ebbb15ed31062504
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp313-cp313-macosx_10_13_x86_64.whl
Size 785.4 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
362f6f7f37935f28203c34cde628ec7f09383af81e40bbdb4456408e34c8dd87
BLAKE2b-256 checksum
How to use checksums
ed1e0e7a3c56f5928ae25ca33a50206e263021925c64fd3730199e02841d17ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp312-cp312-win_amd64.whl
Size 723.3 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
8308713edc6839946abf2ba03e876c2b23729229fd8d59b297ed54671871d0bc
BLAKE2b-256 checksum
How to use checksums
9db57b71f53032ac8b99bcf18abb53462e0d77b10e376a6d3ef20af2deda9557
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp312-cp312-win32.whl
Size 682.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
6d961d4d4ab615461fe30b7d87ec87e8d7dbfc2f3ac3af9326d13b963a9c906d
BLAKE2b-256 checksum
How to use checksums
2b4e1e7e99d9be05657d3096652c408861b58b3434715312538113924116730c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
9f9d89fe51f5ba492caac906e15b9b41f8056c9935ab7cfb6b0275008a97705c
BLAKE2b-256 checksum
How to use checksums
374727fa7c8348cd6199ea2aee0294ce6bf26a5df56138ed4054446e45635c83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp312-cp312-macosx_11_0_arm64.whl
Size 756.2 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
37d6b54711f128c856b14a63a81ff2b7975057ac73d8bc0b7ad16da700535b61
BLAKE2b-256 checksum
How to use checksums
7e84c17fdaa0d9d9d1573c9acead89a12173463cd9800f232288ec1c0700b595
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp312-cp312-macosx_10_13_x86_64.whl
Size 793.2 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
18be64c48358dcbf91ea8b99cc72d79a4fc2b7e89fce544a3ad70047a030512c
BLAKE2b-256 checksum
How to use checksums
8efa7e6535f399d1d755a0efb65852c291cb2d9ecd6c993886ea89aa10bfb3d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp311-cp311-win_amd64.whl
Size 750.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
6724fa751c35d4be2365f27be9be2f35d3ddfbb8eae56a810079623f104fffaf
BLAKE2b-256 checksum
How to use checksums
fd8abf25689d39199ac04d1a4e319669d90531015fd0c511019275b20e10b5bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp311-cp311-win32.whl
Size 715.9 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
4206e1faeff7da66391d86fe5d968e1e2a95951729aa2f2a59e42c0b951fcb39
BLAKE2b-256 checksum
How to use checksums
bf65da2e6d268a1901133a49ebbe2f3c8d3482b34e1d44238d04d8afb7144bca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
7ee5e2c8d277d3c2596027d074e3a980d74d48408396853ca5eccae8e0312b30
BLAKE2b-256 checksum
How to use checksums
1bc90464e95be7a99bdf5c758d65b0548a0c6deda9d2e605a4c9b701231dbd12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp311-cp311-macosx_11_0_arm64.whl
Size 767.4 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ceb5d0066ed00b372fe9b531bf9b589c3e067542be625f34d5d40c9c30fe71c5
BLAKE2b-256 checksum
How to use checksums
84f88039de24b80134199b49023b6678648a75a95575be58e61371fc4447679c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp311-cp311-macosx_10_9_x86_64.whl
Size 826.4 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ff130f2e6af4bba965bbdaf20d1e26f86cc06c55616ae71cebd4e4b8a53c3071
BLAKE2b-256 checksum
How to use checksums
026a5aa0ade3482db860f59638b3b161c2235ea020c70be17c91070d9a3d1344
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp310-cp310-win_amd64.whl
Size 746.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
e475054d4f4c5483d60d526e98b4909e7d4c2964d1735505f3f90efaca6fa848
BLAKE2b-256 checksum
How to use checksums
d34bf6fa7b9b7f0fd84fb9116479bfe4d990a6e3ec908fcbe56562a515f5267c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp310-cp310-win32.whl
Size 684.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
54fbb6114ec8b00a86fcb4b1ecc91bae76812dad129a31471a7cdebc0753beb6
BLAKE2b-256 checksum
How to use checksums
07f17c2c5673004dae878387dc26552ea4bccf96ba836353146c4780285df490
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.3 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
8715ad1032aad46e389e4d7a960fefbade3d986992a76bd561177aeaf5ea65d6
BLAKE2b-256 checksum
How to use checksums
7a7e23f2e700ec665166d4f91b043994f6bc611ae37300eb9e5dce2f49de0a55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp310-cp310-macosx_11_0_arm64.whl
Size 735.4 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a4996d1e2edbc9004d73ee2de7ee2195ee62d999f8c1b3def196c850f340370d
BLAKE2b-256 checksum
How to use checksums
83a61b5141eed37fbf8482ad6b08c64411558145065e193aa48df3ed81ace340
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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

Download URL typedunits-0.0.1.dev20250129041221-cp310-cp310-macosx_10_9_x86_64.whl
Size 792.3 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d283ee28712412df99ee746be373c1230b777cc9e946f73a4cfac0ecf191bb04
BLAKE2b-256 checksum
How to use checksums
24c5d2a6fd7a54063c244cbf0e9ae4dafb7fc25ee8dc287b953e37cd5ae6b19e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

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