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

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

Total release size: 22.8 MB

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

Download URL typedunits-0.0.1.dev20250509200845-cp313-cp313-win_amd64.whl
Size 735.3 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
e248b0d8059859402da69cea2bbf3adf87e8316ce8fcbe1d2ea75b14a05884b5
BLAKE2b-256 checksum
How to use checksums
16badadb83771bc63b73ca87525af0a8b0f51ffd2a8ab8775176b5b9db438744
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.dev20250509200845-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250509200845-cp313-cp313-win32.whl
Size 681.7 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
9abdb60a96a72659ebc2d580296cda63430e2797dd5d9e851b37a51d5a2ce5da
BLAKE2b-256 checksum
How to use checksums
f81a57deb7c8e24fe05542ff03555ef0d40b4bee3d10cfedc73d91927289f156
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.dev20250509200845-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
5bb2d6747e5eea1569a80b40f391afe5ba21d98ea4726e5626d5260fa2f198e4
BLAKE2b-256 checksum
How to use checksums
6d206c8226f86d630123634df6ffa379b5b4c21e80d6897237b7c1ef65baad1e
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.dev20250509200845-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp313-cp313-macosx_11_0_arm64.whl
Size 751.7 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4f02da50559ed5c223300e144282189fc4c4661e03516a3c5d2ad6ea3bcca6d1
BLAKE2b-256 checksum
How to use checksums
3dc3593532fcdee00d48c71d678cc759b945f6581cc34a21facf8c14efc79af2
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.dev20250509200845-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp313-cp313-macosx_10_13_x86_64.whl
Size 795.2 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
d896e23fa99600c2aa973a4cdc7d2533d6954f86f678d8aec869778fd586ab4b
BLAKE2b-256 checksum
How to use checksums
8b3aa4ab02060f6baf9670e6b1d26238eaaeef340605afd2894aed1745848fbc
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.dev20250509200845-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp312-cp312-win_amd64.whl
Size 731.7 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
284838ff984ce9dc9a4590de6b31e24118f425d804a97fdcc4732f1e7ee10ba3
BLAKE2b-256 checksum
How to use checksums
8bd0629564ebfcd34c0c0ae0bcac0c4d3957f553b3c85f1c5bab2dd2f9e00f38
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.dev20250509200845-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250509200845-cp312-cp312-win32.whl
Size 682.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
2092d51c76a8b0e4d21a86b4b375910be97b0842ff5cf745f1deeaf34fe43b10
BLAKE2b-256 checksum
How to use checksums
a8fdb523570c249ea595d4408908a284a34cfeacf43c53bbade754274ba2dae6
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.dev20250509200845-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
26c00049479a304c500ce4e4ba7e0919cb418bfa6af31f867ef65951c3f07c49
BLAKE2b-256 checksum
How to use checksums
06b56061f3697fe47763dd2f87c425e243dce12e2f96a6d3267be7d3d6ba7ffe
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.dev20250509200845-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp312-cp312-macosx_11_0_arm64.whl
Size 755.0 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e3d3e9fcba5524e8ef5fe0e35b327d161b9029f8bfde4669eaf7b27a7a38adec
BLAKE2b-256 checksum
How to use checksums
496f556095bd550d22e4b50b599db3f4ebf8a291d439bba08195c355ee74cdbc
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.dev20250509200845-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp312-cp312-macosx_10_13_x86_64.whl
Size 800.6 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
6c643add105740b238f27b81e1eedb5ca5b7399373a86ee744c4973e5aafc9ce
BLAKE2b-256 checksum
How to use checksums
8e170dfe00845bc5646b0470862e0b7414b2d79616667ed11408fba778b3a0a1
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.dev20250509200845-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp311-cp311-win_amd64.whl
Size 797.0 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
eb33b305141aace6ab10bb7746f835c11e911480ca19b36a2825056dec5aa57f
BLAKE2b-256 checksum
How to use checksums
d1739d642f96d64fb75d0726cb05ccd32ffe977cc1e8cfc4d5811a2e0c3e48f4
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.dev20250509200845-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250509200845-cp311-cp311-win32.whl
Size 746.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
0dba823ba557e01520f17ccd61523246c1f79a6505e6d6b9fe22c429f0d6853f
BLAKE2b-256 checksum
How to use checksums
0b49bad0747c0f5f1b8ed0851e785efd84104eeac8a8165618b293de0fd43890
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.dev20250509200845-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250509200845-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
6b80a595b23c4321af1560339bca1a91ea73168a48c79ad70479a109e2db6388
BLAKE2b-256 checksum
How to use checksums
70da0fa540c69c83ec8691ddcba7b2b0c72fcf2f47a06be069fbd39b57d59f98
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.dev20250509200845-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp311-cp311-macosx_11_0_arm64.whl
Size 780.4 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c2ea288d6330f06f387f244c1deec3ab081cf5af808fd47f3960d30f0360516c
BLAKE2b-256 checksum
How to use checksums
cba62d3fde3e53cf034d322f8ac349dfe5f38e7045487324c036d6955fac1383
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.dev20250509200845-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp311-cp311-macosx_10_9_x86_64.whl
Size 846.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
76515ea205c104ae79930613283736579b229dd3535ffc6157d2353459cc09fd
BLAKE2b-256 checksum
How to use checksums
c6114ebf4302e27edc211b08d069073e24fecfec9ed19a675ebb41cbaffe231e
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.dev20250509200845-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp310-cp310-win_amd64.whl
Size 796.7 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
e56701ce895e5c40c7dc8af294f966bf3f05270b1fcfc953c72cdd81407ee025
BLAKE2b-256 checksum
How to use checksums
fa14b1b1fc39217b1c1c9b02f626d712889d2b60277698e5d603ffb9ca9d2802
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.dev20250509200845-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250509200845-cp310-cp310-win32.whl
Size 717.3 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
6b5e1b29e07aaa334303144d5657698e543354da468d0379b5e3319b4a391521
BLAKE2b-256 checksum
How to use checksums
ce172bb01490e05fde4d9c52ed581608cdd4b7b10b5bb4b228f974669af776ba
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.dev20250509200845-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
2691531e60821528e13ff45109e144b690dd1b7bad06bccfd2e6bb4248c49bc2
BLAKE2b-256 checksum
How to use checksums
5daad42dc19c9b7c7f59fb6d6e3e751b2e43c0c116f24ddc35d442ab06435901
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.dev20250509200845-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp310-cp310-macosx_11_0_arm64.whl
Size 743.1 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6e108974f76fda3785ce159e0070c5a630da0b65a9fdd0bd15b4117ca8e708e8
BLAKE2b-256 checksum
How to use checksums
bbc2c81546d79d87d67d389ec8f9966e01348b804001722bc887d093764d079d
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.dev20250509200845-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250509200845-cp310-cp310-macosx_10_9_x86_64.whl
Size 808.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
51b7df21463c6803549ad022885da11799f5688c676dae1b5008ffe3f42e3201
BLAKE2b-256 checksum
How to use checksums
db091455e5afae871372729dfbed9b89ad04bdf22b71f18e74606e1b3010c4d9
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